Skip to main content

Log formatting with colors!

Project description

colorlog.ColoredFormatter is a formatter for use with pythons logging module.

It allows colors to be placed in the format string, which is mostly useful when paired with a StreamHandler that is outputting to a terminal. This is accomplished by added a set of terminal color codes to the record before it is used to format the string.

Usage

ColoredFormatter requires at minumum a format string, and takes two options - reset (implictly add a reset code at the end of message strings, defaults to true) and color_levels (a mapping of record level names to color names, defaults to colorlog.DEFAULT_COLOR_LEVELS).

from colorlog import ColoredFormatter

formatstring = "%(bg_level)s%(levelname)-8s%(reset)s %(blue)%(message)s"

levels = {
        'DEBUG':    'cyan',
        'INFO':     'green',
        'WARNING':  'yellow',
        'ERROR':    'red',
        'CRITICAL': 'red',
}

formatter = ColoredFormatter(formatstring, reset=True, color_levels=levels)

The formatter can then be used in a normal logging setup.

Codes

The following values are made availible for use in the format string:

  • fg_{colorname}, bg_{colorname}: Foreground and background colors. The colors names are black, red, green, yellow, blue, purple, cyan and white.

  • bold: Bold output.

  • reset: Clear all formatting (both foreground and background colors).

  • log_color: Return the color associated with the records level (from color_levels).

Compatibility

colorlog should work with both Python 2 and 3. At a minimum, it’s been tested on Python 2.7 and 3.2.

Licence

colorlog is distributed under the MIT Licence.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colorlog-1.3.tar.gz (3.7 kB view details)

Uploaded Source

File details

Details for the file colorlog-1.3.tar.gz.

File metadata

  • Download URL: colorlog-1.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for colorlog-1.3.tar.gz
Algorithm Hash digest
SHA256 7b9d46e249a26ad06350ac019a1623749f822677812172139334efbe58a13774
MD5 4c241f809f72b8af81edb29e3ec2a257
BLAKE2b-256 3d6f028645b3d7b212ed1a3ac4fa85458f4b34904e1e6d804ebce9faf7804bd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page