logging and daemons

Diez B. Roggisch deets at nospam.web.de
Mon Feb 16 12:07:12 EST 2009


Fernando M. Maresca schrieb:
> Hello.
> 
> I'm in the process of replacing a custom logger class in one of my apps
> that has several daemons. In the last step of daemonizing a program,
> after closing fds, stderr and stdout are redirected to the logfile of
> the program.
> 
> Now, I'm trying to use TimedRotatingFileHandler as the only channel when
> the programs run in daemon mode. My problem is: I can't see a goog way
> to redirect stderr/stdout both to the logger.
> 
> Note that I don't have any print statements in any of my code, but I
> can't be sure about all the modules I'm importing, and I like to get any
> uncached exception info that may go to stderr/stdout to show up in the
> logfiles.

why don't you just replace the sys.stdout/sys.stdin objects with 
something that is file-like (supports a write-method, that should be 
enough in this case), and then simply write the data arriving to the 
logger instance of your trust.

If other modules don't get overly tricky to bypass sys.stdout, that 
should capture all you output, regardless of the file descriptor issues.

In the end, if a 3rd-party-module choses to open a filedescriptor itself 
& write to that, there is nothing you can do about that. But usually, 
they don't behave that way.

Diez



More information about the Python-list mailing list