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: str) str

Get the package version.

_get_last_commit_hash() str

Get the last Git commit hash.

Todo

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

_log_header(package_name: str) str

Create a header for the log file.

_console_handler(output: str, level: str, color: bool, line_template: str) Handler

Set up the console handler.

_file_handler(file: Path, level: str, color: bool, line_template: str) Handler | Literal[False]

Set up the file handler.

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

Bases: Formatter

Logging formatter supporting colorized output.

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: bool, *args, **kwargs) None

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.

format(record: LogRecord, *args, **kwargs) str

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.

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

Set up logging with both console and file handlers.

main()

Main function.