Colourised logging¶
ANSI colourise the logging stream (works on LINUX/UNIX based systems).
Constants for colours:
attr const BLACK: | |
---|---|
Black | |
attr const RED: | Red |
attr const GREEN: | |
Green | |
attr const YELLOW: | |
Yellow | |
attr const BLUE: | |
Blue | |
attr const CYAN: | |
Cyan | |
attr const WHITE: | |
White |
Class documentation¶
-
class
otrbot.core.colourlog.
ColourFormatter
(*args, **kwargs)¶ ANSI colourise the logging stream (works on LINUX/UNIX based systems).
Initialise some variables for colourising.
Make cache dict for formats, backup original format string, initialise the parent log formatter.
Parameters: - args (tuple) – Positional arguments that should be passed to the parent formatter.
- kwargs (dict) – Keyword arguments that should be passed to the parent formatter. Two keywords are taken from this dict, see below.
- colours (dict) – A dictionary containing the colour schemes to be used by the logger, see below for more information.
- no_colour_nl (bool) – Tell the logger not to colour anything after a new line character.
There is a default colour scheme with 2 colour indexes. You can use these colourschemes as follows:
formatter = ColourFormatter( "$lvl[%(levelname)s]$reset $msg%(name)s %(message)s" )
Note the $lvl and $msg variables are used as template strings in the format string. Also note there is a $reset variable, use this before any change in colour, it is automatically added at the end of the string to prevent the terminal from printing coloured strings after the log line was printed.
You can also make custom colour schemes and pass them as a keyword argument (colours) when instantiating a ColourFormatter object.
The colours dictionary that can be passed to the
ColourFormatter
is formatted as follows.{ 'lvl': { logging.DEBUG: (WHITE, BLUE, False), logging.INFO: (BLACK, GREEN, False), logging.WARNING: (BLACK, YELLOW, False), logging.ERROR: (WHITE, RED, False), logging.CRITICAL: (YELLOW, RED, True), }, 'msg': { logging.DEBUG: (BLUE, None, False), logging.INFO: (GREEN, None, False), logging.WARNING: (YELLOW, None, False), logging.ERROR: (RED, None, False), logging.CRITICAL: (RED, None, True), } }
The dictionary contains indexes followed by the log levels, followed by a tuple in the form of foreground colour, background color, bold face.
The colourschemes above are the default colours, they colour the %(levelname)s in colour scheme lvl, which adds background colours as well as foreground colours. The rest of message is can be formatted using the msg scheme, which does not add any background colours but does add foreground colours.
The lvl and msg indexes specify colourschemes. You can make your own indexes, indexes can have arbitrary names but should be formatted be a-zA-Z0-9-_ and start with a-zA-Z. To make use of your colour scheme you need to change your format string. Like this:
import logging logger = logging.getLogger(__name__) logger.setLevel(level=logging.DEBUG) handler = logging.StreamHandler() handler.setFormatter( ColourFormatter( "$lvl[%(levelname)s]$reset $msg%(name)s %(message)s " "$reset" ) ) logger.addHandler(handler) logger.debug("This is detailed information..") logger.info("This somewhat more useful..") logger.warn("This might be dangerous..") logger.error("Something might have gone a bit wrong") logger.critical("Woah! do something!!")
\x1b[44;37m[DEBUG]\x1b[0m \x1b[34m__main__ This is detailed information..\x1b[0m \x1b[42;30m[INFO]\x1b[0m \x1b[32m__main__ This somewhat more useful..\x1b[0m \x1b[43;30m[WARNING]\x1b[0m \x1b[33m__main__ This might be dangerous..\x1b[0m \x1b[41;37m[ERROR]\x1b[0m \x1b[31m__main__ Something might have gone a bit wrong\x1b[0m \x1b[41;33;1m[CRITICAL]\x1b[0m \x1b[31;1m__main__ Woah! do something!!\x1b[0m