rainbowlog


Namerainbowlog JSON
Version 2.0.5 PyPI version JSON
download
home_pagehttps://github.com/abrahammurciano/rainbowlog
SummaryFormat your python logs with colours based on the log levels.
upload_time2024-06-03 18:59:04
maintainerNone
docs_urlNone
authorAbraham Murciano
requires_python<4.0,>=3.7
licenseMIT
keywords logging color
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Rainbow Log

Format your python logs with colours based on the log levels.

## Installation

You can instll the package with pip.
```sh
$ pip install rainbowlog
```
```sh
$ conda install rainbowlog -c abrahammurciano
```

## Links

* [Documentation](https://abrahammurciano.github.io/rainbowlog/rainbowlog)
* [Github](https://github.com/abrahammurciano/rainbowlog)
* [PyPI](https://pypi.org/project/rainbowlog/)

## Usage

Here's a basic example of a script that logs colorfully to the console, but regularly to a file.

```python
import logging
import rainbowlog

logger = logging.getLogger(__name__)

# This one will write to the console
stream_handler = logging.StreamHandler()

# This one will write to a file
file_handler = logging.FileHandler("output.log")

# Here we decide how we want the logs to look like
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# We want the stream handler to be colorful
stream_handler.setFormatter(rainbowlog.Formatter(formatter))

# We don't want the file handler to be colorful
file_handler.setFormatter(formatter)

# Finally we add the handlers to the logger
logger.addHandler(stream_handler)
logger.addHandler(file_handler)

if __name__ == "__main__":
	logger.debug("This is a debug message")
	logger.info("This is an info message")
	logger.warning("This is a warning message")
	logger.error("This is an error message")
	logger.critical("This is a critical message")
```

If you want to change the format of the logs for each log level, you can use any callable that takes a string and returns the same string with ANSI codes surrounding it. There are many libraries you can use to provide such callables.

```py
import logging
from rainbowlog import Formatter

# Here are some libraries you can use to get a style callable without dealing with ANSI codes
from constyle import Style, Attributes as Attrs
import termcolor
from functools import partial


formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
color_formatter = Formatter(
	formatter,
	log_styles={
		logging.DEBUG: Style(Attrs.BLUE, Attrs.FAINT), # An example using constyle
		logging.INFO: lambda s: f"\033[32m{s}\033[0m", # An example using lambdas
		logging.WARNING: termcolor.red, # An example using termcolor's predifined functions
		logging.ERROR: partial(termcolor.colored, color="red", on_color="on_white", attrs=["bold"]), # An example using functools.partial
		logging.CRITICAL: Attrs.RED + Attrs.ON_YELLOW + Attrs.BOLD + Attrs.UNDERLINE, # An example using constyle's added attributes
	}
	exception_style=lambda s: f"{Attrs.RED + Attrs.ON_WHITE + Attrs.BOLD}{s}{Attrs.RESET}" # An example using lambdas and constyle,
	stack_style=Attrs.RED, # An example using a single constyle attribute
)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/abrahammurciano/rainbowlog",
    "name": "rainbowlog",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.7",
    "maintainer_email": null,
    "keywords": "logging, color",
    "author": "Abraham Murciano",
    "author_email": "abrahammurciano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/84/0f/847db4a474fd514efab9643c0b689e8ba18ceaec4791c7b7ba40313f50f4/rainbowlog-2.0.5.tar.gz",
    "platform": null,
    "description": "# Rainbow Log\n\nFormat your python logs with colours based on the log levels.\n\n## Installation\n\nYou can instll the package with pip.\n```sh\n$ pip install rainbowlog\n```\n```sh\n$ conda install rainbowlog -c abrahammurciano\n```\n\n## Links\n\n* [Documentation](https://abrahammurciano.github.io/rainbowlog/rainbowlog)\n* [Github](https://github.com/abrahammurciano/rainbowlog)\n* [PyPI](https://pypi.org/project/rainbowlog/)\n\n## Usage\n\nHere's a basic example of a script that logs colorfully to the console, but regularly to a file.\n\n```python\nimport logging\nimport rainbowlog\n\nlogger = logging.getLogger(__name__)\n\n# This one will write to the console\nstream_handler = logging.StreamHandler()\n\n# This one will write to a file\nfile_handler = logging.FileHandler(\"output.log\")\n\n# Here we decide how we want the logs to look like\nformatter = logging.Formatter(\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\")\n\n# We want the stream handler to be colorful\nstream_handler.setFormatter(rainbowlog.Formatter(formatter))\n\n# We don't want the file handler to be colorful\nfile_handler.setFormatter(formatter)\n\n# Finally we add the handlers to the logger\nlogger.addHandler(stream_handler)\nlogger.addHandler(file_handler)\n\nif __name__ == \"__main__\":\n\tlogger.debug(\"This is a debug message\")\n\tlogger.info(\"This is an info message\")\n\tlogger.warning(\"This is a warning message\")\n\tlogger.error(\"This is an error message\")\n\tlogger.critical(\"This is a critical message\")\n```\n\nIf you want to change the format of the logs for each log level, you can use any callable that takes a string and returns the same string with ANSI codes surrounding it. There are many libraries you can use to provide such callables.\n\n```py\nimport logging\nfrom rainbowlog import Formatter\n\n# Here are some libraries you can use to get a style callable without dealing with ANSI codes\nfrom constyle import Style, Attributes as Attrs\nimport termcolor\nfrom functools import partial\n\n\nformatter = logging.Formatter(\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\")\ncolor_formatter = Formatter(\n\tformatter,\n\tlog_styles={\n\t\tlogging.DEBUG: Style(Attrs.BLUE, Attrs.FAINT), # An example using constyle\n\t\tlogging.INFO: lambda s: f\"\\033[32m{s}\\033[0m\", # An example using lambdas\n\t\tlogging.WARNING: termcolor.red, # An example using termcolor's predifined functions\n\t\tlogging.ERROR: partial(termcolor.colored, color=\"red\", on_color=\"on_white\", attrs=[\"bold\"]), # An example using functools.partial\n\t\tlogging.CRITICAL: Attrs.RED + Attrs.ON_YELLOW + Attrs.BOLD + Attrs.UNDERLINE, # An example using constyle's added attributes\n\t}\n\texception_style=lambda s: f\"{Attrs.RED + Attrs.ON_WHITE + Attrs.BOLD}{s}{Attrs.RESET}\" # An example using lambdas and constyle,\n\tstack_style=Attrs.RED, # An example using a single constyle attribute\n)\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Format your python logs with colours based on the log levels.",
    "version": "2.0.5",
    "project_urls": {
        "Documentation": "https://abrahammurciano.github.io/rainbowlog/rainbowlog/",
        "Homepage": "https://github.com/abrahammurciano/rainbowlog",
        "Repository": "https://github.com/abrahammurciano/rainbowlog"
    },
    "split_keywords": [
        "logging",
        " color"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "78377899c348ddd021768f84eadf76a0c79087b04a0403d95081a43644c3f576",
                "md5": "ce983bc975790ee7c48353e7343e7e03",
                "sha256": "e4d851bc90d96da5faff0f1692d7e7b2405f545ca17cd2fade0edbbdd7f8c2ff"
            },
            "downloads": -1,
            "filename": "rainbowlog-2.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ce983bc975790ee7c48353e7343e7e03",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 4475,
            "upload_time": "2024-06-03T18:59:02",
            "upload_time_iso_8601": "2024-06-03T18:59:02.420616Z",
            "url": "https://files.pythonhosted.org/packages/78/37/7899c348ddd021768f84eadf76a0c79087b04a0403d95081a43644c3f576/rainbowlog-2.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "840f847db4a474fd514efab9643c0b689e8ba18ceaec4791c7b7ba40313f50f4",
                "md5": "65d63673281e86ddf6d823f2fa2d8006",
                "sha256": "26b7d8d8560198826bf5bd9af015fe2995233955458b3626e045802589402b6f"
            },
            "downloads": -1,
            "filename": "rainbowlog-2.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "65d63673281e86ddf6d823f2fa2d8006",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 3700,
            "upload_time": "2024-06-03T18:59:04",
            "upload_time_iso_8601": "2024-06-03T18:59:04.166244Z",
            "url": "https://files.pythonhosted.org/packages/84/0f/847db4a474fd514efab9643c0b689e8ba18ceaec4791c7b7ba40313f50f4/rainbowlog-2.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-03 18:59:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "abrahammurciano",
    "github_project": "rainbowlog",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "rainbowlog"
}
        
Elapsed time: 0.42967s