[Python-Dev] Re: PEP 282 comments

Trent Mick trentm@ActiveState.com
Wed, 20 Mar 2002 18:08:17 -0800


[Mark Hammond wrote]
> [Trent]
> 
> > How about this (just an idea):
> >     def info(self, msgOrException, *args):
> >         if isinstance(msgOrException, Exception):
> >             # format the exception and
> >             msg, args = args[0], args[1:]
> >         else:
> >             # treat it like before
> >
> >     try:
> >         # ...
> >     except Exception, ex:
> >         log.info(ex, "Eeeeek!")
> 
> I don't like this.  I think it rare you will want to log an exception with
> no contextual message.

But you still *can* log contextual information. What I meant and should have
shown is:

     def info(self, firstarg, *args):
         if isinstance(firstarg, Exception):
             exc = firstarg
             msg, args = args[0], args[1:]
         else:
             exc = None
             msg, args = firstarg, args
         # ... (if exc is not None then format the exception and append to
         #      msg%args

     try:
         # ...
     except Exception, ex:
         log.info(ex, "Eeeeek! %s %s %s", var1, var2, var3)

basically this log() implementation is just trying to do:

    def log(self, exception, msg, *args):
        #...
    def log(self, msg, *args):
        #...

...in a language that does not support method overloading.

Kinda klugdy? Maybe, though I would rather have:

    def exception(self, msg, *args, *kwargs):
        if not kwargs.has_key("level"):
            level = logging.ERROR
        else:
            level = kwargs["level"]
        #...

than:

    def debugException(...)
    def infoException(...)
    def warnException(...)
    def errorException(...)
    def fatalException(...)
    def logException(...)


[Guido on logging an exception at a level other than ERROR]
> If this use case is rare enough, maybe a better approach would be to
> let you format it yourself using the traceback module?

then just have:
    def exception(self, msg, *args):
        #...
    
Startin' to sound good to me.

Trent

-- 
Trent Mick
TrentM@ActiveState.com