简体   繁体   中英

forcing a log rotation in python logging module

If I have a rotating logging file. (logging_file.log) that rotates when x condition is met. How do I make it rotate? I know the rotating log module rotates automatically when the size exceeds a certain amount, but how do I make this rotation happen in the logging module for a different condition that is not size. Or how do I just force it to rotate the logging into a new logging file for whatever reason. Is there a function or command for this? Simply put how do I manually rotate a logging file if I want to use a newer and archive the old one. something like

if lines_in_file > 3
    rotate()

[note: the condition to make it rotate is irrelevant but I'm trying to ask for what function will make this rotation happen. Is there a simple function that does the job of rotate?

You should have (or can save) a reference to the appropriate instance of RotatingFileHandler from when you configured the logger. You just need to invoke its doRollover method:

rfh = RotatingFileHandler('foo.log')

...

rfh.doRollover()

More generally, RotatingFileHandler.doRollover uses BaseRotatingHandler.rotate , which takes the source and destination file names. rotate either calls the handler's rotator attribute with the same arguments, or just moves the source to the destination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM