ConcurrentLogHandler

Concurrent logging handler (drop-in replacement for RotatingFileHandler)
Download

ConcurrentLogHandler Ranking & Summary

Advertisement

  • Rating:
  • License:
  • The Apache License 2.0
  • Price:
  • FREE
  • Publisher Name:
  • Lowell Alleman

ConcurrentLogHandler Tags


ConcurrentLogHandler Description

Concurrent logging handler (drop-in replacement for RotatingFileHandler) ConcurrentLogHandler is a Python module that provides an additional log handler for Python's standard logging package (PEP 282). This handler will write log events to log file which is rotated when the log file reaches a certain size. Multiple processes can safely write to the same log file concurrently.DetailsThe 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 events. This module provides a file rotation scheme like with RotatingFileHanler. 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 log events are dropped).This module 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.If you have multiple instances of a script (or multiple scripts) all running at the same time and writing to the same log file, then all of the scripts should be using ConcurrentRotatingFileHandler. You should not attempt to mix and match RotatingFileHandler and ConcurrentRotatingFileHandler.This package bundles portalocker to deal with file locking. Please be aware that portalocker only supports Unix (posix) an NT platforms at this time, and therefore this package only supports those platforms as well.InstallationUse the following command to install this package:easy_install ConcurrentLogHandlerIf you are installing from source, you can use:python setup.py installExamplesSimple ExampleHere is a example demonstrating how to use this module directly (from within Python code):from logging import getLogger, INFOfrom cloghandler import ConcurrentRotatingFileHandlerimport oslog = 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 exampleIf 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 gracefully fallback to the built in RotatingFileHandler, here is an example:try: from cloghandler import ConcurrentRotatingFileHandler as RFHandlerexcept ImportError: # Next 2 lines are optional: issue a warning to the user from warnings import warn warn("ConcurrentLogHandler package not installed. Using builtin log handler") from logging.handlers import RotatingFileHandler as RFHandlerlog = getLogger()rotateHandler = RFHandler("/path/to/mylogfile.log", "a", 1048576, 15)log.addHandler(rotateHandler)Config file exampleThis 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:keys=rootkeys=hand01keys=form01level=NOTSEThandlers=hand01class=handlers.ConcurrentRotatingFileHandlerlevel=NOTSETformatter=form01args=("rotating.log", "a", 512*1024, 5)format=%(asctime)s %(levelname)s %(message)sExample Python code: app.py:import logging, logging.configimport cloghandlerlogging.config.fileConfig("logging.ini")log = logging.getLogger()log.info("Here is a very exciting log message, just for you") Requirements: · Python What's New in This Release: · Fixed lock-file naming issue · Resovled a minor issue where lock-files would be improperly named if the log file contained ".log" in the middle of the log name. For example, if you log file was "/var/log/mycompany.logging.mysource.log", the lock file would be named "/var/log/mycompany.ging.mysource.lock", which is not correct. Thanks to Dirk Rothe for pointing this out. Since this introduce a slight lock-file behaviour difference, make sure all concurent writers are updated to 0.8.4 at the same time if this issue effects you. · Updated ez_setup.py to 0.6c11


ConcurrentLogHandler Related Software