mylogging.colors.colors_module module

Module for colors. Contain functions for coloring messages by level (yellow, red, grey…) or function that color python code.

class mylogging.colors.colors_module.ColorsConfig[source]

Bases: object

Settings for color module.

COLOR_PALETTE: dict[str, str] = {'CRITICAL': '\x1b[31;1m', 'DEBUG': '\x1b[38;21m', 'ERROR': '\x1b[31;21m', 'INFO': '\x1b[38;21m', 'WARNING': '\x1b[33;21m', 'reset': '\x1b[0m'}

Ansi code for defined colors like for example x1b[33;21m

USE_COLORS: bool = True

You can set this variable and then use it in colorize function so you can configure it from one place. It can also be configured from config module.

mylogging.colors.colors_module.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.colors_module.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...