Redirecting stderr and stdout to syslog

Lincoln Yeoh lyeoh at pop.jaring.my
Tue Aug 5 09:47:20 EDT 2008


Hi,

I've just started to learn python (I've been using perl for some years).

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

Say I have a program called foo:

#!/usr/bin/python
import syslog
import os, sys
class logstderr:
         def write(self, data):
                 syslog.syslog('STDERR: %s' % data)
syslog.openlog('test[%u]' % os.getpid() )
sys.stderr=logstderr()
cmd='ls -al asdfsdf'
os.system(cmd)
bar('foo')

And bar is a nonexistent function.

If I run test I get:
./test
ls: cannot access asdfsdf: No such file or directory

And in /var/log/messages I get:
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: Traceback (most recent 
call last):
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:   File "./foo", line 
11, in <module>
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: bar('foo')
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: NameError
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: :
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: name 'bar' is not defined
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:


What I want is the "ls: cannot access asdfsdf: No such file or 
directory" message to go to syslog instead:
e.g.
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: ls: cannot access 
asdfsdf: No such file or directory
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: Traceback (most recent 
call last):
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:   File "./foo", line 
11, in <module>
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: bar('foo')
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: NameError
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: :
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR: name 'bar' is not defined
Aug  5 21:08:35 linux-9k3z test[2186]: STDERR:

Explanation:

I do not normally redirect STDERR and STDOUT to /dev/null for daemons I write.

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.

Sorry if this has been dealt with before - I haven't found the 
solution in my searches though.

I do NOT want to resort to this:

#!/bin/sh
/bin/foo 2>&1 | logger -t "test: STDERR/STDOUT"

:)

Thanks,

Link.




More information about the Python-list mailing list