ulif.loghandlers

Additional logging handlers

Downloads available at http://pypi.python.org/pypi/ulif.loghandlers/#downloads

This package provides additional loghandlers for use with the Python standardlib logging module.

Currently, only one additional logging handler is provided, which manages log file rotation based on Cron-like settings. So you can tell to rotate the logs at every day, week, month, or similar. Different to the logging.handlers.TimedRotatingFileHandler you can expect the rotation to happen at beginning of intervals.

For instance, if you start daily logging with logging.handlers.TimedRotatingFileHandler at 11:12h, a rollover will happen at 11:12h on the next day. With ulif.loghandlers.DatedRotatingFileHandler the rollover will instead happen at midnight, regardless of when you started the logging.

Furthermore you can let the number of backup files increase indefinitely by passing backupCount = -1.

Please note, that this package is still in a very early state and changes, also to the API, are likely to happen in near future.

Comments and patches are welcome. Please send these to uli at gnufix dot de.

Note

Python2.6, 2.7 only!

The package is currently usable with Python 2.6 and 2.7 only! Please drop me a line if you need it for other versions.

Installation

The package can be installed by:

$ pip install ulif.loghandlers

Afterwards you can use ulif.loghandlers in your Python programmes. See the documentation for details.

API

class ulif.loghandlers.DatedRotatingFileHandler(filename, when='h', interval=1, backupCount=-1, encoding=None, delay=False)

Handler for logging to a file, rotating the log file at certain timed intervals.

By default there will be an infinite number of backups. You can specify backupCount to limit the maximum number of backup files.

Different to other rotating file handlers, you can set backupCount to -1 to require indefinite number of backup files.

Rollover occurs whenever a point in time specified by when is reached. See the list of possible values below. Note that they are not case sensitive.

Value Type of interval
'S' Seconds
'MIN' Minutes
'H' Hours
'D' Days
'W' Week day (0=Monday)
'MON' Months
'Y' Years

A when value of ‘H’ will rotate the logs on every full hour (localtime), while ‘W0’ will rotate on every Monday at 0:00 h.

Different to logging.handlers.TimedRotatingFileHandler the rotation will happen at beginning of a new interval. If the interval is measured in hours, we rollover at beginning of each hour, if it is months, then we rollover at 0:00 h of first day of a new month after logging started.

If backupCount is nonzero, at most backupCount files will be kept, and if more would be created when rollover occurs, the oldest one is deleted. If backupCount is -1, an indefinite number of backup files will be kept.

If delay is true, then file opening is deferred until the first call to emit().

The interval parameter is currently ignored. It might be used for more Cron-like finetuning in future versions.

doRollover()

Do a rollover.

If backupCount is <> 0, the system will successively create new files with the same pathname as the base file, but with extensions ‘.1’, ‘.2’ etc. appended to it. For example, with a backupCount of 5 and a base file name of ‘app.log’, you would get ‘app.log’, ‘app.log.1’, ‘app.log.2’, ... through to ‘app.log.5’. The file being written to is always ‘app.log’ - when it gets filled up, it is closed and renamed to ‘app.log.1’, and if files ‘app.log.1’, ‘app.log.2’ etc. exist, then they are renamed to ‘app.log.2’, ‘app.log.3’ etc. respectively.

emit(record)

Emit a record.

Output the record to the file, catering for rollover as described in doRollover().

For Developers

The complete sources are available via SVN from https://svn.gnufix.de/repos/main/ulif.loghandlers.

You can get a copy of the trunk with:

$ svn co https://svn.gnufix.de/repos/main/ulif.loghandlers/trunk

As this package is based on zc.buildout, you might want to build the development environment first.

Change to the checked out directory and do:

$ python bootstrap.py

This will create local files needed for running buildout. This step is normally needed only once.:

$ ./bin/buildout

will then do the heavy stuff: fetch needed packages, install the testrunner and documentation generation scripts, etc.

You normally only have to rerun buildout only after changes to setup.py or buildout.cfg.

ulif.loghandlers tests are based on py.test. The needed test script should be generated by buildout automatically. You can run the tests entering:

$ ./bin/py.test

Releases of ulif.loghandlers should provide a test coverage of 100%. If you intend to send a patch, please also provide tests for your changes and check the test coverage with:

$ ./bin/py.test --cov src/ulif/loghandlers

The documentation can be built doing:

$ ./bin/sphinx-build docs/source docs/build/html

and can be found as HTML in docs/build/html/ afterwards.

Changes

0.1.1 (2012-07-04)

  • Fixed bug: second rollover did not rename files correctly if backupCount was set to -1.

0.1 (2012-05-22)

Initial implementation.

Indices and tables

Table Of Contents

This Page