Search code examples
pythonlogrotatelog-rotation

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?


Solution

  • 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