Skip to main content

Wrapper for the Python logging module to add color.

Project description

Chroma Formatter

An extended Python logging formatter that adds color.

Demo

Installation

Chroma Formatter is on PyPI and can be installed with:

pip install chromaformatter

Usage

Chroma Formatter adds two features to the default logging formatter, colors can be added to the log format string, and formatted arguments in a log message can be colored. The syntax to add colors in the format string is $COLOR_NAME_HERE to add a color. $LEVEL refers to the color of the logging level for a log.

log_format = ('$GREEN[%(asctime)-s]'
              '$LEVEL[%(levelname)-s]'
              '$MAGENTA[%(filename)-s:%(lineno)-d]'
              '$LEVEL: %(message)s')

To use, we use a chromaformatter.ChromaFormatter rather than the logging.Formatter.

import sys
import logging
from chromaformatter import ChromaFormatter

log = logging.getLogger()
log_format = ('$GREEN[%(asctime)-s]'
              '$LEVEL[%(levelname)-s]'
              '$MAGENTA[%(filename)-s:%(lineno)-d]'
              '$LEVEL: %(message)s')
formatter = ChromaFormatter(log_format)
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
log.addHandler(handler)

All supported colors:

Regular Light
$BLACK $LI_BLACK
$RED $LI_RED
$GREEN $LI_GREEN
$YELLOW $LI_YELLOW
$BLUE $LI_BLUE
$MAGENTA $LI_MAGENTA
$CYAN $LI_CYAN
$WHITE $LI_WHITE

Additionally $BOLD applies bold text and $RESET resets back to no colors unless use_bold is True, then it resets to bold text.

Formatted Arguments in a Log

By inserting a %s placeholder in your log message and passing the value in as a parameter, colors will be added.

log.info('Format %s.', 10)

Additional Configuration

ChromaFormatter has a dict called color_map to determine the colors of parts of the log msg that can't be configured from the format string passed into ChromaFormatter. Logging levels, and the color of formatted arguments are set in color_map.

By default, the colors are:

Category Color
DEBUG BLUE
INFO Cyan
WARNING YELLOW
ERROR LIGHTRED_EX
CRITICAL RED
ARGS White

To change color_map colors use colorama:

formatter.color_map[chromaformatter.INFO] = colorama.Fore.WHITE
formatter.color_map[chromaformatter.ARGS] = colorama.Fore.MAGENTA

Applying to Existing Loggers

If you are using a third party module that uses the standard python logging module you can apply a ChromaFormatter as such:

import sys
import logging

from chromaformatter import ChromaFormatter

log_format = ('$GREEN[%(asctime)-0s]'
              '$LEVEL[%(levelname)-5s]'
              '$MAGENTA[%(filename)-21s:'
              '%(lineno)-3d]'
              '$LEVEL: %(message)s')
stream_formatter = ChromaFormatter(log_format)
stream_handler = logging.StreamHandler(stream=sys.stdout)

flask_logger = logging.getLogger('werkzeug')
while flask_logger.handlers:
    flask_logger.removeHandler(flask_logger.handlers.pop())
flask_logger.addHandler(stream_handler)

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

chromaformatter-4.0.0.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file chromaformatter-4.0.0.tar.gz.

File metadata

  • Download URL: chromaformatter-4.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.23.0 setuptools/49.3.1 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.6

File hashes

Hashes for chromaformatter-4.0.0.tar.gz
Algorithm Hash digest
SHA256 550c4676aee334dc20baa5788b34256c1f5beccfab8c05bddad61811cf287502
MD5 270843521395f506fe304ee69601b168
BLAKE2b-256 bc3cfdc8652b62ffedee14d28cb9ca76cec8e1c01d47a720be689eb1fd33438c

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