[stdlib-sig] logging

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Sep 18 15:54:36 CEST 2009


Barry Warsaw <barry at ...> writes:

> 
> So, I'm a big fan of the logging package and thank Vinay for his work  
> on it over the years.  It was quite a joy to chuck all the hacky  
> Mailman 2 logging crap in favor of the standard logging package for MM3.
>

Yay, a thumbs up! Thanks, Barry, you're very welcome!
 
> The one thing I (very) occasionally want is to ask a logger for a file- 
> like object suitable for print.  There are some situations where I  
> have a 3rd party API that requires a file-like object to output to,  
> but I really want that output to go to a log.  I'm pretty sure I've  
> wrangled it out of a file-based logger, but it would be nice have this  
> as an official API.  Maybe it's there and I've just missed it though.
> 

A StreamHandler (which includes file-based handlers) has a stream attribute
which is a file-like object. Just pass that to your 3rd-party API.

N.B. If it's a file-based handler and you specify a delay argument to the
handler constructor, the file isn't actually opened until you first log to it,
so stream will be None. So you might need to be careful about that (by default,
a file-based handler doesn't delay opening the file).

Regards,

Vinay Sajip



More information about the stdlib-sig mailing list