circular logging in python

logging, python

Solution

Depending on how you want to rotate the logs, you can use either the `TimedRotatingFileHandler` to rotate files at a given time interval (for example, create a new file ever day, or every hour); or you can use the `RotatingFileHandler` to create new files after they reach a certain size.

The logging cookbook has examples to help you get started.

Problem

I am currently using `logging` package in Python to log statements to a single file (`log.log`) The problem I am facing is if the log file gets too large, it takes a lot of time to open the log file. I thought after a pre-defined file size or line numbers, I can create a log file called `log.log.1`, and continue running with a fresh `log.log`. Also, after `log.log.5`, the old logs should get deleted automatically. How can I achieve this?

Original source

Related problems