<div align="center">
<!-- Title: -->
<h1>Chroma Formatter</h1>
<!-- Labels: -->
<!-- First row: -->
<img src="https://img.shields.io/badge/license-MIT-green"
height="20"
alt="License: MIT">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg"
height="20"
alt="Code style: black">
<img src="https://img.shields.io/pypi/v/chromaformatter.svg"
height="20"
alt="PyPI version">
<img src="https://img.shields.io/badge/coverage-100%25-success"
height="20"
alt="Code Coverage">
<h3>Wrapper for Python logging formatter that adds color</h3>
<img src="https://gitlab.com/mburkard/chroma-formatter/-/raw/main/docs/chroma_demo.png"
alt="Demo">
</div>
## 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:
```python
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.
```python
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.
```python
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:
```python
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:
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/mburkard/chroma-formatter",
"name": "chromaformatter",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Matthew Burkard",
"author_email": "matthewjburkard@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b3/f4/f3d8afa1fa46c14c120dc0af885ac307090f844276b30aeb1dcb95f2f623/chromaformatter-6.0.0.tar.gz",
"platform": null,
"description": "<div align=\"center\">\n<!-- Title: -->\n <h1>Chroma Formatter</h1>\n<!-- Labels: -->\n <!-- First row: -->\n <img src=\"https://img.shields.io/badge/license-MIT-green\"\n height=\"20\"\n alt=\"License: MIT\">\n <img src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"\n height=\"20\"\n alt=\"Code style: black\">\n <img src=\"https://img.shields.io/pypi/v/chromaformatter.svg\"\n height=\"20\"\n alt=\"PyPI version\">\n <img src=\"https://img.shields.io/badge/coverage-100%25-success\"\n height=\"20\"\n alt=\"Code Coverage\">\n <h3>Wrapper for Python logging formatter that adds color</h3>\n <img src=\"https://gitlab.com/mburkard/chroma-formatter/-/raw/main/docs/chroma_demo.png\"\n alt=\"Demo\">\n</div>\n\n## Installation\n\nChroma Formatter is on PyPI and can be installed with:\n\n```\npip install chromaformatter\n```\n\n## Usage\n\nChroma Formatter adds two features to the default logging formatter, colors can be added\nto the log format string, and formatted arguments in a log message can be colored.\nColors can be inserted info the format string as such:\n\n```python\nlog_format = (\n f'{Colors.Fore.GREEN}%(asctime)-s '\n f'{Colors.LEVEL_COLOR}%(levelname).1s '\n f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '\n f'{Colors.LEVEL_COLOR}- %(message)s'\n)\n```\n\nThen, use chromaformatter.ChromaFormatter rather than logging.Formatter.\n\n```python\nimport sys\nimport logging\n\nfrom chromaformatter import ChromaFormatter, Colors\n\nlog = logging.getLogger()\nlog_format = (\n f'{Colors.Fore.GREEN}%(asctime)-s '\n f'{Colors.LEVEL_COLOR}%(levelname).1s '\n f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '\n f'{Colors.LEVEL_COLOR}- %(message)s'\n)\nformatter = ChromaFormatter(\n fmt=log_format,\n arg_start_color=Colors.Fore.WHITE,\n arg_end_color=Colors.LEVEL_COLOR\n)\nhandler = logging.StreamHandler(stream=sys.stdout)\nhandler.setFormatter(formatter)\nlog.addHandler(handler)\n```\n\n### Formatted Arguments in a Log\n\nBy setting `arg_start_color` for argument colors and `arg_end_color` for the rest of the\nstring that comes after the argument, those colors will be applied to arguments.\n\n```python\nlog.info('This %s will be colored.', 'variable')\n```\n\n### Additional Configuration\n\nChromaFormatter has a dict called `color_map` to determine the colors of each logging\nlevel.\n\nBy default, the colors are:\n\n| Category | Color |\n|----------|-------------------|\n| NOTSET | Fore.LIGHTBLUE_EX |\n| DEBUG | Fore.BLUE |\n| INFO | Fore.Cyan |\n| WARNING | Fore.YELLOW |\n| ERROR | Fore.LIGHTRED_EX |\n| CRITICAL | Fore.RED |\n| ARGS | Fore.White |\n\nColor map can be changed as such:\n\n```python\nformatter.color_map[logging.INFO] = Colors.Fore.WHITE\nformatter.color_map[logging.DEBUG] = Colors.Fore.MAGENTA\n```\n\n## Applying to Existing Loggers\n\nIf you are using a third party module that uses the standard python logging module you\ncan apply a ChromaFormatter as such:\n\n```python\nimport sys\nimport logging\n\nfrom chromaformatter import ChromaFormatter, Colors\n\nlog_format = (\n f'{Colors.Fore.GREEN}%(asctime)-s '\n f'{Colors.LEVEL_COLOR}%(levelname).1s '\n f'{Colors.Fore.MAGENTA}%(filename)-s:%(lineno)03d '\n f'{Colors.LEVEL_COLOR}- %(message)s'\n)\nstream_formatter = ChromaFormatter(log_format)\nstream_handler = logging.StreamHandler(stream=sys.stdout)\n\nflask_logger = logging.getLogger('werkzeug')\nwhile flask_logger.handlers:\n flask_logger.removeHandler(flask_logger.handlers.pop())\nflask_logger.addHandler(stream_handler)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Wrapper for the Python logging formatter that adds color.",
"version": "6.0.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "45002bd66d95760fda5bb8e22677fe0ddf56a197a35eeb98c536fe5a30d873ad",
"md5": "e7c2b87e234570e22556b9a000104417",
"sha256": "85fd0077dd7891b116ea47da677da6654f92f3eb0bec199c27f35cdac1f83a82"
},
"downloads": -1,
"filename": "chromaformatter-6.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e7c2b87e234570e22556b9a000104417",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 4335,
"upload_time": "2023-03-29T21:53:26",
"upload_time_iso_8601": "2023-03-29T21:53:26.528388Z",
"url": "https://files.pythonhosted.org/packages/45/00/2bd66d95760fda5bb8e22677fe0ddf56a197a35eeb98c536fe5a30d873ad/chromaformatter-6.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b3f4f3d8afa1fa46c14c120dc0af885ac307090f844276b30aeb1dcb95f2f623",
"md5": "3e8de0f9dfac194acc4a22aef5becbfb",
"sha256": "deb6a8c83c9a065efb13c1986978644afd562f01db9a867d638d61f0cadd0aed"
},
"downloads": -1,
"filename": "chromaformatter-6.0.0.tar.gz",
"has_sig": false,
"md5_digest": "3e8de0f9dfac194acc4a22aef5becbfb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 4392,
"upload_time": "2023-03-29T21:53:28",
"upload_time_iso_8601": "2023-03-29T21:53:28.284664Z",
"url": "https://files.pythonhosted.org/packages/b3/f4/f3d8afa1fa46c14c120dc0af885ac307090f844276b30aeb1dcb95f2f623/chromaformatter-6.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-29 21:53:28",
"github": false,
"gitlab": true,
"bitbucket": false,
"gitlab_user": "mburkard",
"gitlab_project": "chroma-formatter",
"lcname": "chromaformatter"
}