Skip to content

fake-api-server.ci.surveillance.log

This module provides functionality for initializing and configuring logging settings. It allows customization of logging formats, date/time formats, and logging levels. The module also ensures compatibility for Python versions below 3.9 by handling the absence of the encoding parameter in the logging.basicConfig function.

init_logger_config(formatter='', datefmt='', level=logging.INFO, encoding='utf-8')

Configures the logger with the specified settings and initializes the logging system. Allows setting the format, date format, logging level, and encoding for the logs. If the logging level is DEBUG, it overrides the formatter and datefmt with predefined debug-level configurations. Compatibility for Python versions below 3.9 is handled by omitting the encoding parameter in those cases.

PARAMETER DESCRIPTION
formatter

Specifies the log message format. Defaults to an empty string.

TYPE: str DEFAULT: ''

datefmt

Specifies the date/time format for log messages. Defaults to an empty string.

TYPE: str DEFAULT: ''

level

Sets the logging level (e.g., DEBUG, INFO, WARNING). Defaults to logging.INFO.

TYPE: int DEFAULT: INFO

encoding

Encoding used for log messages. Defaults to "utf-8". This parameter is applied only for Python versions 3.9 and above.

TYPE: str DEFAULT: 'utf-8'

RETURNS DESCRIPTION
None

This function does not return a value.

Source code in fake_api_server_plugin/ci/surveillance/log.py
def init_logger_config(
    formatter: str = "",
    datefmt: str = "",
    level: int = logging.INFO,
    encoding: str = "utf-8",
) -> None:
    """
    Configures the logger with the specified settings and initializes the logging system.
    Allows setting the format, date format, logging level, and encoding for the logs.
    If the logging level is DEBUG, it overrides the `formatter` and `datefmt` with predefined
    debug-level configurations. Compatibility for Python versions below 3.9 is handled
    by omitting the encoding parameter in those cases.

    :param formatter: Specifies the log message format. Defaults to an empty string.
    :param datefmt: Specifies the date/time format for log messages. Defaults to an empty string.
    :param level: Sets the logging level (e.g., DEBUG, INFO, WARNING). Defaults to logging.INFO.
    :param encoding: Encoding used for log messages. Defaults to "utf-8". This parameter is applied
        only for Python versions 3.9 and above.
    :return: This function does not return a value.
    """
    if level == logging.DEBUG:
        formatter = DEBUG_LEVEL_LOG_FORMAT
        datefmt = DEBUG_LEVEL_LOG_DATETIME_FORMAT

    if sys.version_info >= (3, 9):
        logging.basicConfig(
            format=formatter,
            datefmt=datefmt,
            level=level,
            encoding=encoding,
        )
    else:
        logging.basicConfig(
            format=formatter,
            datefmt=datefmt,
            level=level,
        )