Log formatting with colors!
Project description
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.
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).
Arguments
ColoredFormatter takes several arguments:
format: The format string used to output the message (required).
datefmt: An optional date format passed to the base class. See logging.Formatter.
reset: Implicitly adds a color reset code to the message output, unless the output already ends with one. Defaults to True.
log_colors: A mapping of record level names to color names. The defaults can be found in colorlog.default_log_colors, or the below example.
style: Availible on Python 3.2 and above. See logging.Formatter.
Examples
The following code creates a ColoredFormatter for use in a logging setup, passing each arguments defaults to the constructor:
from colorlog import ColoredFormatter formatter = ColoredFormatter( "%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s", datefmt=None, reset=True, color_levels={ 'DEBUG': 'cyan', 'INFO': 'green', 'WARNING': 'yellow', 'ERROR': 'red', 'CRITICAL': 'red', } )
With dictConfig
logging.config.dictConfig({ 'formatters': { 'colored': { '()': 'colorlog.ColoredFormatter', 'format': "%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s" } }, ... })
A full example dictionary can be found in tests/test_colorlog.py.
With fileConfig
... [formatters] keys=color [formatter_color] class=colorlog.ColoredFormatter format=%(log_color)s%(levelname)-8s%(reset)s %(bg_blue)s[%(name)s]%(reset)s %(message)s from fileConfig datefmt=%m-%d %H:%M:%S ...
An instance of ColoredFormatter created with those arguments will then be used by any handlers that are configured to use the color formatter.
A full example configuration can be found in tests/test_config.ini.
Tests and Compatibility
Tests similar to those in the above examples can be found in tests/test_colorlog.py.
colorlog should work with both Python 2 and 3. Nose is used to test the code on both Python 2.7 and 3.2.
nosetests-2.7 --nocapture --nologcapture nosetests-3.2 --nocapture --nologcapture
Licence
colorlog is distributed under the MIT Licence.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file colorlog-1.4.tar.gz
.
File metadata
- Download URL: colorlog-1.4.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f9c35b41fb307a365e2b81d1a4944e606335ded4622ed6a4c6b496a07f94816 |
|
MD5 | bb5a016f7d0004ffc1c7463d25c15850 |
|
BLAKE2b-256 | f1caa685c6dfc25d3d2c432ce83aa4fe43cf2fecb4d6d87017f883ffa6ce3777 |