mylogging.my_traceback.my_traceback_module module

Functions for my_traceback subpackage.

mylogging.my_traceback.my_traceback_module.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.my_traceback_module.enhance_excepthook_reset()[source]

Reset original excepthook.

mylogging.my_traceback.my_traceback_module.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.my_traceback_module.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.my_traceback_module.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.my_traceback_module.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