mylogging.colors package

Subpackage that helps with colors. It contains palette of color codes or functions that will color string.

mylogging.colors.colorize(message: str, level: typing_extensions.Literal[DEBUG, INFO, WARNING, ERROR, CRITICAL] = 'WARNING', use: Optional[bool] = None) str[source]

Add color to message based on level.

Usually warnings and errors, to know what is internal error on first sight. There is global config.colorize value that can be configured, so it’s not necessary to pass as argument.

Parameters
  • message (str) – Any string you want to color.

  • level (str, optional) – “INFO” and “DEBUG” not colored, “WARNING”: yellow, “ERROR”: red or “CRITICAL”: more red. Defaults to “WARNING”.

  • use (Union[bool, None], optional) – It’s possible to turn on and off colors with one config variable to keep syntax simple. Defaults to None.

Returns

Message in yellow color. Symbols added to string cannot be read in some terminals.

If config colorize is False, it return original string.

Return type

str

Example

>>> message = "Hello there"
>>> colored_message = colorize(message, use=True)
>>> colored_message
'\x1b[33;21m Hello there \x1b[0m'
mylogging.colors.colorize_traceback(traceback_str: str) str[source]

Colorize traceback to be more readable.

Parameters

traceback_str (str) – Get from traceback with traceback.format_exc().

Returns

String with added symbols cause that string will be colorized.

Return type

str

Example

>>> import traceback
...
>>> try:
...     1/0
... except ZeroDivisionError:
...     colorize_traceback(traceback.format_exc())
'Traceback (most recent call last):\n  File \x1b[36m"<doctest mylogging.colors.colors...

Submodules