Colorizing Console Streams¶
ColorizingStreamHandler
is a handler which allows colorizing of console
streams, described here in more detail.
-
class
logutils.colorize.
ColorizingStreamHandler
(stream=None)¶ A stream handler which supports colorizing of console streams under Windows, Linux and Mac OS X.
Parameters: strm – The stream to colorize - typically sys.stdout
orsys.stderr
.-
colorize
(message, record)¶ Colorize a message for a logging event.
This implementation uses the
level_map
class attribute to map the LogRecord’s level to a colour/intensity setting, which is then applied to the whole message.Parameters: - message – The message to colorize.
- record – The
LogRecord
for the message.
-
emit
(record)¶ Emit a record.
If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If exception information is present, it is formatted using traceback.print_exception and appended to the stream. If the stream has an ‘encoding’ attribute, it is used to determine how to do the output to the stream.
-
format
(record)¶ Formats a record for output.
This implementation colorizes the message line, but leaves any traceback unolorized.
-
property
is_tty
¶ Returns true if the handler’s stream is a terminal.
-
level_map
= {10: (None, 'blue', False), 20: (None, 'black', False), 30: (None, 'yellow', False), 40: (None, 'red', False), 50: ('red', 'white', True)}¶ Maps levels to colour/intensity settings.
-
output_colorized
(message)¶ Output a colorized message.
On Linux and Mac OS X, this method just writes the already-colorized message to the stream, since on these platforms console streams accept ANSI escape sequences for colorization. On Windows, this handler implements a subset of ANSI escape sequence handling by parsing the message, extracting the sequences and making Win32 API calls to colorize the output.
Parameters: message – The message to colorize and output.
-