loki2.utils.logger ================== .. py:module:: loki2.utils.logger .. autoapi-nested-parse:: Logging utilities for Loki2. This module provides colored logging functionality with support for both console and file-based logging with rotation. Module Contents --------------- .. py:class:: ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True) Bases: :py:obj:`logging.Formatter` A custom formatter class that adds color to log messages based on the log level. :param logging.Formatter: The base class for formatting log messages. .. method:: format(record) Formats the log record and adds color based on the log level. .. attribute:: None .. rubric:: Example formatter = ColoredFormatter() log_handler.setFormatter(formatter) .. py:method:: format(record: logging.LogRecord) -> str Format the log record and add color based on the log level. :param record: The log record to be formatted. :returns: The formatted log message with added color. :rtype: str .. py:class:: Logger(level: Literal['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], log_dir: Union[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. :param level: Logger.level :type level: Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"] :param log_dir: Path to save logfile in. Defaults to None. :type log_dir: Union[Path, str], optional :param comment: additional comment for save file. Defaults to 'logs'. :type comment: str, optional :param formatter: Custom formatter. Defaults to None. :type formatter: str, optional :param use_timestamp: Using timestamp for time-logging. Defaults to False. :type use_timestamp: bool, optional :param file_level: 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. :type file_level: Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"], optional .. attribute:: level The level of logging. :type: str .. attribute:: comment The comment to be added to the log file name. :type: str .. attribute:: log_parent_dir The directory where the log file will be saved. :type: Union[Path, str] .. attribute:: use_timestamp Whether to use timestamp for time-logging. :type: bool .. attribute:: formatter The formatter for the log messages. :type: str .. attribute:: file_level The level of logging for the output file. :type: str .. method:: create_handler(logger logging.Logger): Creates logging handler for sys output and rotating files in parent_dir. .. method:: create_logger() -> logging.Logger Creates the logger. :raises FileNotFoundError: If the specified log directory does not exist. .. rubric:: Example >>> logger = Logger(level="INFO", log_dir="/path/to/log_dir", comment="my_logs", use_timestamp=True) >>> logger.create_logger() .. py:attribute:: level .. py:attribute:: comment .. py:attribute:: log_parent_dir .. py:attribute:: use_timestamp .. py:method:: create_handler(logger: logging.Logger) -> None Create logging handler for sys output and rotating files in parent_dir. :param logger: The logger instance to add handlers to. .. py:method:: create_logger() -> logging.Logger Create the logger :returns: The logger to be used. :rtype: Logger