Redirecting stderr and stdout to syslog

Matthew Woodcraft mattheww at chiark.greenend.org.uk
Tue Aug 5 15:29:08 EDT 2008


In article <mailman.1134.1217944475.922.python-list at python.org>,

> How do I redirect ALL stderr stuff to syslog, even stderr from 
> external programs that don't explicitly change their own stderr?

Sending messages to syslog involves more than writing to a file
descriptor, so there's no way to make this happen without having some
process read the external programs' output and send it to syslog (which
is basically the 'logger' method that you said you didn't like).


> Since in _theory_ nothing should be "leaking" out, if stuff does leak 
> out in practice, something is not quite right.

> So I want all such "leaks" be redirected to syslog (or my logging 
> routines), so that I can see the bugs/warnings - whether in my 
> program or other programs/modules I call/use.

One reasonable way to do this is to have a separate 'breakage log' file
for this kind of message, and point standard error there. This will
catch output from external programs, and any output to standard error
which libraries in your main program (or the Python interpreter) decide
to produce.

You'd do that with something like this (untested):

    STDERR = 2
    se = os.open("breakage.log", os.O_WRONLY|os.O_APPEND)
    os.dup2(se, STDERR)
    os.close(se)

You can also use this breakage log for errors from your own program, if
there are any cases where you think things might be too messed up for
you to want to rely on your normal logging routines.

Then since the breakage log should normally be empty, you can write a
cronjob or something to keep an eye on it and notify you if anything
appears there.

-M-



More information about the Python-list mailing list