util.log_manager module
Python dual-logging setup (console and log file).
It supportd 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
Todo
Modernize this, with type hint etc.
- class LogFormatter(color, *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, *args, **kwargs)
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, *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.
- main()
Main function.
- set_up_logging(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('lightwin.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.