mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
Provided better example for logging cookbook (GH-101164)
Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
This commit is contained in:
parent
3fa8fe7177
commit
8cdbc46090
1 changed files with 31 additions and 10 deletions
|
|
@ -1982,26 +1982,47 @@ Using a rotator and namer to customize log rotation processing
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
An example of how you can define a namer and rotator is given in the following
|
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::
|
runnable script, which shows gzip compression of the log file::
|
||||||
|
|
||||||
|
import gzip
|
||||||
|
import logging
|
||||||
|
import logging.handlers
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
def namer(name):
|
def namer(name):
|
||||||
return name + ".gz"
|
return name + ".gz"
|
||||||
|
|
||||||
def rotator(source, dest):
|
def rotator(source, dest):
|
||||||
with open(source, "rb") as sf:
|
with open(source, 'rb') as f_in:
|
||||||
data = sf.read()
|
with gzip.open(dest, 'wb') as f_out:
|
||||||
compressed = zlib.compress(data, 9)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
with open(dest, "wb") as df:
|
|
||||||
df.write(compressed)
|
|
||||||
os.remove(source)
|
os.remove(source)
|
||||||
|
|
||||||
rh = logging.handlers.RotatingFileHandler(...)
|
|
||||||
|
rh = logging.handlers.RotatingFileHandler('rotated.log', maxBytes=128, backupCount=5)
|
||||||
rh.rotator = rotator
|
rh.rotator = rotator
|
||||||
rh.namer = namer
|
rh.namer = namer
|
||||||
|
|
||||||
These are not "true" .gz files, as they are bare compressed data, with no
|
root = logging.getLogger()
|
||||||
"container" such as you’d find in an actual gzip file. This snippet is just
|
root.setLevel(logging.INFO)
|
||||||
for illustration purposes.
|
root.addHandler(rh)
|
||||||
|
f = logging.Formatter('%(asctime)s %(message)s')
|
||||||
|
rh.setFormatter(f)
|
||||||
|
for i in range(1000):
|
||||||
|
root.info(f'Message no. {i + 1}')
|
||||||
|
|
||||||
|
After running this, you will see six new files, five of which are compressed:
|
||||||
|
|
||||||
|
.. code-block:: shell-session
|
||||||
|
|
||||||
|
$ ls rotated.log*
|
||||||
|
rotated.log rotated.log.2.gz rotated.log.4.gz
|
||||||
|
rotated.log.1.gz rotated.log.3.gz rotated.log.5.gz
|
||||||
|
$ zcat rotated.log.1.gz
|
||||||
|
2023-01-20 02:28:17,767 Message no. 996
|
||||||
|
2023-01-20 02:28:17,767 Message no. 997
|
||||||
|
2023-01-20 02:28:17,767 Message no. 998
|
||||||
|
|
||||||
A more elaborate multiprocessing example
|
A more elaborate multiprocessing example
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue