-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provided better example for logging cookbook (GH-101164) #101164
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're doing this, we might as well change this snippet into a runnable script:
import gzip
import logging
import logging.handlers
import os
import shutil
def namer(name):
return name + '.gz'
def rotator(source, dest):
with open(source, 'rb') as f_in:
with gzip.open(dest, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
os.remove(source)
rh = logging.handlers.RotatingFileHandler('rotated.log', maxBytes=1024, backupCount=5)
rh.rotator = rotator
rh.namer = namer
root = logging.getLogger()
root.setLevel(logging.INFO)
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}')
and show that it seems to work:
$ gzip -dc rotated.log.1.gz | head -n 5
2023-01-19 21:55:12,677 Message no. 955
2023-01-19 21:55:12,678 Message no. 956
2023-01-19 21:55:12,678 Message no. 957
2023-01-19 21:55:12,678 Message no. 958
2023-01-19 21:55:12,709 Message no. 959
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be put in the comfy chair! |
I have made the requested changes; please review again |
Thanks for making the requested changes! @vsajip: please review the changes made to this pull request. |
GH-101183 is a backport of this pull request to the 3.10 branch. |
(cherry picked from commit 8cdbc46) Co-authored-by: Vladimir Malinovskii <galqiwi@galqiwi.ru> Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
GH-101184 is a backport of this pull request to the 3.11 branch. |
(cherry picked from commit 8cdbc46) Co-authored-by: Vladimir Malinovskii <galqiwi@galqiwi.ru> Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
@galqiwi : Thanks for this patch, now merged, but you signed the CLA using your |
This change is trivial, so, according to guidelines, i haven't created an issue.
I just made a small fix to the code from logging cookbook. The rotator function should compress file. And, to compress file, you need to use gzib library, not zlib.
To save you some time, this fact is clearly stated in zlib docs