LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction
  • Requirements
  • Installation
  • Configuration
  • Usage
  1. Extensions

Colorlog

PreviousArgparseNextConfigParser

Last updated 2 years ago

Introduction

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

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:

Setting

Description

level

The level to display logs for. One of INFO, WARNING, ERROR, FATAL, DEBUG. Default: INFO

file

The filesystem path of the log file. Default: None

to_console

Whether or not to log to console. Default: True

rotate

Whether or not rotate the log file. Default: False

max_bytes

Maximum file size (in bytes) until the log file is rotated (if rotation is enabled). Default: 512000

max_files

Maximum number of files to keep when rotating is enabled. Default: 4

colorize_file_log

Whether or not to colorize the log file. Default: False

colorize_console_log

Whether or not to colorize the console log. Default: True

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

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

ColorLogHandler
LoggingLogHandler
Logging
Cement Colorlog Extension
Colorlog Module
ColorLogHandler.Meta.colors