logging.Formatter allowing for customized and colored logging output.
pip install styled-log==1.0
styled-log
: logging.Formatter subclass that allows for custom formatted and styled logging.By default, powered by Colorama.
Allows for both ansi-styling capabilities for logger attributes, AND ansi-styling for particular values of that attribute.
https://gitlab.com/ittVannak/styled-log/blob/master/styled-log.gif
import logging
# don't call basicConfig
logger = logging.getLogger('test_logger')
styled_formatter = StyledFormatter()
writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)
logger.addHandler(writes_to_console)
import logging
# don't call basicConfig
logger = logging.getLogger('test_logger')
custom_map = OrderedDict()
custom_map['%(asctime)s'] = ('blue_fore',)
custom_map['%(module)s'] = ('yellow_fore',)
custom_map['%(lineno)s'] = ('red_fore',)
custom_map['%(message)s'] = None
custom_map['%(levelname)s'] = {
'DEBUG' : ('cyan_fore',),
'INFO' : ('blue_fore',),
'ERROR' : ('red_fore', 'bright_style'),
'WARNING' : ('red_fore', 'bright_style'),
'CRITICAL': ('red_fore', 'bright_style')
}
styled_formatter = StyledFormatter(custom_map)
writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)
logger.addHandler(writes_to_console)
logger.info('hey there!')
2019-03-24 10:35:59,091 ; <ipython-input-2-7091e3083bda> ; 190 ; hey there! ; INFO
import logging
# don't call basicConfig
logger = logging.getLogger('test_logger')
custom_ansi_map = {
'red': '\x1b[31m',
'blue':'\x1b[34m',
'green':'\x1b[32m'
}
load_ansi_map(custom_ansi_map)
# now every subsequent styling string we provide must be either 'red','blue', or 'green'
custom_map = OrderedDict()
custom_map['%(asctime)s'] = ('blue',)
custom_map['%(module)s'] = ('red',)
custom_map['%(lineno)s'] = ('red',)
custom_map['%(message)s'] = None
custom_map['%(levelname)s'] = {
'DEBUG' : ('green', ),
'INFO' : ('blue', ),
'ERROR' : ('red', ),
'WARNING' : ('red', ),
'CRITICAL': ('red', )
}
styled_formatter = StyledFormatter(custom_map)
writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)
logger.addHandler(writes_to_console)
logger.info('hey there!')
2019-03-24 10:35:59,091 ; <ipython-input-2-7091e3083bda> ; 190 ; hey there! ; INFO
There are many ways this could be implemented, but my primary goals were:
Knowing that, here are some basic concepts in this library:
global ansi map: dictionary of human-readable names to ansi style codes.
load_ansi_map: function that sets global ansi map value. Allows user to supply their own human-readable names to ansi style codes.
styledFormatter: subclass of logging.Formatter, allowing for styled bylines for each logging message. Styling arguments for this class must align with the human-readable names in the global ansi map.
style: function that actually references the global ansi map and applies the ansi code.
show_ansi_map: function that shows the effects of the different ansi codes on a string.
%()d/f
attributes as normal, we are string formatting, so they will come back as strings, though I don't expect that to be an issue.Why tuples?
style(text="ALERT", ("red_fore", "bright_style"))
Why a global dictionary?
load_ansi_map(custom_ansi_map)
low_intensity: some_ansi, mid_intensity:another_ansi, high_intensity: a_third_ansi
and then being able to switch those values on the fly.__slots__
.