wiki:PythonLogging

Python Logging Framework

  1. Introduction
  2. Channels
    1. File channel
    2. SysLog? channel
    3. Stderr channel
  3. Configuration
  4. Usage
    1. Create a logger
    2. Update a logger
    3. Log message

Introduction

Python logging module is responsible for progress and debugging information output. It is for testing and experimenting with logging ideas.

Currently, it supports three channels (file, syslog and stderr) and five levels (debug, info, warning, error and critical), more levels will be added later.

Channels

A daemon can't use syslog and stderr channel at the same time. SysLog? channel is intended for use when the daemon is running as a background process, and stderr channel works for foreground process. File channel is an optional, which output log messages to a disk file. File channel will be enabled if we specify the path of log file.

File channel

File channel has three config options, all of them can be reconfigured at runtime.

-log_file : The location of log file. File channel will be closed if the log_file is not specified.

-log_max_bytes : The option is used to limit log growth. The default behavior is not to limit the size of the file.

-log_versions : Logger retain that many backup versions of the file by renaming them when opening. No backup versions are kept by default.

SysLog? channel

It send log messages to the syslog. If facility is not specified, LOG_USER is used.

Stderr channel

All log messages send to the server's standard error stream.

Configuration

There are five configuration items for logging, the first two of which are logger config options while the last three for configuring the file channel . All of them can be reconfigurated by bindctl.

    "config_data": [
      {
        "item_name": "log_name",                   //the module name.
        "item_type": "string",
        "item_optional": False,
        "item_default": "Xfrout"
      },
      {
        "item_name": "log_severity",           //the logger severity               
        "item_type": "string",
        "item_optional": False,
        "item_default":  "debug"
      },
      {
        "item_name": "log_file",              //the location of log file           
        "item_type": "string",
        "item_optional": False,
        "item_default":  "@@LOCALSTATEDIR@@/@PACKAGE@/log/Xfrout.log"
      },
      {
        "item_name": "log_versions",           //how many versions of the file will be saved each time the file is opened.                   
        "item_type": "integer",
        "item_optional": False,
        "item_default":  5
      },
      {
        "item_name": "log_max_bytes",           //how large the file is allowed to become          
        "item_type": "integer",
        "item_optional": False,
        "item_default":  1048576
      } 
     ]

Usage

Take Xfrout module for example.

Create a logger

def __init__(self):
    ...
    self._cc = isc.config.ModuleCCSession(SPECFILE_LOCATION, self.config_handler, self.command_handler)
    self._log = isc.log.NSLogger(self._config_data.get('log_name'), self._config_data.get('log_file'),
                                 self._config_data.get('log_severity'), self._config_data.get('log_versions'),
                                 self._config_data.get('log_max_bytes'), log_to_console = verbose)
    ...

"log_to_console = True" means the daemon is running as a foreground process

Update a logger

def config_handler(self, new_config):
    ...
    if(self._log):
        self._log.update_config(new_config)
    ...

Log message

logger.log_message("info", "This is a log message")
Last modified 7 years ago Last modified on Jun 17, 2010, 11:22:15 PM