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")
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.2: Minor bug fix release (again)
Found and resolved another issue with older logging packages that do not support encoding.
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for ConcurrentLogHandler-0.8.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced2516783d670f2ec075afc755c6af6bb87e3a50276d68fa2cf6690d260d08f |
|
MD5 | 4a2c38eb9f2d8312f2463c2d608cbc6f |
|
BLAKE2b-256 | 3e4f765035c72150e2987a14d7f8de294bf22ae0670df5dfc73a3c986e78fb57 |
Hashes for ConcurrentLogHandler-0.8.2-py2.5.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a47b8fd9ab32293937b84661417a25bbd2b8dbb06303ccc0a39ea7fd2c3bf50 |
|
MD5 | 8a77782c2e2853e91d80371fef0261ca |
|
BLAKE2b-256 | 02c4083a7cacad94c2b73cead296e6e0c27d76b3408fda174b17a7d295ebc7c3 |
Hashes for ConcurrentLogHandler-0.8.2-py2.4.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1b534961b89a6e5a75efab8b73671efd21d1ba73430ebfab59c29e7b692bfc3 |
|
MD5 | 9d9ed7fc2516c12d0acb1719254def58 |
|
BLAKE2b-256 | 063efb41cf3323ca434dc8a850becb85ba49d101a5731430d313f08c1b82e855 |