Colorlog

Introduction

The ColorLog Extension provides the ColorLogHandler for logging, and is a sub-class and drop-in replacement for the default log handler LoggingLogHandler.

Documentation References:

API References:

Requirements

  • Colorlog

Cement 3.0.8+:

pip install cement[colorlog]

Applications using Cement <3.0.8 should continue to include colorlog in their dependencies.

Installation

pip install cement[colorlog]

Configuration

This handler honors the following settings under a [log.colorlog] section of the configuration:

Note that there are precautions in place to disable colorized logging if the session is not a valid TTY via sys.stdout.istty()

A sample config section might look like:

~/.myapp.conf
[log.colorlog]
file = /path/to/config/file
level = info
to_console = true
rotate = true
max_bytes = 512000
max_files = 4
colorize_file_log = false
colorize_console_log = true

Usage

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['colorlog']
        log_handler = 'colorlog'

with MyApp() as app:
    app.run()
    app.log.debug('This is my debug message')
    app.log.info('This is my info message')
    app.log.warning('This is my warning message')
    app.log.error('This is my error message')
    app.log.fatal('This is my critical message')

The colors can be customized by passing in a colors dictionary mapping overriding the ColorLogHandler.Meta.colors option

from cement import App, init_defaults

META = init_defaults('log.colorlog')
META['log.colorlog']['colors'] = {
    'DEBUG':    'cyan',
    'INFO':     'green',
    'WARNING':  'yellow',
    'ERROR':    'red',
    'CRITICAL': 'red,bg_white',
}

class MyApp(App):
    class Meta:
        label = 'myapp'
        extension = ['colorlog']
        log_handler = 'colorlog'
        meta_defaults = META

Last updated