Setting up logging in python application with features like rotating logger.

Introduction

  1. Read The Hitchhiker’s Guide to Python - Logging.
  2. Read Logging HOWTO complete.

Setting up logging:

I decided to use dict based config.

  1. An example dictionary-based configuration
  2. Elegant setup of Python logging in Django
  3. logging.config — Logging configuration
  4. LogRecord attributes

I used the following configuration:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '[%(asctime)s] [%(levelname)s] [%(name)s:%(funcName)s:%(lineno)d] [%(process)d:%(thread)d] %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {

    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'altin.log',
            'maxBytes': '16777216',  # 16megabytes
            'formatter': 'verbose',
            'backupCount': 10
        },
    },
    'loggers': {

    },
    'root': {
        'handlers': ['file'],
        'propagate': True,
        'level': 'DEBUG'
    }
}

And used it as following:

import logging
import logging.config


logging.config.dictConfig(LOGGING)

logger = logging.getLogger(__name__)
logger.debug("Sample msg")

Compression

Haven’t implemented it but the process is very simple as mentioned in Python, want logging with log rotation and compression.