mylogging.my_traceback package

Contain tools for logging of caught exceptions or and raised errors.

Usually there is one function that is heavily used and it is ‘enhance_excepthook’. If you call it and then somewhere use raise, it will make it colorful, highlight a user message and if debugging, remove extra stack frames.

Another name for this subpackage is also exceptions as this is hard to separate those terms.

mylogging.my_traceback.enhance_excepthook()[source]

Change default excepthook to formatted one.

That means that if there is a uncaught raise, output message with traceback will be colored if possible.

mylogging.my_traceback.enhance_excepthook_reset()[source]

Reset original excepthook.

mylogging.my_traceback.format_traceback(message: str = '', caption: str = 'error_type', level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'ERROR', remove_frame_by_line_str: None | list = None) str[source]

Raise warning with current traceback as content. It means, that error was caught, but still something crashed.

Parameters
  • message (str) – Any string content of traceback.

  • caption (str, optional) – Caption of warning. If ‘error_type’, than Error type (e.g. ZeroDivisionError)

  • 'error_type'. (is used. Defaults to) –

  • level (Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], optional) – Defaults to “DEBUG”.

  • stack_level (int, optional) – How many calls to log from error. Defaults to 3.

  • remove_frame_by_line_str (None | list, optional) – If there is some level in stack that should be

  • omitted

  • None. (add line here. Defaults to) –

mylogging.my_traceback.get_traceback_str_with_removed_frames(lines: Sequence[str], exact_match: bool = True) str[source]

Remove particular levels of stack trace defined by content.

Note

If not using exact_match, beware of not using short message that can be also elsewhere, where not supposed, as it can make debugging a nightmare.

Parameters
  • lines (list) – Line in call stack that we want to hide.

  • exact_match (bool, optional) – If True, stack frame will be removed only if it is exactly the same. If False, then line can be just subset of stack frame.

Returns

String traceback ready to be printed.

Return type

str

Example

>>> def buggy():
...     return 1 / 0
...
>>> try:
...     buggy()
... except ZeroDivisionError:
...     traceback = get_traceback_str_with_removed_frames([])
...     traceback_cleaned = get_traceback_str_with_removed_frames(["buggy()"])
>>> "buggy()" in traceback
True
>>> "buggy()" not in traceback_cleaned
True
mylogging.my_traceback.raise_enhanced(exception_type: Type[Exception], value: BaseException | None, traceback: None | TracebackType, clean_debug_extra: bool = True, highlight: bool = True, indent: int = 2) None[source]

Enhance printed exception.

Message as well as traceback. It adds colors if configured. You can call it directly. This function can be directly assigned to sys.excepthook.

Parameters
  • exception_type (Type[Exception]) – E.g. <class ‘RuntimeError’>.

  • value (BaseException | None) –

      1. RuntimeError.

  • traceback (None | TracebackType) – Traceback.

  • clean_debug_extra (bool, optional) – There can be some extra lines of stack trace for example when debugging. This will remove those lines. E.g. lines from VS Code python extension or runpy.py. Defaults to True.

  • highlight (bool, optional) – Highlight main message from the error. Defaults to True.

  • indent (int, optional) – Whether indent raised message or not. Defaults to 2.

mylogging.my_traceback.remove_debug_stack_trace(traceback_list: list[str]) list[str][source]

On windows when debugging (at least in VS Code) caught stack trace from raise function contain extra files not used by user or imported library. All the extra lines come from runpy.py. This will remove such a lines so the error is more readable.

Parameters

traceback_list (list[str]) – List of stack trace messages.

Returns

Returns actual list of strings without extra runpy.py content.

Return type

str

Submodules