Skip to content

staticglobal/rainbow_logging_handler

 
 

Repository files navigation

rainbow_logging_handler

https://travis-ci.org/laysakura/rainbow_logging_handler.png?branch=master Latest PyPI version

Ultimate Python colorized logger.

http://github.com/laysakura/rainbow_logging_handler/raw/master/doc/screenshot.png

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()

http://github.com/miohtama/rainbow_logging_handler/raw/master/doc/screenshot_django.png

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")

More about configuring loggers for Django.

Set handler_console section in develop.ini:

[handler_console]
class = rainbow_logging_handler.RainbowLoggingHandler
args = (sys.stderr,)
level = NOTSET
format = [%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s

As apparent from above screenshot, each column of logs are differently colored. Even default coloring should make log reading easier.

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.

http://github.com/laysakura/rainbow_logging_handler/raw/master/doc/screenshot-custom-color.png

Linux, BSD, Mac OS, and Windows are supposed to be supported.

Runs with both Python 2.6 or higher & Python 3.2 or higher.

pip install rainbow_logging_handler
pip install -e "git+https://github.com/laysakura/rainbow_logging_handler.git#egg=rainbow_logging_handler"

Mikko Ohtamaa <mikko@opensourcehacker.com>, Sho Nakatani <lay.sakura@gmail.com>

And special thanks to 10sr for advice.

This is free and unencumbered public domain software. For more information, see <http://unlicense.org/> or the accompanying LICENSE.txt file.

Packages

No packages published

Languages

  • Python 100.0%