Skip to content

Log Settings

disruption_py.settings.log_settings.LogSettings dataclass ¤

Settings for configuring logging.

ATTRIBUTE DESCRIPTION
log_file_path

Path to the log file. If None, no log file will be created (default is None).

TYPE: (str, optional)

file_log_level

Logging level for the log file (default is logging.WARNING). Can be set to logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, or logging.CRITICAL.

TYPE: (int, optional)

log_file_write_mode

File write mode for the log file (default is 'w').

TYPE: (str, optional)

log_to_console

Whether to log messages to the console (default is True).

TYPE: (bool, optional)

console_log_level

Logging level for console output (default is logging.WARNING). Can be set to logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, or logging.CRITICAL.

TYPE: (int, optional)

use_custom_logging

Whether to use custom logging setup (default is False). If True, no logging setup is done within this class.

TYPE: (bool, optional)

_logging_has_been_setup

Internal flag to prevent multiple setups (default is False).

TYPE: (bool, optional)

Source code in disruption_py/settings/log_settings.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@dataclass
class LogSettings:
    """
    Settings for configuring logging.

    Attributes
    ----------
    log_file_path : str, optional
        Path to the log file. If None, no log file will be created (default is None).
    file_log_level : int, optional
        Logging level for the log file (default is logging.WARNING).
        Can be set to logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
        or logging.CRITICAL.
    log_file_write_mode : str, optional
        File write mode for the log file (default is 'w').
    log_to_console : bool, optional
        Whether to log messages to the console (default is True).
    console_log_level : int, optional
        Logging level for console output (default is logging.WARNING).
        Can be set to logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
        or logging.CRITICAL.
    use_custom_logging : bool, optional
        Whether to use custom logging setup (default is False).
        If True, no logging setup is done within this class.
    _logging_has_been_setup : bool, optional
        Internal flag to prevent multiple setups (default is False).
    """

    log_file_path: str = None
    file_log_level: int = logging.WARNING
    log_file_write_mode: str = "w"

    log_to_console: bool = True
    console_log_level: int = logging.WARNING

    use_custom_logging: bool = False

    _logging_has_been_setup: bool = False

    def logger(self, logger_name="disruption_py") -> logging.Logger:
        """
        Get or set up the logger with the specified settings.

        Parameters
        ----------
        logger_name : str, optional
            Name of the logger (default is "disruption_py").

        Returns
        -------
        logging.Logger
            The configured logger instance.
        """
        return self.setup_logging(logger_name)

    def setup_logging(self, logger_name="disruption_py") -> logging.Logger:
        """
        Set up logging based on the provided settings.

        Parameters
        ----------
        logger_name : str, optional
            Name of the logger (default is "disruption_py").

        Returns
        -------
        logging.Logger
            Configured logger instance.
        """
        logger = logging.getLogger(logger_name)
        if self.use_custom_logging or self._logging_has_been_setup:
            return logger
        self._logging_has_been_setup = True

        formatter = logging.Formatter(
            "%(asctime)s,%(msecs)d %(name)s %(levelname)s | %(message)s", "%H:%M:%S"
        )
        logger.propagate = False
        logger.handlers.clear()
        logger.setLevel(logging.DEBUG)
        if self.log_file_path is not None:
            fh = logging.FileHandler(self.log_file_path, mode=self.log_file_write_mode)
            fh.setFormatter(formatter)
            fh.setLevel(self.file_log_level)
            logger.addHandler(fh)
        if self.log_to_console:
            sh = logging.StreamHandler()
            sh.setLevel(self.console_log_level)
            sh.setFormatter(formatter)
            logger.addHandler(sh)
        return logger

logger ¤

logger(logger_name='disruption_py') -> logging.Logger

Get or set up the logger with the specified settings.

PARAMETER DESCRIPTION
logger_name

Name of the logger (default is "disruption_py").

TYPE: str DEFAULT: 'disruption_py'

RETURNS DESCRIPTION
Logger

The configured logger instance.

Source code in disruption_py/settings/log_settings.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def logger(self, logger_name="disruption_py") -> logging.Logger:
    """
    Get or set up the logger with the specified settings.

    Parameters
    ----------
    logger_name : str, optional
        Name of the logger (default is "disruption_py").

    Returns
    -------
    logging.Logger
        The configured logger instance.
    """
    return self.setup_logging(logger_name)

setup_logging ¤

setup_logging(
    logger_name="disruption_py",
) -> logging.Logger

Set up logging based on the provided settings.

PARAMETER DESCRIPTION
logger_name

Name of the logger (default is "disruption_py").

TYPE: str DEFAULT: 'disruption_py'

RETURNS DESCRIPTION
Logger

Configured logger instance.

Source code in disruption_py/settings/log_settings.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def setup_logging(self, logger_name="disruption_py") -> logging.Logger:
    """
    Set up logging based on the provided settings.

    Parameters
    ----------
    logger_name : str, optional
        Name of the logger (default is "disruption_py").

    Returns
    -------
    logging.Logger
        Configured logger instance.
    """
    logger = logging.getLogger(logger_name)
    if self.use_custom_logging or self._logging_has_been_setup:
        return logger
    self._logging_has_been_setup = True

    formatter = logging.Formatter(
        "%(asctime)s,%(msecs)d %(name)s %(levelname)s | %(message)s", "%H:%M:%S"
    )
    logger.propagate = False
    logger.handlers.clear()
    logger.setLevel(logging.DEBUG)
    if self.log_file_path is not None:
        fh = logging.FileHandler(self.log_file_path, mode=self.log_file_write_mode)
        fh.setFormatter(formatter)
        fh.setLevel(self.file_log_level)
        logger.addHandler(fh)
    if self.log_to_console:
        sh = logging.StreamHandler()
        sh.setLevel(self.console_log_level)
        sh.setFormatter(formatter)
        logger.addHandler(sh)
    return logger