Skip to main content
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.

Release files for coloredlogs 6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for coloredlogs 6.3
File Size Uploaded
coloredlogs-6.3.tar.gz 30.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for coloredlogs 6.3
File Interpreter ABI Platform
coloredlogs-6.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 63.7 kB

Release files / coloredlogs-6.3.tar.gz

Download URL coloredlogs-6.3.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4e82f9eb8cf3011dbdbd6cb7d534f3ebbca43a0da83523eb74efd84517e21177
BLAKE2b-256 checksum
How to use checksums
3545805d372def74bae2521e6cbce9632c30b10d56c27a30f7c710a9c818fed5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / coloredlogs-6.3-py2.py3-none-any.whl

Download URL coloredlogs-6.3-py2.py3-none-any.whl
Size 33.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f7226cd1b0a81b3ff982b522b2987b93a5e4ddf8cf1cef0fafcbd302da9b6589
BLAKE2b-256 checksum
How to use checksums
f34854e205d27a2de49d2f0cece6269e66315074685c9332f56d9aa8ea2e073e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

15.0.1

2 release files

15.0

2 release files

14.3

2 release files

14.2

2 release files

14.1

2 release files

14.0

2 release files

12.0

2 release files

11.3

2 release files

11.2

2 release files

11.1

2 release files

11.0

2 release files

10.0

2 release files

9.3.1

2 release files

9.3

2 release files

9.2

2 release files

9.1

2 release files

9.0

2 release files

8.0

2 release files

7.3.3

2 release files

7.3.2

2 release files

7.3.1

2 release files

7.3

2 release files

7.2

2 release files

7.1

2 release files

7.0.1

2 release files

7.0

2 release files

6.4

2 release files

This release

6.3 This release

2 release files

6.2

2 release files

6.1

2 release files

6.0

2 release files

5.2

2 release files

5.1.1

2 release files

5.1

1 release file

5.0

1 release file

4.0

1 release file

3.5

1 release file

3.4

1 release file

3.3

1 release file

3.2

1 release file

3.1.4

1 release file

3.1.3

1 release file

3.1.2

1 release file

3.1.1

1 release file

3.1

1 release file

2.0

1 release file

1.0.1

1 release file

1.0

1 release file

0.8

1 release file

0.7.1

1 release file

0.6

1 release file

0.5

1 release file

0.4.9

1 release file

0.4.8

1 release file

0.4.7

1 release file

0.4.6

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page