[Tutor] redirecting output to logfile and logrotate

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jul 22 20:46:09 CEST 2005

On Fri, 22 Jul 2005, Winfried Tilanus wrote:

> I am installing a server-like python program that opens a logfile in the
> following way:
>     # redirect output for logging to file
>     applog = open(config.log, 'a', 1)
>     sys.stdout = applog
>     sys.stderr = sys.stdout
> Througout the program are lines like:
>             print '[', time.asctime(),']', message[:3]
> The logfiles of the program can grow quite a big, so I decided to
> include them in logrotate.

Hi Winfried,

You may want to look at the 'logging' module:


because it comes with a RotatingFileHandler that behaves like logrotate
for Python programs.

> How can I make the logging (including stderr) 'logrotate-proof'?

The approach to keep the log file open throughout the lifetime of the
program is what's causing issues with logrotate.  logrotate doesn't work
nicely with persistant server programs, because the server keeps the file
object open during the rotation, and that's bad.

(Just as an aside: the Mailman folks run into a similar issue:

So if it's possible, I'd recommend using the RotatingFileHandler logger
from Python's 'logging' Standard Library module instead of 'logrotate'.

Hope this helps!

More information about the Tutor mailing list