The GlobalLogger class provides a flexible logging system for the SupplyNetPy simulations. It allows logging messages to both the console and a file, and can be easily enabled or disabled during runtime.

SupplyNetPy.Components.logger

GlobalLogger

GlobalLogger(logger_name='sim_trace', log_to_file=True, log_file='simulation_trace.log', log_to_screen=True)

A simple logger class that allows logging messages to the screen or to a file. Logging can be turned on or off.

Parameters:
  • log_to_file (bool, default: True ) –

    Whether to log messages to a file.

  • log_file (str, default: 'simulation_trace.log' ) –

    The file to which log messages will be written.

  • log_to_screen (bool, default: True ) –

    Whether to log messages to the screen.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

  • log_to_file (bool) –

    Flag indicating whether to log to a file.

  • log_to_screen (bool) –

    Flag indicating whether to log to the screen.

Methods:

Name Description
set_log_file

Sets the log file to write simulation logs.

get_log_file

Returns the current log file name (path) in which simulation logs are written.

set_logger

Sets the logger name to write simulation logs.

configure_logger

Configures the logger by adding handlers based on user preferences.

log

Logs a message at the specified level.

enable_logging

Enables logging by configuring the logger with the appropriate handlers.

disable_logging

Disables logging by clearing all handlers from the logger.

Initializes the Logger with options to log to file, log file name, and log to screen.

Parameters:
  • log_to_file (bool, default: True ) –

    Whether to log messages to a file. Defaults to False.

  • log_file (str, default: 'simulation_trace.log' ) –

    The file to which log messages will be written. Defaults to 'app.log'.

  • log_to_screen (bool, default: True ) –

    Whether to log messages to the screen. Defaults to True.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

  • log_to_file (bool) –

    Flag indicating whether to log to a file.

  • log_to_screen (bool) –

    Flag indicating whether to log to the screen.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def __init__(self, logger_name='sim_trace', log_to_file=True, log_file='simulation_trace.log', log_to_screen=True):
    """
    Initializes the Logger with options to log to file, log file name, and log to screen.

    Parameters:
        log_to_file (bool): Whether to log messages to a file. Defaults to False.
        log_file (str): The file to which log messages will be written. Defaults to 'app.log'.
        log_to_screen (bool): Whether to log messages to the screen. Defaults to True.

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages.
        log_to_file (bool): Flag indicating whether to log to a file.
        log_to_screen (bool): Flag indicating whether to log to the screen.

    Returns:
        None
    """
    self.logger = logging.getLogger(logger_name)
    self.logger.setLevel(logging.DEBUG)

    self.log_to_file = log_to_file
    self.log_to_screen = log_to_screen

    self.log_file = log_file
    self.configure_logger()

set_log_file

set_log_file(filename)

Sets given log file to write simulation logs

Parameters:
  • filename (str) –

    The file to which log messages will be written.

Attributes:
  • log_file (str) –

    The file to which log messages will be written.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def set_log_file(self,filename):
    """
    Sets given log file to write simulation logs

    Parameters:
        filename (str): The file to which log messages will be written.

    Attributes:
        log_file (str): The file to which log messages will be written.

    Returns:
        None
    """
    self.log_file = filename
    self.configure_logger()

get_log_file

get_log_file()

Returns current log file name (path) in which simulation logs are written

Returns:
  • str

    The current log file name (path).

Source code in src/SupplyNetPy/Components/logger.py
def get_log_file(self):
    """
    Returns current log file name (path) in which simulation logs are written

    Parameters:
        None

    Attributes:
        None

    Returns:
        str: The current log file name (path).
    """
    return self.log_file

set_logger

set_logger(logger_name)

Sets given logger name to write simulation logs

Parameters:
  • logger_name (str) –

    The name of the logger to be used for logging messages.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def set_logger(self,logger_name):
    """
    Sets given logger name to write simulation logs

    Parameters:
        logger_name (str): The name of the logger to be used for logging messages.

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages.

    Returns:
        None
    """
    self.logger = logging.getLogger(logger_name)

configure_logger

configure_logger()

Configures the logger by adding handlers based on user preferences.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

  • log_to_file (bool) –

    Flag indicating whether to log to a file.

  • log_to_screen (bool) –

    Flag indicating whether to log to the screen.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def configure_logger(self):
    """
    Configures the logger by adding handlers based on user preferences.

    Parameters:
        None

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages.
        log_to_file (bool): Flag indicating whether to log to a file.
        log_to_screen (bool): Flag indicating whether to log to the screen.

    Returns:
        None
    """
    self.logger.handlers = []

    if self.log_to_screen:
        screen_handler = logging.StreamHandler()
        screen_handler.setLevel(logging.DEBUG)
        screen_format = logging.Formatter('%(levelname)s %(name)s - %(message)s')
        screen_handler.setFormatter(screen_format)
        self.logger.addHandler(screen_handler)

    if self.log_to_file:
        file_handler = logging.FileHandler(self.log_file, mode='w')
        file_handler.setLevel(logging.DEBUG)
        file_format = logging.Formatter('%(levelname)s %(name)s - %(message)s')
        file_handler.setFormatter(file_format)
        self.logger.addHandler(file_handler)

log

log(level, message)

Logs a message at the specified level.

Parameters:
  • level (str) –

    The level at which to log the message. Can be 'debug', 'info', 'warning', 'error', or 'critical'.

  • message (str) –

    The message to log.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def log(self, level, message):
    """
    Logs a message at the specified level.

    Parameters:
        level (str): The level at which to log the message. Can be 'debug', 'info', 'warning', 'error', or 'critical'.
        message (str): The message to log.

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages.

    Returns:
        None
    """
    if self.logger.handlers:
        if level == 'debug':
            self.logger.debug(message)
        elif level == 'info':
            self.logger.info(message)
        elif level == 'warning':
            self.logger.warning(message)
        elif level == 'error':
            self.logger.error(message)
        elif level == 'critical':
            self.logger.critical(message)

enable_logging

enable_logging(log_to_file=True, log_to_screen=True)

Enables logging by configuring the logger with the appropriate handlers.

Parameters:
  • log_to_file (bool, default: True ) –

    Whether to log messages to a file. Defaults to True.

  • log_to_screen (bool, default: True ) –

    Whether to log messages to the screen. Defaults to True.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

  • log_to_file (bool) –

    Flag indicating whether to log to a file.

  • log_to_screen (bool) –

    Flag indicating whether to log to the screen.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def enable_logging(self,log_to_file=True,log_to_screen=True):
    """
    Enables logging by configuring the logger with the appropriate handlers.

    Parameters:
        log_to_file (bool): Whether to log messages to a file. Defaults to True.
        log_to_screen (bool): Whether to log messages to the screen. Defaults to True.

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages.
        log_to_file (bool): Flag indicating whether to log to a file.
        log_to_screen (bool): Flag indicating whether to log to the screen.

    Returns:
        None
    """
    logging.disable(logging.NOTSET)
    self.log_to_file = log_to_file
    self.log_to_screen = log_to_screen
    self.configure_logger()

disable_logging

disable_logging()

Disables logging by clearing all handlers from the logger.

Attributes:
  • logger (Logger) –

    The logger instance used for logging messages.

Returns:
  • None

Source code in src/SupplyNetPy/Components/logger.py
def disable_logging(self):
    """
    Disables logging by clearing all handlers from the logger.

    Parameters:
        None

    Attributes:
        logger (logging.Logger): The logger instance used for logging messages. 

    Returns:
        None
    """
    logging.disable(logging.CRITICAL)
    self.logger.handlers = []