log_manager module

Python dual-logging setup (console and log file).

It supports different log levels and colorized output.

Created by Fonic <https://github.com/fonic> Date: 04/05/20 - 02/07/23

Based on: https://stackoverflow.com/a/13733863/1976617 https://uran198.github.io/en/python/2016/07/12/colorful-python-logging.html https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

_get_package_version(package_name)[source]

Get the package version.

Parameters:

package_name (str)

Return type:

str

_get_last_commit_hash()[source]

Get the last Git commit hash. :rtype: str

Todo

Will look for the script commit number, not for the code commit number!

_log_header(package_name)[source]

Create a header for the log file.

Parameters:

package_name (str)

Return type:

str

_console_handler(output, level, color, line_template)[source]

Set up the console handler.

Parameters:
Return type:

Handler

_file_handler(file, level, color, line_template, mode='w')[source]

Set up the file handler.

Parameters:
Return type:

Union[Handler, Literal[False]]

class LogFormatter(color, *args, **kwargs)[source]

Bases: Formatter

Logging formatter supporting colorized output.

Parameters:

color (bool)

COLOR_CODES = {   10: '\x1b[1;30m',     20: '\x1b[0;37m',     30: '\x1b[1;33m',     40: '\x1b[1;31m',     50: '\x1b[1;35m'}
RESET_CODE = '\x1b[0m'
__init__(color, *args, **kwargs)[source]

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 or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

Parameters:

color (bool)

format(record, *args, **kwargs)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

Parameters:

record (LogRecord)

Return type:

str

set_up_logging(package_name, console_log_output='stdout', console_log_level='INFO', console_log_color=True, console_log_line_template='%(color_on)s[%(levelname)-8s] [%(filename)-20s]%(color_off)s %(message)s', logfile_file=PosixPath('simultipac.log'), logfile_log_level='INFO', logfile_log_color=False, logfile_line_template=('%(color_on)s[%(asctime)s] [%(levelname)-8s] [%(filename)-20s]%(color_off)s '
set_up_logging '%(message)s'), logfile_mode='w')

Set up logging with both console and file handlers.

Parameters:
  • package_name (str)

  • console_log_output (str, default: 'stdout')

  • console_log_level (str, default: 'INFO')

  • console_log_color (bool, default: True)

  • console_log_line_template (str, default: '%(color_on)s[%(levelname)-8s] [%(filename)-20s]%(color_off)s %(message)s')

  • logfile_file (Path, default: PosixPath('simultipac.log'))

  • logfile_log_level (str, default: 'INFO')

  • logfile_log_color (bool, default: False)

  • logfile_line_template (str, default: '%(color_on)s[%(asctime)s] [%(levelname)-8s] [%(filename)-20s]%(color_off)s %(message)s')

  • logfile_mode (Literal['a', 'w'], default: 'w')

Return type:

bool

main()[source]

Main function.