Logger#

Module providing enhanced logging functionality with support for custom date formats and color-coded console output.

This module defines the Logger class, which extends the Translator class to provide logging capabilities with customizable date formats and color-coded console messages. The Logger class supports logging messages with different levels of severity and can write logs to both the console and a file. It also includes support for log rotation based on file size and backup count.

Dependencies:
  • logging: Standard Python logging module for logging messages.

  • logging.handlers: Provides handlers for logging, including rotating file handlers.

  • sys: Provides access to system-specific parameters and functions.

  • os: Provides a portable way of using operating system-dependent functionality.

  • time: Provides time-related functions.

  • modules.translator: Custom module for handling message translations.

  • modules.colored_formatter: Custom module defining the ColoredFormatter class for color-coded logging.

Classes:
  • Logger:

    A logging class that extends the Translator class to handle logging messages with custom formatting and color output.

- `local`

The default language locale for translations.

Type:

str

- `date_format`

Format for date and time in log messages.

Type:

str

- `log_path`

Path to the log file where logs will be written. If not specified, logs are only output to the console.

Type:

str

- `log_level`

Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) to control which messages are logged.

Type:

str

- `log_max_size`

Maximum size of the log file before rotation occurs, specified in megabytes.

Type:

int

- `log_backup_count`

Number of backup files to keep when rotating logs.

Type:

int

Usage:

Initialize the Logger class with parameters to set up logging configurations such as file path, log level, date format, maximum file size, and backup count. Logs can be written to the console and/or a file, and messages will be color-coded based on their severity level in the console.

Example configuration:

logger = Logger(
    local="en",
    date_format="%Y-%m-%d %H:%M:%S",
    log_path="path/to/logfile.log",
    log_level="INFO",
    log_max_size=10,  # in megabytes
    log_backup_count=5
)
Log methods:
  • info(msg_key=””, **kwargs): Logs an informational message.

  • success(msg_key=””, **kwargs): Logs a success message.

  • warning(msg_key=””, **kwargs): Logs a warning message.

  • error(msg_key=””, **kwargs): Logs an error message.

  • debug(msg_key=””, **kwargs): Logs a debug message.

  • critical(msg_key=””, **kwargs): Logs a critical message.

Error Handling:

The Logger class raises specific exceptions with translated messages when invalid parameters are provided: - InvalidLogSizeError: Raised if log_max_size is not a positive integer. - InvalidLogCountError: Raised if log_backup_count is not a non-negative integer. - InvalidLogLevelError: Raised if log_level is not one of the valid logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).

class modules.logger.Logger[source]#

Bases: Translator

Logger class that extends the Translator class to handle logging messages with custom formatting and color output.

local#

The default language locale for translations.

Type:

str

date_format#

Format for date and time in log messages.

Type:

str

log_path#

Path to the log file where logs will be written. If not specified, logs are only output to the console.

Type:

str

log_level#

Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) to control which messages are logged.

Type:

str

log_max_size#

Maximum size of the log file before rotation occurs, specified in bytes.

Type:

int

log_backup_count#

Number of backup files to keep when rotating logs.

Type:

int

__init__(local='en', date_format='%Y-%m-%d %H:%M:%S', log_path=None, log_level='INFO', log_max_size=10, log_backup_count=5)[source]#

Initializes the Logger with a default locale, date format, and logging configuration.

Parameters:
  • local (str, optional) – The default language locale for translations. Defaults to ‘en’.

  • date_format (str, optional) – The format for date and time in log messages. Defaults to “%Y-%m-%d %H:%M:%S”.

  • log_path (str, optional) – Path to the log file. If None, logs are not written to a file. Defaults to None.

  • log_level (str, optional) – The logging level. Defaults to “INFO”. Valid values are DEBUG, INFO, WARNING, ERROR, CRITICAL.

  • log_max_size (int, optional) – Maximum log file size before rotation in megabytes. Defaults to 10.

  • log_backup_count (int, optional) – Number of backup files to keep. Defaults to 5.

info(msg_key='', **kwargs)[source]#

Logs an informational message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None

success(msg_key='', **kwargs)[source]#

Logs a success message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None

warning(msg_key='', **kwargs)[source]#

Logs a warning message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None

error(msg_key='', **kwargs)[source]#

Logs an error message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None

debug(msg_key='', **kwargs)[source]#

Logs a debug message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None

critical(msg_key='', **kwargs)[source]#

Logs a critical message.

Parameters:
  • msg_key (str) – The key for the message to translate.

  • **kwargs – Additional keyword arguments to format the translated message.

Return type:

None