logs#

This module contains two classed designed for enhanced logging experience: ColorFormatter and CustomLogger.

ColorFormatter#

class pocketwelt.logs.ColorFormatter(fmt: str | None = '%(asctime)s  %(levelname)-8s %(filename)s:%(lineno)d >>  %(message)s', datefmt: str | None = '%Y-%m-%d %H:%M:%S', style: Literal['%', '{', '$'] = '%', use_colors: bool = True)#

Bases: Formatter

Custom colored formatter converting logRecord to text.

Adapted from:
color_message(level_no: int, msg: str) str#

Color the log message based on the log level.

Parameters:
  • level_no (int) – The log level number.

  • msg (str) – The log message.

Returns:

The colored log message.

Return type:

str

format(record: LogRecord) str#

Format the specified record.

Parameters:

record (logging.LogRecord) – The log record to be formatted.

Returns:

The formatted log record as a string, possibly with color applied.

Return type:

str

level_name_colors = {10: <function ColorFormatter.<lambda>>, 20: <function ColorFormatter.<lambda>>, 30: <function ColorFormatter.<lambda>>, 40: <function ColorFormatter.<lambda>>, 50: <function ColorFormatter.<lambda>>}#

CustomLogger#

class pocketwelt.logs.CustomLogger(name: str, level: str | int = 30, formatter: Formatter | None = None, log_directory: str | None = None, use_colors: bool = True, **fmt_kwargs: Any)#

Bases: Logger

Custom Logger class accepting formatter objects and adding file logging option.

setLevel(level: str | int) None#

Set the logging level for this logger and all its handlers.

Parameters:

level (Union[str, int]) – The logging level to set. Can be a string (e.g., ‘INFO’, ‘DEBUG’) or an integer representing the level.

stdout_to_logger#

pocketwelt.logs.stdout_to_logger(logger: Logger, func: Callable, *args) str#

Redirect stdout to a logger and execute a function. Method can be used when you are not sure were given print() statements are located in huge codebases, so you can wrap any method in this and force logs via your logging system.

Parameters:
  • logger (logging.Logger) – Your own logger to use for logging the stdout output.

  • func (Callable) – The function to execute with stdout redirection.

  • *args – Variable length argument list to pass to the function.

Returns:

The return value of the executed function.

Return type:

Any

getCustomLogger#

pocketwelt.logs.getCustomLogger(name: str, level: str | int = 30) CustomLogger#

Create custom logger instance.

Parameters:
  • name (str) – Name of the logger.

  • level (Union[str, int], optional) – The logging level for the logger. Can be a string (e.g., ‘INFO’, ‘DEBUG’) or an integer. Defaults to logging.WARNING.

Returns:

Instance of the CustomLogger.

Return type:

CustomLogger

Note

This method temporarily replaces base class for loggers. We do this because we do not want to multiply the Manager object created initially in logging.py. Instead we use this object to create loggers with our custom class - logger hierarchy is intact and we get our colorful logs this way.

DATETIME_FMT#

Default datetime format:

DATETIME_FMT = "%Y-%m-%d %H:%M:%S"

LOG_FMT#

Default logs format:

LOG_FMT = "%(asctime)s  %(levelname)-8s %(filename)s:%(lineno)d >>  %(message)s"