Skip to main content

Concurrent logging handler (drop-in replacement for RotatingFileHandler)

Project description

Overview

This module provides an additional log handler for Python’s standard logging package (PEP 282). This handler will write a log entries to a set of files, which switches from one file to the next when the current file reaches a certain size. Multiple processes can write to the log file concurrently.

Details

The ConcurrentRotatingFileHandler class is a drop-in replacement for Python’s standard log handler RotatingFileHandler. This module uses file locking so that multiple processes can concurrently log to a single file without dropping or clobbering log records. This module provides a file rotation scheme like RotatingFileHanler, except that extra care is taken to ensure that logs can be safely rotated before the rotation process is started. (This module works around the file rename issue with RotatingFileHandler on Windows, where a rotation failure means that all subsequent logs are lost).

This module’s attempts to preserve log records at all cost. This means that log files will grow larger than the specified maximum (rotation) size. So if disk space is tight, you may want to stick with RotatingFileHandler, which will strictly adhere to the maximum file size.

Installation

Use the following command to install this package:

easy_install ConcurrentLogHandler

If you are installing from source, you can use:

python setup.py install

Examples

Simple Example

Here is a example demonstrating how to use this module directly (from within Python code):

from logging import getLogger, INFO
from cloghandler import ConcurrentRotatingFileHandler
import os

log = getLogger()
# Use an absolute path to prevent file rotation trouble.
logfile = os.path.abspath("mylogfile.log")
# Rotate log after reaching 512K, keep 5 old copies.
rotateHandler = ConcurrentRotatingFileHandler(logfile, "a", 512*1024, 5)
log.addHandler(rotateHandler)
log.setLevel(INFO)

log.info("Here is a very exciting log message, just for you")

Config file example

This example shows you how to use this log handler with the logging config file parser. This allows you to keep your logging configuration code seperate from your application code.

Example config file: logging.ini:

[loggers]
keys=root

[handlers]
keys=hand01

[formatters]
keys=form01

[logger_root]
level=NOTSET
handlers=hand01

[handler_hand01]
class=handlers.ConcurrentRotatingFileHandler
level=NOTSET
formatter=form01
args=("rotating.log", "a", 512*1024, 5)

[formatter_form01]
format=%(asctime)s %(levelname)s %(message)s

Example Python code: app.py:

import logging, logging.config
import cloghandler

logging.config.fileConfig("logging.ini")
log = logging.getLogger()
log.info("Here is a very exciting log message, just for you")

Changelog

  • 0.8.0: Minor feature release
    • Add better support for using logging.config.fileConfig(). This class is now avaiable using class=handlers.ConcurrentRotatingFileHandler.

    • Minor changes in how the filename parmerer is handled when given a relative path.

  • 0.7.4: Minor bug fix
    • Fixed a typo in the package description (incorrect class name)

    • Added a change log; which you are reading now.

    • Fixed the close() method to no longer assume that stream is still open.

To-do

  • This module has not yet be tested in a multi-threaded environment. I see no reason why this should be an issue, but no stress-testing has been done in a threaded situation. If this is important to you, you could always add threading support to the stresstest.py script and send me the patch.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ConcurrentLogHandler-0.8.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ConcurrentLogHandler-0.8.0-py2.5.egg (25.0 kB view details)

Uploaded Egg

ConcurrentLogHandler-0.8.0-py2.4.egg (25.1 kB view details)

Uploaded Egg

File details

Details for the file ConcurrentLogHandler-0.8.0.tar.gz.

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.0.tar.gz
Algorithm Hash digest
SHA256 afd36f369cf7d788f87149e1546f25e0707c900b431a54c99ff88a201dd54cdc
MD5 f872253f3c224414f8d90f99eca99d1e
BLAKE2b-256 e0801d518288461e1fd0a8df32aaf29636bf8662937be17c6fe2681faf4769ab

See more details on using hashes here.

File details

Details for the file ConcurrentLogHandler-0.8.0-py2.5.egg.

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.0-py2.5.egg
Algorithm Hash digest
SHA256 e27b438451005f51acc02b3fcf24862752b2f95fbdc4761ff1d41e4a7649343e
MD5 fb25d6e214de8cdfa17ebbb1508a5850
BLAKE2b-256 1a84f2d091753b2c18f95bf080c4f11676229e30862391149b177b332d4bee83

See more details on using hashes here.

File details

Details for the file ConcurrentLogHandler-0.8.0-py2.4.egg.

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.0-py2.4.egg
Algorithm Hash digest
SHA256 d219e35e752afda8bc834ad69c367e40dede7cb0f36481eab84fc51b883b0350
MD5 e96317c568334a8cd93e6fde81fb784b
BLAKE2b-256 93fabfc6550e53bd1c8cc79401b9c42f01bbfc2d4bf36addc96843b988b650a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page