[Tutor] logging stderr

Arne Mueller a.mueller@icrf.icnet.uk
Mon, 19 Apr 1999 21:20:48 +0100


"Joseph J. Strout" wrote:
> 
> At 12:24 PM -0700 04/19/99, Arne Mueller wrote:
> 
> >is there a way to write messages normally going to 'sys.stderr' (e.g.
> >pythons error messages) to a file AND to 'sys.stderr' (on unix) without
> >changing all my 'sys.stderr.write('...')' statements of the source
> >files?
> 
> Sure!  (It's Python -- there's always a way!)  Just create an object that
> has a write() method.  The code of this method will log to a file, and to
> whatever the previous sys.stderr went to (you'll need to save that,
> probably in a member variable).  Then assign sys.stderr to this method.
> Presto!

Brilliant! Cool, I'm realy excited! 

class StreamCP:

    def __init__(self, src=sys.stderr, dst=sys.stdout):
        self.src = src
        self.dst = dst
        
    def write(self, msg):
        self.src.write(msg)
        self.dst.write(msg)


log_file = open('./blastflt.log', 'a+')
log = StreamCP(sys.stderr, log_file)
sys.stderr = log

Please note, the above assignment wokrs but assigning sys.stderr to
log.write doesn't.

	greetings,
	Arne