简体   繁体   中英

Is there a difference between RotatingFileHandler and logrotate.d + WatchedFileHandler for Python log rotation?

Python has its own RotatingFileHandler which is supposed to automatically rotate log files. As part of a linux application which would need to rotate it's log file every couple of weeks/months, I am wondering if it is any different than having a config file in logrotate.d and using a WatchedFileHandler instead.

Is there any difference in how they operate? Is one method safer, more efficient, or considered superior to the other?

What is the intended audience of your program?

If you're creating a desktop application and most users can't be expected to read the logs, you should handle it for them. Not only rotating, but also deleting old ones - you don't want to fill the poor user's hard drive!

On the other hand, if the audience is experienced UNIX sysadmins, you'll have to take a different approach.

Sysadmins will need features you cannot possibly anticipate. Send them by email, write them to append-only storage, you name it. For this audience, it's best if your logging is as flexible as possible. Flexible (in UNIX) means simple - so just write to a file and consider it done.

Also, sysadmins don't want to re-learn how to do logging all over again . Even if you want to provide this kind of feature, make sure the default is reasonable within this assumption.

Finally. tdelaney raised a important point: the standard FileHandler doesn't pay much attention to the file it's writing to. You should use a WatchedFileHandler , which was written specifically for this purpose

RotatingFileHandler allows a log file to grow up to size N, and then immediately and automatically rotates to a new file.

logrotate.d runs once per day usually. If you want to limit a log file's size, logrotate.d is not the most helpful because it only runs periodically.

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