Skip to main content

Colored terminal output for Python's logging module

Project description

https://travis-ci.org/xolox/python-coloredlogs.svg?branch=master https://coveralls.io/repos/xolox/python-coloredlogs/badge.png?branch=master

The coloredlogs package enables colored terminal output for Python’s logging module. The ColoredFormatter class inherits from logging.Formatter and uses ANSI escape sequences to render your logging messages in color. It uses only standard colors so it should work on any UNIX terminal. It’s currently tested on Python 2.6, 2.7, 3.4, 3.5, 3.6 and PyPy. On Windows coloredlogs automatically pulls in Colorama as a dependency and enables ANSI escape sequence translation using Colorama. Here is a screen shot of the demo that is printed when the command coloredlogs --demo is executed:

https://peterodding.com/code/python/coloredlogs/screenshots/terminal.png

Note that the screenshot above includes the custom logging level VERBOSE defined by my verboselogs package: if you install both coloredlogs and verboselogs it will Just Work (verboselogs is of course not required to use coloredlogs).

Format of log messages

The ColoredFormatter class supports user defined log formats so you can use any log format you like. The default log format is as follows:

%(asctime)s %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s

This log format results in the following output:

2015-10-23 03:32:22 peter-macbook coloredlogs.demo[30462] DEBUG message with level 'debug'
2015-10-23 03:32:23 peter-macbook coloredlogs.demo[30462] VERBOSE message with level 'verbose'
2015-10-23 03:32:24 peter-macbook coloredlogs.demo[30462] INFO message with level 'info'
...

You can customize the log format and styling using environment variables as well as programmatically, please refer to the online documentation for details.

Usage

Here’s an example of how easy it is to get started:

import coloredlogs, logging

# Create a logger object.
logger = logging.getLogger('your-module')

# By default the install() function installs a handler on the root logger,
# this means that log messages from your code and log messages from the
# libraries that you use will all show up on the terminal.
coloredlogs.install(level='DEBUG')

# If you don't want to see log messages from libraries, you can pass a
# specific logger object to the install() function. In this case only log
# messages originating from that logger will show up on the terminal.
coloredlogs.install(level='DEBUG', logger=logger)

# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warn("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

Colored output from cron

When coloredlogs is used in a cron job, the output that’s e-mailed to you by cron won’t contain any ANSI escape sequences because coloredlogs realizes that it’s not attached to an interactive terminal. If you’d like to have colors e-mailed to you by cron there’s a simple way to set it up:

MAILTO="your-email-address@here"
CONTENT_TYPE="text/html"
* * * * * root coloredlogs --to-html your-command

The coloredlogs program is installed when you install the coloredlogs package. When you execute coloredlogs --to-html your-command it runs your-command under the external program script (you need to have this installed). This makes your-command think that it’s attached to an interactive terminal which means it will output ANSI escape sequences which will then be converted to HTML by the coloredlogs program. Yes, this is a bit convoluted, but it works great :-)

You can use this feature without using coloredlogs in your Python modules, but please note that only normal text, bold text and text with one of the foreground colors black, red, green, yellow, blue, magenta, cyan and white (these are the portable ANSI color codes) are supported.

Contact

The latest version of coloredlogs is available on PyPI and GitHub. The online documentation is available on Read The Docs. For bug reports please create an issue on GitHub. If you have questions, suggestions, etc. feel free to send me an e-mail at peter@peterodding.com.

License

This software is licensed under the MIT license.

© 2017 Peter Odding.

Download files

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

Source Distribution

coloredlogs-6.4.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

coloredlogs-6.4-py2.py3-none-any.whl (33.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file coloredlogs-6.4.tar.gz.

File metadata

  • Download URL: coloredlogs-6.4.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for coloredlogs-6.4.tar.gz
Algorithm Hash digest
SHA256 3507b43c6933ba84424d9a7f1a1f209f421dbe3b998b0551d5c2a85d18560464
MD5 d39b85096702f163c0f642b943e185e2
BLAKE2b-256 4043dcc4adfd21994f2a30e8bebe467b89c28eba46fcdf282a6b8bde2ef60f1c

See more details on using hashes here.

File details

Details for the file coloredlogs-6.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for coloredlogs-6.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 776a53d04ca0199e0810bfeb09962e67b2232b1f084502d805d2f3401f886732
MD5 70d5a82e24359080b564425fa71d6b18
BLAKE2b-256 6e382bca3ee67ac3fb6802bc29b8210e73d4263cf068f08018add0913fbcaec5

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