Skip to main content

Log formatting with colors!

Project description

Latest PyPI version https://travis-ci.org/borntyping/colorlog.png

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.

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: Available on Python 3.2 and above. See logging.Formatter.

Examples

Example output

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,
        log_colors={
                '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.

Compatibility

colorlog works on Python 2.6 and above, including Python 3.

On Windows, requires colorama to work properly. A dependancy on colorama is included as an optional package dependancy - depending on colorlog[windows] instead of colorlog will ensure it is included when installing.

Tests

Tests similar to the above examples are found in tests/test_colorlog.py. They require colorlog to be installed or otherwise available to Python.

tox will run the tests under all compatible python versions.

Licence

Copyright (c) 2012 Sam Clements <sam@borntyping.co.uk>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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-2.3.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

colorlog-2.3.1-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for colorlog-2.3.1.tar.gz
Algorithm Hash digest
SHA256 fe2361d0eb5551695498aaa85b008fe4487fedf749326ac0eaae625786ee2932
MD5 b1dd218d984aa922afe2079f523588c2
BLAKE2b-256 99af6276ca6fd3dca50c25d82d0919eca46671177ecad2a507c03566998c3f1c

See more details on using hashes here.

File details

Details for the file colorlog-2.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for colorlog-2.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9471f3e1e4fb57b9266fcb9a50f26675a11011f6f94cabd8d98570f6043767c4
MD5 b365c82dcee0fe242b54221446cebed5
BLAKE2b-256 86ea6539194b9ef78b31503fff409eb26364072f0888bff1e9f3e79d43167bf2

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