Search code examples
pythonloggingtwistedtwistd

Twisted Python - twistd - How to remove old log files


I am using twistd to run my application, and I read how to customize the logger. However, I can't seem to figure out how to get the default logger (-l option, uses sequential integers for extensions) or the DailyLogFile to limit the number of rotated logs. I'd ideally like the rotated log files to have date extensions in "%Y-%m-%d" format and be limited to 7 at a time.

I tried using logrotate on my system, but I found that it inexplicably puts binary data in the log file when it does a copytruncate.

EDIT: I believe the reason for the binary data may be similar to this answer.


Solution

  • For the default logger, you can specify maxRotatedFiles which will limit the number of files created. See this answer for an example https://stackoverflow.com/a/32273153/66349

    The DailyLogFile class doesn't support this option. There is a patch to add the functionality but I believe (from memory) it hasn't been accepted yet as it's missing a few tests and/or doesn't meet some code style guidelines.

    Also note that twisted changed their logging system recently so this is all considered legacy now. It still works but that may change at some point. Here's a link to the new twisted.logger docs https://twisted.readthedocs.io/en/latest/core/howto/logger.html