[Python-Dev] Re: PEP 282 comments

Vinay Sajip vinay_sajip@red-dove.com
Thu, 21 Mar 2002 02:33:02 -0000


> 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(...)

I agree that xxxException() methods are not really needed. Consider this:
I've provided a single logException() method (in 0.4, announcement just
posted to c.l.py), and it is certainly *functionally* enough to get the job
done. My suggestion about xxxException() methods was in response to Jeremy
saying he'd really  like the convenience methods. But it's probably too high
a price to pay for calls which only occur in exception handlers - which will
be a small proportion of the code in any (non-pathological!) system.

In most cases, people will be looking to associate traceback information
with errors. For them,

logger.exception(msg, args)

is just the ticket. For the less common cases, a call like

 logger.logException(logging.WARN, sys.exc_info(), msg, args)

does everything that is needed. This, it seems to me, is the simplest
interface which gives the desired flexibility. Even

exception(...)

is just shorthand for

logException(ERROR, sys.exc_info(), ...)

So at the cost of one extra method in the interface, we keep the
implementation neat,  clean and easy to understand and change. (And it's not
even "extra", in the sense that it's already there ...) For my money, the
above two methods provide the best value.

Regards

Vinay