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)

Get the package version.

Return type:

str

_get_last_commit_hash()

Get the last Git commit hash.

Todo

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

Return type:

str

_log_header(package_name)

Create a header for the log file.

Return type:

str

_console_handler(output, level, color, line_template)

Set up the console handler.

Return type:

Handler

_file_handler(file, level, color, line_template)

Set up the file handler.

Return type:

Handler | Literal[False]

class LogFormatter(color, *args, **kwargs)

Bases: Formatter

Logging formatter supporting colorized output.

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.

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'
format(record, *args, **kwargs)

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.

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('eemilib.log'), logfile_log_level='INFO', logfile_log_color=False, logfile_line_template='%(color_on)s[%(asctime)s] [%(levelname)-8s] [%(filename)-20s]%(color_off)s %(message)s')

Set up logging with both console and file handlers.

Return type:

bool

main()

Main function.