Chroma Formatter
Wrapper for Python logging formatter that adds color
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)
Release files for chromaformatter 6.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| chromaformatter-6.0.0.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| chromaformatter-6.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.7 kB
Release files / chromaformatter-6.0.0.tar.gz
| Download URL | chromaformatter-6.0.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
deb6a8c83c9a065efb13c1986978644afd562f01db9a867d638d61f0cadd0aed
|
|
BLAKE2b-256 checksum How to use checksums |
b3f4f3d8afa1fa46c14c120dc0af885ac307090f844276b30aeb1dcb95f2f623
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.6 Linux/6.2.0-76060200-generic
|
Release files / chromaformatter-6.0.0-py3-none-any.whl
| Download URL | chromaformatter-6.0.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85fd0077dd7891b116ea47da677da6654f92f3eb0bec199c27f35cdac1f83a82
|
|
BLAKE2b-256 checksum How to use checksums |
45002bd66d95760fda5bb8e22677fe0ddf56a197a35eeb98c536fe5a30d873ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.6 Linux/6.2.0-76060200-generic
|