Extending logging module

J

jay

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
 
R

Richard Levasseur

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
 
V

Vinay Sajip

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

Where's the difficulty in extending the SyslogHandler class? Simply
override the methods you need to provide the behaviour you want, then
use your handler instead of the standard SyslogHandler. I'm travelling
at the moment and cannot post a working example, but it should be easy
enough to do. It's just the same as extending any other Python class -
but remember to call the base class's __init__ from your own __init__,
if you provide one.

Best regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Use of logging module to track TODOs 0
Logging 0
Logging help 0
JSON logging ? 0
Why BOM in logging message? 9
AttributeError: logging module bug ? 8
logging and PyQt4 1
python logging 11

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top