loki2.utils.logger

Logging utilities for Loki2.

This module provides colored logging functionality with support for both console and file-based logging with rotation.

Module Contents

class loki2.utils.logger.ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True)

Bases: logging.Formatter

A custom formatter class that adds color to log messages based on the log level.

Parameters:

logging.Formatter – The base class for formatting log messages.

format(record)

Formats the log record and adds color based on the log level.

None

Example

formatter = ColoredFormatter() log_handler.setFormatter(formatter)

format(record: logging.LogRecord) str

Format the log record and add color based on the log level.

Parameters:

record – The log record to be formatted.

Returns:

The formatted log message with added color.

Return type:

str

class loki2.utils.logger.Logger(level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], log_dir: pathlib.Path | str = None, comment: str = 'logs', formatter: str = None, use_timestamp: bool = False, file_level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] = None)

“A Logger for sys-logging and RotatingFileHandler-logging using the python logging module.

Initialize a Logger for sys-logging and RotatingFileHandler-logging by using python logging module. The logger can be used out of the box without any changes, but is also adaptable for specific use cases. In basic configuration, just the log level must be provided. If log_dir is provided, another handler object is created logging into a file into the log_dir directory. The filename can be changes by using comment, which basically is the filename. To create different log files with specific timestamp set ‘use_timestamp’ = True. This adds an additional timestamp to the filename.

Parameters:
  • level (Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]) – Logger.level

  • log_dir (Union[Path, str], optional) – Path to save logfile in. Defaults to None.

  • comment (str, optional) – additional comment for save file. Defaults to ‘logs’.

  • formatter (str, optional) – Custom formatter. Defaults to None.

  • use_timestamp (bool, optional) – Using timestamp for time-logging. Defaults to False.

  • file_level (Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"], optional) – Set Logger.level. for output file. Can be useful if a different logging level should be used for terminal output and logging file. If no level is selected, file level logging is the same as for console. Defaults to None.

level

The level of logging.

Type:

str

comment

The comment to be added to the log file name.

Type:

str

log_parent_dir

The directory where the log file will be saved.

Type:

Union[Path, str]

use_timestamp

Whether to use timestamp for time-logging.

Type:

bool

formatter

The formatter for the log messages.

Type:

str

file_level

The level of logging for the output file.

Type:

str

create_handler(logger

logging.Logger): Creates logging handler for sys output and rotating files in parent_dir.

create_logger() logging.Logger

Creates the logger.

Raises:

FileNotFoundError – If the specified log directory does not exist.

Example

>>> logger = Logger(level="INFO", log_dir="/path/to/log_dir", comment="my_logs", use_timestamp=True)
>>> logger.create_logger()
<Logger __main__ (INFO)>
level
comment
log_parent_dir
use_timestamp
create_handler(logger: logging.Logger) None

Create logging handler for sys output and rotating files in parent_dir.

Parameters:

logger – The logger instance to add handlers to.

create_logger() logging.Logger

Create the logger

Returns:

The logger to be used.

Return type:

Logger