473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Redirecting stderr and stdout to syslog

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.

Aug 5 '08 #1
1 6661
In article <ma*************************************@python.or g>,
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-
Aug 5 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jan Knop | last post by:
Hello I am writing a Windows application where I need to redirect stdin, stdout and stderr from Python. to my application Is it a simple way of do it ? Has anyone done it using Winsock ?
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
0
by: Ahmad Hosseinzadeh | last post by:
Hello, I’m trying to run an external program in my application. Both are coded in python. I need to write an independent module that is used in the main application. Its responsibility is to...
2
by: Jason Heyes | last post by:
Here is what I use to redirect printed messages to a file: std::ofstream file("logfile"); std::cout.rdbuf(file.rdbuf()); std::cout << "this message goes to a file" << std::endl; Will the...
3
by: Laszlo Zsolt Nagy | last post by:
Hello, I have this code: s = smtplib.SMTP() s.set_debuglevel(1) s.connect(host=smtp_host) s.set_debuglevel(0) log("Connected, sending e-mail") sys.stdout.flush()
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
9
by: Fuzzyman | last post by:
Hello, I'm trying to redirect standard out in a single namespace. I can replace sys.stdout with a custom object - but that affects all namespaces. There will be code running simultaneously...
10
by: SamG | last post by:
How could i make, from inside the program, to have the stdout and stderr to be printed both to a file as well the terminal(as usual).
1
by: n00b | last post by:
greetings, i would like to redirect stdout/err to a mysql table and would like a) some peer review and b) suggestions for hardening the approach for a general purpose class. thank you very much....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.