diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2012-01-04 12:02:26 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2012-01-04 12:02:26 (GMT) |
commit | 23b94d0b989d374cefaa6b81ed89301c65a0916a (patch) | |
tree | 897d41e9b324a8f55ab98353492161d7c8ab62f1 /Doc/howto | |
parent | 239a0429fdd8af2ff8a8a5fccbc19fe640d992b5 (diff) | |
download | cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.zip cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.tar.gz cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.tar.bz2 |
Refactored logging rotating handlers for improved flexibility.
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/logging-cookbook.rst | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index 7dc8021..37b2f29 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -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 you’d find in an actual gzip file. This snippet is just +for illustration purposes. + |