Skip to main content

Colored stream handler for the logging module

Project description

The coloredlogs.ColoredStreamHandler class is a simple logging handler that inherits from logging.StreamHandler 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. Currently this module does not support non-UNIX terminals (e.g. the Windows console). Here is a screenshot of the demo that is printed when coloredlogs.py is executed directly:

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

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

The logging handler does not use ANSI escape sequences when output redirection applies (for example when the standard error stream is being redirected to a file or another program) so if you like the format (see below), you can use it for your log files as well.

Format of log messages

As can be seen in the screenshot above, the logging handler includes four fields in every logged message by default:

  1. A timestamp indicating when the event was logged. This field is visible by default. To hide it you can pass the keyword argument show_timestamps=False when you create the handler.

  2. The hostname of the system on which the event was logged. This field is visible by default. To hide it you can pass the keyword argument show_hostname=False when you create the handler.

  3. The name of the logger that logged the event. This field is hidden by default. To show it you can pass the keyword argument show_name=True when you create the handler.

  4. The human friendly name of the log level / severity.

  5. The message that was logged.

Usage

Here’s an example of how you would use the logging handler:

# Configure your logger.
import logging, coloredlogs
logger = logging.getLogger('your-module')
logger.addHandler(coloredlogs.ColoredStreamHandler())

# Some examples.
logger.setLevel(logging.DEBUG)
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.fatal("this is a fatal message")
logger.critical("this is a critical message")

For people who like Vim

Although the logging handler was originally meant for interactive use, it can also be used to generate log files. In this case the ANSI escape sequences are not used so the log file will contain plain text and no colors. If you use Vim and coloredlogs and would like to view your log files in color, you can try the two Vim scripts included in the coloredlogs source distributions and git repository:

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

Contact

The latest version of coloredlogs is available on PyPi and GitHub. 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.

© 2013 Peter Odding.

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

coloredlogs-0.2.tar.gz (5.9 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for coloredlogs-0.2.tar.gz
Algorithm Hash digest
SHA256 d9e4b434dd38c1423d2fe23c687d69ed576cc3f8181c761d6b2e9ebe18d5d1f1
MD5 c8ef389aae5a5e39792f92cb213b2913
BLAKE2b-256 7a598cf520283da411a951e00e8b964a3b99c333ca9710d5c7c38132138d758e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page