jay wrote:
> Hello,
> 
> I've been using the python logging module a lot lately, and I've come 
> across an instance where I need some new levels.  Specifically, python 
> does not include ALERT and NOTICE in the default set of logging levels.  
> I am wondering how trivial it would be to extend the logging module to 
> include these 2 levels that are standard with syslog?
> 
> My first test was just to use the addLevelName method to add the two new 
> levels, but syslog ignores them and logs at the next highest priority, 
> no matter what I use.
> 
> Looking further into the code and as a test, I changed the following to 
> see if this would even work
> 
> lib/python2.5/logging/__init__.py
>   -> class Logger
>   -> add new notice and alert root level functions
>   -> new _levelNames for notice and alert
>   -> new default level names for notice and alert

You could do all this by patching the logging module at runtime:
import logging
logging.ALERT = 60
logging.addLevelName(logging.ALERT, 'ALERT')

def Logger_alert(self, msg, *args, **kwargs):
  ...

logging.Logger.alert = Logger_alert

def alert(msg, *args, **kwargs):
   ...

logging.alert = alert

It's a little ugly but since you are just extending the module with new 
constants and functions it seems pretty safe and clean to me. It would 
be cool if addLevelName() did all this patching for you, it wouldn't be 
hard to do...maybe you want to propose a patch...

> lib/python2.5/logging/handlers.py
>   -> class SysLogHandler priority_map was changed to include notice and 
> alert

You can do this by subclassing SysLogHandler:

class FixedSysLogHandler(SysLogHandler):
   priority_map = { ... }

then configure logging to use FixedSysLogHandler instead of SysLogHandler.


You might want to post about this on comp.lang.python, I think the 
author of the logging module will see it there.

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to