Colored terminal output for Python's logging module
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
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:
Note that the screenshot above includes the custom logging level
defined by my verboselogs package: if you install both
verboselogs it will Just Work (
verboselogs is of course not required to use
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 DEBUG message with level 'debug' 2015-10-23 03:32:23 peter-macbook coloredlogs.demo VERBOSE message with level 'verbose' 2015-10-23 03:32:24 peter-macbook coloredlogs.demo 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.
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")
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
that it’s not attached to an interactive terminal. If you’d like to have colors
e-mailed to you by cron there are two ways to make it happen:
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.
Here’s an example of a minimal crontab:
MAILTO="your-email-address@here" CONTENT_TYPE="text/html" * * * * * root coloredlogs --to-html your-command
The coloredlogs program is installed when you install the
Python 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 :-)
The ColoredCronMailer class provides a context manager that automatically enables HTML output when the $CONTENT_TYPE variable has been correctly set in the crontab.
This requires my capturer package which you can install using pip install 'coloredlogs[cron]'. The [cron] extra will pull in capturer 2.4 or newer which is required to capture the output while silencing it - otherwise you’d get duplicate output in the emails sent by cron.
The context manager can also be used to retroactively silence output that has already been produced, this can be useful to avoid spammy cron jobs that have nothing useful to do but still email their output to the system administrator every few minutes :-).
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 firstname.lastname@example.org.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|coloredlogs-7.0-py2.py3-none-any.whl (36.0 kB) Copy SHA256 Checksum SHA256||py2.py3||Wheel||May 18, 2017|
|coloredlogs-7.0.tar.gz (32.9 kB) Copy SHA256 Checksum SHA256||–||Source||May 18, 2017|