Skip to main content

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

Automatic fallback example

If you are distributing your code and you are unsure if the ConcurrentLogHandler package has been installed everywhere your code will run, Python makes it easy to setup gracefully fall back to the built in RotatingFileHandler, here is an example:

try:
    from cloghandler import ConcurrentRotatingFileHandler as RFHandler
except ImportError:
    from warnings import warn
    warn("ConcurrentLogHandler package not installed.  Using builtin log handler")
    from logging.handlers import RotatingFileHandler as RFHandler

log = getLogger()
rotateHandler = RFHandler("/path/to/mylogfile.log", "a", 1048576, 15)
log.addHandler(rotateHandler)

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 separate 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.1: Minor bug fix release
    • Now importing “codecs” directly; I found some slight differences in the logging module in different Python 2.4.x releases that caused the module to fail to load.

  • 0.8.0: Minor feature release
    • Add better support for using logging.config.fileConfig(). This class is now available 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 had minimal testing in a multi-threaded process. 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.

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.1.tar.gz (20.5 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.1-py2.5.egg (25.3 kB view details)

Uploaded Egg

ConcurrentLogHandler-0.8.1-py2.4.egg (25.5 kB view details)

Uploaded Egg

File details

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

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.1.tar.gz
Algorithm Hash digest
SHA256 88d57f7762fd9a9c220d44999ee441cff459810d36b0d5e1d8180318a2ab85a7
MD5 084ea0976145e7cd7647b2a576d46597
BLAKE2b-256 e904dfdb1c6dff85fd54ae963a08d1ec963fb7ec95eca54a197824cf78603183

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.1-py2.5.egg
Algorithm Hash digest
SHA256 9edca190730c8b3e22fc0a1297c6c897787ec7dc7fee957560a2557bd7d98121
MD5 da4ba79a5d2e5bd170e2bef84e873d6e
BLAKE2b-256 89a4846d03b3a8d4c5a084ad66a39e6789f7f995c29890ef5475491312b40920

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ConcurrentLogHandler-0.8.1-py2.4.egg
Algorithm Hash digest
SHA256 1c8f2bff98a6881304e6505f40edbcf1e7932cbade75551323fa7b5ba65d1b15
MD5 70d996e5d403871db2883e01be5b243f
BLAKE2b-256 da3ac4b2ed018de0cb009637fc634aca77a53073ca83b7d36b84113f3bab0964

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 Sentry Error logging StatusPage Status page