Extending logging module
Richard Levasseur
richardlev at gmail.com
Fri Aug 10 21:26:14 EDT 2007
On Aug 9, 9:08 am, jay <titleistf... at gmail.com> 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
>
> lib/python2.5/logging/handlers.py
> -> class SysLogHandler priority_map was changed to include notice
> and alert
>
> It actually worked, but this is not the way I would like to handle the
> problem. I would prefer to extend the classes instead. However, I'm
> not too familiar with that, and I had to change things in so many
> places, I'm wondering if its even possible? I don't like changing the
> standard libraries, what happens if I have to upgrade python? My
> changes get overwritten.
>
> Can anyone offer some help or suggestions? Thanks
>
> Jay
Try looking at module new, i did the same to add a 'profile' level.
If push comes to shove, you can always wrap your logger object up or
access the __dict__ directly.
http://docs.python.org/lib/module-new.html
hope this helps,
richard
More information about the Python-list
mailing list