Skip to main content

Wrapper for the Python logging formatter that adds color.

Project description

Chroma Formatter

License: MIT Code style: black PyPI version Code Coverage CodeFactor

Wrapper for 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. Colors can be inserted info the format string as such:

log_format = (
    f'{Colors.Fore.GREEN}%(asctime)-s '
    f'{Colors.LEVEL_COLOR}%(levelname).1s '
    f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '
    f'{Colors.LEVEL_COLOR}- %(message)s'
)

Then, use chromaformatter.ChromaFormatter rather than logging.Formatter.

import sys
import logging

from chromaformatter import ChromaFormatter, Colors

log = logging.getLogger()
log_format = (
    f'{Colors.Fore.GREEN}%(asctime)-s '
    f'{Colors.LEVEL_COLOR}%(levelname).1s '
    f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '
    f'{Colors.LEVEL_COLOR}- %(message)s'
)
formatter = ChromaFormatter(
    fmt=log_format,
    arg_start_color=Colors.Fore.WHITE,
    arg_end_color=Colors.LEVEL_COLOR
)
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
log.addHandler(handler)

Formatted Arguments in a Log

By setting arg_start_color for argument colors and arg_end_color for the rest of the string that comes after the argument, those colors will be applied to arguments.

log.info('This %s will be colored.', 'variable')

Additional Configuration

ChromaFormatter has a dict called color_map to determine the colors of each logging level.

By default, the colors are:

Category Color
NOTSET Fore.LIGHTBLUE_EX
DEBUG Fore.BLUE
INFO Fore.Cyan
WARNING Fore.YELLOW
ERROR Fore.LIGHTRED_EX
CRITICAL Fore.RED
ARGS Fore.White

Color map can be changed as such:

formatter.color_map[logging.INFO] = Colors.Fore.WHITE
formatter.color_map[logging.DEBUG] = Colors.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, Colors

log_format = (
    f'{Colors.Fore.GREEN}%(asctime)-s '
    f'{Colors.LEVEL_COLOR}%(levelname).1s '
    f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '
    f'{Colors.LEVEL_COLOR}- %(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-5.3.7.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

chromaformatter-5.3.7-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: chromaformatter-5.3.7.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.7 Linux/5.16.15-76051615-generic

File hashes

Hashes for chromaformatter-5.3.7.tar.gz
Algorithm Hash digest
SHA256 27f571e509ecefbba7622363eee7971111ee8beab97af9185041cbc5b63035ff
MD5 b1247a5da42c85adced62ab2542ce7f5
BLAKE2b-256 0e933a79bbd74d8aab80711566bf86af67b3d3645918344e929cbda4276471e5

See more details on using hashes here.

File details

Details for the file chromaformatter-5.3.7-py3-none-any.whl.

File metadata

  • Download URL: chromaformatter-5.3.7-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.7 Linux/5.16.15-76051615-generic

File hashes

Hashes for chromaformatter-5.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 430b988011ed22870792a6cb3f6f8f259bd6c0eba4b4d4d9ec1dc52d82923954
MD5 05d04dfc495bdc4e71d332cd07ad3956
BLAKE2b-256 863afa1c723dd919071023225399585bbb0cade304ff782c4a4175a65cbe88e4

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