[Python-Dev] Re: PEP 282 comments

Trent Mick trentm@ActiveState.com
Wed, 20 Mar 2002 23:56:45 -0800


[Barry Warsaw wrote]
> This reminds me of another use case for logging, which may or may not
> be covered by PEP 282.
> 
> When an uncaught exception happens in Mailman (i.e. it percolates up
> to the "main loop" of the current process), I always want to log it
> somewhere because it definitely indicates a bug.
> 
> If this exception happens in the web interface, I will usually log the
> exception in some file, and also format the exception into html for
> output in the browser, /except/ when a `stealth mode' is enabled.  The
> traceback can be considered sensitive information, so when debugging
> we turn stealth mode off, but we always ship with stealth mode on.  In
> stealth mode, we still always log the traceback to a file, but we
> never output the traceback onto the web page.
> 
> It would also be great if I can create a logger that knows how to tee
> its output to multiple sinks.

For sure. A Logger object maintains a list of Handlers. You could have one
WebInterfaceHandler and one FileHandler attached to your Logger.


> If I understand the PEP correctly, I
> think I would use two standard formatters, a plaintext formatter and
> an html formatter.  I'd then have two custom handlers, one which
> outputs to html mindful of the current stealth mode, and one which
> tees the LogRecord to two sub-handlers (i.e. the stealth-mode-aware
> handler, and the normal to-file handler).

I don't know if I understand the example case here: do you not even log _to
file_ in stealth mode? In any case I would handle this by attaching a Filter
object to any Handler that should *not* log in stealth mode:

    class StealthModeFilter(logging.Filter):
        """Note this is using the Filter interface in the version of the PEP
        that I haven't re-released yet."""
        def filter(self, record):
            if record.exc_info and inStealthMode():
                return None  # drop the record
            else:
                return record

> 
> Does this seem reasonable given PEP 282's current proposal?

Sure. It depends on how you lay out your code whether the formatting and
display of the web interface is done via the logging module. I don't think
that that would be the right fit. The logging to file definitely fits into
the logging module's purview.



Trent

-- 
Trent Mick
TrentM@ActiveState.com