csbot package

Module contents

class csbot.PrettyStreamHandler(stream=None, colour=None)[source]

Bases: logging.StreamHandler

Wrap log messages with severity-dependent ANSI terminal colours.

Use in place of logging.StreamHandler to have log messages coloured according to severity.

>>> handler = PrettyStreamHandler()
>>> handler.setFormatter(logging.Formatter('[%(levelname)-8s] %(message)s'))
>>> logging.getLogger('').addHandler(handler)

stream corresponds to the same argument to logging.StreamHandler, defaulting to stderr.

colour overrides TTY detection to force colour on or off.

This source for this class is released into the public domain.

Code author: Alan Briolat <alan.briolat@gmail.com>

COLOURS = {10: '\x1b[36m', 30: '\x1b[33m', 40: '\x1b[31m', 50: '\x1b[31;7m'}

Mapping from logging levels to ANSI colours.

COLOUR_END = '\x1b[0m'

ANSI code for resetting the terminal to default colour.


Get a coloured, formatted message for a log record.

Calls logging.StreamHandler.format() and applies a colour to the message if appropriate.