custom_logging
A module containing custom logging classes and functions, that are used in the PQAnalysis package.
Summary
Classes:
A custom color formatter class that extends the CustomFormatter class. |
|
A custom formatter class that extends the logging.Formatter class. |
|
A custom logger class that extends the logging.Logger class. |
Functions:
general setup for the logger |
Reference
- class CustomColorFormatter(
- fmt=None,
- datefmt=None,
- style='%',
- validate=True,
- *,
- defaults=None,
Bases:
CustomFormatter
A custom color formatter class that extends the CustomFormatter class.
This class extends the CustomFormatter class, so that for each log level a different color is used. The colors are defined in the FORMATS dictionary. The color is set to the log level of the log message.
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.- format_level(
- record: LogRecord,
Formats the level of the log message.
It formats the level of the log message with the color of the log level.
- Parameters:
record (logging.LogRecord) – the log record to format
- Returns:
the formatted level of the log message
- Return type:
str
- FORMATS = {'CUSTOM': '\x1b[36;1m%(levelname)s:\x1b[0m', 10: '\x1b[33;1m%(levelname)s:\x1b[0m', 20: '\x1b[32;1m%(levelname)s:\x1b[0m', 30: '\x1b[33;1m%(levelname)s:\x1b[0m', 40: '\x1b[31;20m%(levelname)s:\x1b[0m', 50: '\x1b[31;1m%(levelname)s:\x1b[0m'}
- bold_cyan = '\x1b[36;1m'
- bold_green = '\x1b[32;1m'
- bold_orange = '\x1b[33;1m'
- bold_red = '\x1b[31;1m'
- bold_yellow = '\x1b[33;1m'
- header = '%(levelname)s:'
- red = '\x1b[31;20m'
- reset = '\x1b[0m'
- class CustomFormatter(
- fmt=None,
- datefmt=None,
- style='%',
- validate=True,
- *,
- defaults=None,
Bases:
Formatter
A custom formatter class that extends the logging.Formatter class.
This class extends the logging.Formatter class and is used to format the log messages of the PQAnalysis package. It re-implements the format method of the logging.Formatter class to format the log messages in a custom way. The format method is used to format the log messages of the logger.
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.- format(
- record: LogRecord,
Formats the log message.
- Parameters:
record (logging.LogRecord) – The log record to format.
- Returns:
The formatted log message.
- Return type:
str
- format_level(
- record: LogRecord,
Formats the level of the log message.
- Parameters:
record (logging.LogRecord) – The log record to format.
- Returns:
The formatted level of the log message.
- Return type:
str
- level_keys = dict_keys(['CRITICAL', 'FATAL', 'ERROR', 'WARN', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'])
- longest_level = 'CRITICAL'
- class CustomLogger(name, level=0)[source]
Bases:
Logger
A custom logger class that extends the logging.Logger class.
This class extends the logging.Logger class and is set as the default logger class for the PQAnalysis package. It re-implements the _log method of the logging.Logger class in conjunction with the error and critical methods. For more details see the documentation of the _log, error and critical methods.
To return to the original logging.Logger class, the original_error and original_critical methods can be used.
Initialize the logger with a name and an optional level.
- critical(
- msg: Any,
- *args,
- exception: Exception | None = None,
- **kwargs,
This method logs the message with the logging.CRITICAL level and raises an exception if the logger is enabled for logging.DEBUG. If the logger is not enabled for logging.DEBUG, the program exits with code 1. If an exception is given, it is raised with the message of the log message otherwise a generic Exception is raised.
- Parameters:
msg (Any) – The message to log.
exception (Exception, optional) – The exception to raise, by default None. If None, a generic Exception is raised.
*args (Any) – The arguments of the log message.
**kwargs (Any) – The keyword arguments of the log message.
- error(
- msg: Any,
- *args,
- exception: Exception | None = None,
- **kwargs,
This method logs the message with the logging.ERROR level and raises an exception if the logger is enabled for logging.DEBUG. If the logger is not enabled for logging.DEBUG, the program exits with code 1. If an exception is given, it is raised with the message of the log message otherwise a generic Exception is raised.
In order to return to the original logging.Logger class, the original_error method can be used.
- Parameters:
msg (Any) – The message to log.
exception (Exception, optional) – The exception to raise, by default None. If None, a generic Exception is raised.
*args (Any) – The arguments of the log message.
**kwargs (Any) – The keyword arguments of the log message.
- original_critical(
- msg: Any,
- *args,
- **kwargs,
This method logs the message with the logging.CRITICAL level.
It corresponds to the original critical method of the logging.Logger class.
- Parameters:
msg (Any) – The message to log.
*args (Any) – The arguments of the log message.
**kwargs (Any) – The keyword arguments of the log message.
- original_error(
- msg: Any,
- *args,
- **kwargs,
This method logs the message with the logging.ERROR level.
It corresponds to the original error method of the logging.Logger class.
- Parameters:
msg (Any) – The message to log.
*args (Any) – The arguments of the log message.
**kwargs (Any) – The keyword arguments of the log message.
- setup_logger(
- logger: Logger,
general setup for the logger
This function can be used to setup a generic logger for functions/classes… of the PQAnalysis package. It automatically adds a stream handler to the logger and if the use_log_file is set to True, it also adds a file handler to the logger. The file handler writes the log messages to the file specified in the config log_file_name.
- Parameters:
logger (logging.Logger) – The logger to setup.
- Returns:
The logger that was setup.
- Return type:
logging.Logger