logging and daemons

Garrett Cooper yanegomi at gmail.com
Mon Feb 16 14:07:45 CET 2009


On Mon, Feb 16, 2009 at 5:02 AM, Fernando M. Maresca <fmaresca at gmail.com> wrote:
>
> 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.
>
> Any ideas?
> Thanks a lot,

Hopefully this'll answer your question:

You can actually set sys.std[err|out] to your ?file? descriptor of
choice in python (it has to have read, write, and flush methods, IIRC
to function). The only thing is (like all things dealing with multiple
file descriptors like std[err|out]) the output may come in out of
order due to flushing and insertion into the buffers, but it shouldn't
be as much of an issue considering that the file descriptor for both
items is the same descriptor, but this is just a note of forewarning.

nose does something similar with the way that it scrapes stderr /
stdout for exception data and messages.

-Garrett



More information about the Python-list mailing list