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

To apply color to a formatted argument in a log use {} as a placeholder for arguments. ChromaFormatter will substitute {} with any arguments passed in.

log.info('Format {}.', 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-2.0.0.tar.gz (5.4 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: chromaformatter-2.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1

File hashes

Hashes for chromaformatter-2.0.0.tar.gz
Algorithm Hash digest
SHA256 e73913ff4042fc6066abb35498fdf0e3ea1c1967b2a15f15502c327d2467ae99
MD5 a9fb8d5a9401eefd3f743dbef947487c
BLAKE2b-256 68eec664281d7beafa032e1183285c2370d2e4ec2e8b99d94e96b87b9ae155ac

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