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
  • Configuration
  • Application Configuration Settings
  • Toggle Log Level via Command-line
  • Usage
  1. Extensions

Logging

PreviousJsonNextMemcached

Last updated 6 years ago

Introduction

The Logging Extension includes the LoggingLogHandler, and provides log handling based on the standard library.

Documentation References:

API References:

Requirements

  • No external dependencies

Configuration

Application Configuration Settings

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

Setting

Description

level

The logging 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

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

Toggle Log Level via Command-line

This extension supports an optional feature to add a command-line argument to toggle the log level. This feature is not enabled by default for one specific reason: the log level will not be modified until after argument parsing happens. This can lead to a lot of confusion for developers who might not see their debug logs from a pre_setup hook, or anything that happens before argument parsing completes. For this reason, you should use this feature with caution and thus we disable it by default.

from cement import App, init_defaults

META = init_defaults('log.logging')
META['log.logging']['log_level_argument'] = ['-l', '--level']

class MyApp(App):
    class Meta:
        label = 'myapp'
        meta_defaults = META
$ python myapp.py --help
usage: myapp [-h] [--debug] [--quiet] [-l {info,warning,error,debug,fatal}]

optional arguments:
  -h, --help            show this help message and exit
  --debug               full application debug mode
  --quiet               suppress all output
  -l {info,warning,error,debug,fatal}
                        logging level


$ python myapp.py
INFO: This is an info message
WARNING: This is an warning message
ERROR: This is an error message
CRITICAL: This is a fatal message

$ python myapp.py -l error
ERROR: This is an error message
CRITICAL: This is a fatal message

Usage

You can enable the log level argument by setting via for the log.logging handler:

See the .

logging
Logging
Cement Logging Extension
Python Logging Library
App.Meta.meta_defaults
Logging Documentation