FELiCS.Misc.logging#

Module Contents#

Classes#

CustomFormatter

Custom log formatter with colored output for different log levels.

Logger

Logger class for flexible and colored logging to file and console.

Functions#

FELiCS.Misc.logging.in_notebook()#
class FELiCS.Misc.logging.CustomFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

Bases: logging.Formatter

Custom log formatter with colored output for different log levels.

Provides colored formatting for log messages in the terminal, making it easier to distinguish between log levels such as DEBUG, INFO, WARNING, ERROR, and CRITICAL.

Initialize the CustomFormatter object

No parameters are required for initialization.

Variables:
  • grey (str) – ANSI escape code for grey color.

  • yellow (str) – ANSI escape code for yellow color.

  • red (str) – ANSI escape code for red color.

  • bold_red (str) – ANSI escape code for bold red color.

  • reset (str) – ANSI escape code to reset color.

  • format (str) – Log message format string.

  • FORMATS (dict) – Mapping of log levels to their respective colored format strings.

grey = '\x1b[38;20m'#
yellow = '\x1b[33;20m'#
red = '\x1b[31;20m'#
bold_red = '\x1b[31;1m'#
reset = '\x1b[0m'#
format_str = '%(levelname)-8s | %(filename)-22s | %(funcName)-26s (line %(lineno)-4s) : %(message)s'#
FORMATS#
format(record)#

Format the specified log record as text with color based on log level.

Applies color formatting to the log message depending on the log level.

Parameters:

record (logging.LogRecord) – The log record to be formatted.

Returns:

The formatted log message string.

Return type:

str

class FELiCS.Misc.logging.Logger(debug_mode=False, test_mode=False, logger_name='log')#

Logger class for flexible and colored logging to file and console.

Provides a singleton logger with support for colored console output, file logging, dynamic log file location changes, and debug/test modes.

Initialize the Logger object

Parameters:
  • debug_mode (bool, optional) – If True, enables debug logging (default is False).

  • test_mode (bool, optional) – If True, enables test mode logging (default is False).

  • logger_name (str, optional) – Name of the logger and log file prefix (default is “log”).

Variables:
  • debug_mode (bool) – Indicates if debug mode is enabled.

  • test_mode (bool) – Indicates if test mode is enabled.

  • logger_name (str) – Name of the logger.

  • _logger (logging.Logger) – The underlying Python logger instance.

property logger#

Get the underlying logger instance.

Returns:

The configured logger instance.

Return type:

logging.Logger

Raises:

RuntimeError – If the logger has not been properly initialized.

classmethod get_logger(name=None)#

Retrieve the singleton logger instance.

If the logger has not been initialized, creates a new instance with the specified name.

Parameters:

name (str, optional) – Name for the logger if it needs to be created.

Returns:

The singleton logger instance.

Return type:

logging.Logger

Raises:

RuntimeError – If the logger is not initialized and no name is provided.

classmethod change_log_location(new_log_path)#

Move log files to a new directory and update file handlers.

Moves all current log files to the specified new directory and updates the logger’s file handlers to write to the new location.

Parameters:

new_log_path (str) – The new directory path for log files.

Raises:

RuntimeError – If the logger is not initialized.

close_logger(logger)#

Remove all handlers from the given logger.

Clears all handlers from the specified logger instance.

Parameters:

logger (logging.Logger) – The logger instance to close.