Refactored logging rotating handlers for improved flexibility.

This commit is contained in:
Vinay Sajip 2012-01-04 12:02:26 +00:00
parent 239a0429fd
commit 23b94d0b98
4 changed files with 215 additions and 17 deletions

View file

@ -1102,3 +1102,31 @@ This dictionary is passed to :func:`~logging.config.dictConfig` to put the confi
For more information about this configuration, you can see the `relevant
section <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_
of the Django documentation.
.. _cookbook-rotator-namer:
Using a rotator and namer to customise log rotation processing
--------------------------------------------------------------
An example of how you can define a namer and rotator is given in the following
snippet, which shows zlib-based compression of the log file::
def namer(name):
return name + ".gz"
def rotator(source, dest):
with open(source, "rb") as sf:
data = sf.read()
compressed = zlib.compress(data, 9)
with open(dest, "wb") as df:
df.write(compressed)
os.remove(source)
rh = logging.handlers.RotatingFileHandler(...)
rh.rotator = rotator
rh.namer = namer
These are not “true” .gz files, as they are bare compressed data, with no
“container” such as youd find in an actual gzip file. This snippet is just
for illustration purposes.