openc2lib.utils.log_formatting

Formatting log messages

This modules provides a formatter for the logging framework.

The following generic format is provided: [] : where the is colorized to give prominence to more critical messages. All fields can be optionally omitted.

This module requires 256-colors terminals.

 1""" Formatting log messages
 2
 3	This modules provides a formatter for the `logging` framework.
 4	
 5	The following generic format is provided:
 6	<datetime> [<level>] <modulename>: <message>
 7	where the <level> is colorized to give prominence to more critical messages. 
 8	All fields can be optionally omitted.
 9
10	This module requires 256-colors terminals.
11"""
12
13import logging
14
15class LogFormatter(logging.Formatter):
16	""" Colored logging formatter
17
18		The colormap is fixed, but the user can select which fields to include:
19		- datetime: date and time of the log
20		- module name: the name of the module that generated the log
21		The level and message are always included and cannot be omitted.
22	"""
23
24	grey = '\x1b[38;21m'
25	green = '\x1b[38;5;42m'
26	blue = '\x1b[38;5;39m'
27	yellow = '\x1b[38;5;226m'
28	red = '\x1b[38;5;196m'
29	highlight_red = '\x1b[37;1m\x1b[41m'
30	reset = '\x1b[0m'
31
32	def __init__(self, datetime= True, name=True):
33		""" Set the custom format
34
35			  Select which optional fields will be included.
36			  :param datetime: Set to `False` to disable date/time indication.
37			  :param name: Set to `False` to diable module name indication.
38		"""
39		super().__init__()
40		if datetime:
41			self.fmt = "{asctime:s} [COLOR{levelname:^8s}" + self.reset + "]"
42		else:
43			self.fmt = "COLOR{levelname:8s}" + self.reset + ":"
44		if name:
45			self.fmt += "{name:s}:"
46		self.fmt += " {message:s}"
47
48		self.COLORS = {
49		    logging.DEBUG: self.grey,
50		    logging.INFO: self.green,
51		    logging.WARNING: self.yellow,
52		    logging.ERROR: self.red,
53		    logging.CRITICAL: self.highlight_red
54		}
55
56	def format(self, record):
57		""" Color the record according to the log level """
58		log_fmt = self.fmt.replace('COLOR', self.COLORS[record.levelno])
59		formatter = logging.Formatter(log_fmt, style='{')
60		return formatter.format(record)
class LogFormatter(logging.Formatter):
16class LogFormatter(logging.Formatter):
17	""" Colored logging formatter
18
19		The colormap is fixed, but the user can select which fields to include:
20		- datetime: date and time of the log
21		- module name: the name of the module that generated the log
22		The level and message are always included and cannot be omitted.
23	"""
24
25	grey = '\x1b[38;21m'
26	green = '\x1b[38;5;42m'
27	blue = '\x1b[38;5;39m'
28	yellow = '\x1b[38;5;226m'
29	red = '\x1b[38;5;196m'
30	highlight_red = '\x1b[37;1m\x1b[41m'
31	reset = '\x1b[0m'
32
33	def __init__(self, datetime= True, name=True):
34		""" Set the custom format
35
36			  Select which optional fields will be included.
37			  :param datetime: Set to `False` to disable date/time indication.
38			  :param name: Set to `False` to diable module name indication.
39		"""
40		super().__init__()
41		if datetime:
42			self.fmt = "{asctime:s} [COLOR{levelname:^8s}" + self.reset + "]"
43		else:
44			self.fmt = "COLOR{levelname:8s}" + self.reset + ":"
45		if name:
46			self.fmt += "{name:s}:"
47		self.fmt += " {message:s}"
48
49		self.COLORS = {
50		    logging.DEBUG: self.grey,
51		    logging.INFO: self.green,
52		    logging.WARNING: self.yellow,
53		    logging.ERROR: self.red,
54		    logging.CRITICAL: self.highlight_red
55		}
56
57	def format(self, record):
58		""" Color the record according to the log level """
59		log_fmt = self.fmt.replace('COLOR', self.COLORS[record.levelno])
60		formatter = logging.Formatter(log_fmt, style='{')
61		return formatter.format(record)

Colored logging formatter

The colormap is fixed, but the user can select which fields to include:

  • datetime: date and time of the log
  • module name: the name of the module that generated the log The level and message are always included and cannot be omitted.
LogFormatter(datetime=True, name=True)
33	def __init__(self, datetime= True, name=True):
34		""" Set the custom format
35
36			  Select which optional fields will be included.
37			  :param datetime: Set to `False` to disable date/time indication.
38			  :param name: Set to `False` to diable module name indication.
39		"""
40		super().__init__()
41		if datetime:
42			self.fmt = "{asctime:s} [COLOR{levelname:^8s}" + self.reset + "]"
43		else:
44			self.fmt = "COLOR{levelname:8s}" + self.reset + ":"
45		if name:
46			self.fmt += "{name:s}:"
47		self.fmt += " {message:s}"
48
49		self.COLORS = {
50		    logging.DEBUG: self.grey,
51		    logging.INFO: self.green,
52		    logging.WARNING: self.yellow,
53		    logging.ERROR: self.red,
54		    logging.CRITICAL: self.highlight_red
55		}

Set the custom format

Select which optional fields will be included.

Parameters
  • datetime: Set to False to disable date/time indication.
  • name: Set to False to diable module name indication.
grey = '\x1b[38;21m'
green = '\x1b[38;5;42m'
blue = '\x1b[38;5;39m'
yellow = '\x1b[38;5;226m'
red = '\x1b[38;5;196m'
highlight_red = '\x1b[37;1m\x1b[41m'
reset = '\x1b[0m'
COLORS
def format(self, record):
57	def format(self, record):
58		""" Color the record according to the log level """
59		log_fmt = self.fmt.replace('COLOR', self.COLORS[record.levelno])
60		formatter = logging.Formatter(log_fmt, style='{')
61		return formatter.format(record)

Color the record according to the log level

Inherited Members
logging.Formatter
converter
datefmt
default_time_format
default_msec_format
formatTime
formatException
usesTime
formatMessage
formatStack