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 or sys.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.
format(record)

Formats a record for output.

This implementation colorizes the message line, but leaves any traceback unolorized.

is_tty

Returns true if the handler’s stream is a terminal.

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.