Ultimate Python colorized logger with user-custom color
Project description
Ultimate Python colorized logger.
Usage
Generic usage example
This script runs like above screenshot.
import sys
import logging
from rainbow_logging_handler import RainbowLoggingHandler
def main_func():
# setup `logging` module
logger = logging.getLogger('test_logging')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s") # same as default
# setup `RainbowLoggingHandler`
handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("debug msg")
logger.info("info msg")
logger.warn("warn msg")
logger.error("error msg")
logger.critical("critical msg")
try:
raise RuntimeError("Opa!")
except Exception as e:
logger.exception(e)
if __name__ == '__main__':
main_func()
Usage with Django
Django is a popular Python web framework.
Put the following to your settings.py to get more pleasant development server console output:
# Add this to your settings.py if DEBUG: # Install rainbow logging handler when running Django in develoment mode import sys LOGGING["handlers"]["rainbow"] = {"level": "DEBUG", "class": "rainbow_logging_handler.RainbowLoggingHandler", 'stream': sys.stderr} LOGGING["loggers"]['']["handlers"].append("rainbow")
Features
Column-by-column colored log
As apparent from above screenshot, each column of logs are differently colored. Even default coloring should make log reading easier.
User custom color
Every column colors are customizable.
formatter = logging.Formatter('%(pathname)s [%(module)s] - %(funcName)s:L%(lineno)d : %(message)s')
handler = RainbowLoggingHandler(
sys.stderr,
# Customizing each column's color
color_pathname=('black', 'red' , True), color_module=('yellow', None, False),
color_funcName=('blue' , 'white', True), color_lineno=('green' , None, False),
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("hello custom world")
Above code produces output like this.
High portability
Linux, BSD, Mac OS, and Windows are supposed to be supported.
Runs with both Python 2.6 or higher & Python 3.2 or higher.
Install
Install from PyPI
$ pip install rainbow_logging_handler
Install from Github repo
$ git clone https://github.com/laysakura/rainbow_logging_handler.git
$ cd rainbow_logging_handler
$ ./setup.py install
License
This is free and unencumbered public domain software. For more information, see <http://unlicense.org/> or the accompanying LICENSE.txt file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file rainbow_logging_handler-2.1.2.zip
.
File metadata
- Download URL: rainbow_logging_handler-2.1.2.zip
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cbb69d0cc5059f785dca5f4cf04b7402e97300ee6ffe11c688856945496b293 |
|
MD5 | 03c922e5d2959419b37177767b82afa0 |
|
BLAKE2b-256 | af8a0c384c61e26c768fb43841cf35b1dbdc47b3e0435bfbcc613bd42487727f |