Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

logging module: problem with custom handlers

5 views
Skip to first unread message

Andreas Jung

unread,
Oct 27, 2003, 6:50:54 AM10/27/03
to pytho...@python.org
I am trying to write a custom logger with a custom handler:

class MyHandler(StreamHandler):
pass

class Logger:

def __init__(self):
self._l = logging.getLogger('someident')
self._l.addHandler(MyHandler('/path/to/logfile'))


Now I want to configure this logger through a configuration
file. How can I refer to the new handler in the configuration file?
Something like "class=MyHandler" did not work.

Any ideas?

Thanks,
Andreas


wolf

unread,
Oct 27, 2003, 11:54:44 AM10/27/03
to
Andreas Jung <li...@andreas-jung.com> wrote in message news:<mailman.130.1067255...@python.org>...

> I am trying to write a custom logger with a custom handler:

you will have to be a tad more specific, i´m afraid. what does your
file layout look like? assuming that you have ::

fx.py
class X:
...

fy.py
class Y:
...
y = Y()

then, in order to refer to X, Y, or y from a third file, you can do
something like ::

from fx import X
from fy import Y
from fy import y

or ::

import fx.X

or ::

import fx.X as foo

there. does that help?

_wolf

Peter Otten

unread,
Oct 27, 2003, 1:17:08 PM10/27/03
to
Andreas Jung wrote:

> I am trying to write a custom logger with a custom handler:

> Now I want to configure this logger through a configuration
> file. How can I refer to the new handler in the configuration file?
> Something like "class=MyHandler" did not work.
>
> Any ideas?

The handler's class is evaluatated like so in module logging.config:

klass = eval(klass, vars(logging))

For this to succeed you could do something like:


import logging
import logging.config

class MyHandler(logging.StreamHandler):
pass

logging.MyHandler = MyHandler

logging.config.fileConfig("test.cfg")
lgr = logging.getLogger("test")

lgr.info("Hello world")


where the relevant handler section has the line

class=MyHandler

I do not know if there is an "official" way to achieve the same, but then,
why would you build your own Handler in the first place?

Peter


Neil Padgen

unread,
Oct 28, 2003, 7:54:50 AM10/28/03
to
>>>>> "Peter" == Peter Otten <__pet...@web.de> writes:

Peter> I do not know if there is an "official" way to achieve the
Peter> same, but then, why would you build your own Handler in the
Peter> first place?

I've just done exactly the same approach (independently), because of a
need to build two Handlers of my own. One is to avoid tracebacks
being logged with a SysLogHandler (it makes the syslog messages too
long, so nothing is logged), and the other is a subclass of
SMTPHandler which changes the subject of the mail if an exception of a
certain type is raised. [1]

I can think of plenty of other instances where building a custom
Handler is needed; for example, an SQLHandler which might log messages
to a database.

[1] This might be better done with a different Formatter, but getting
a custom Formatter to be used by logging.config is much harder than a
custom Handler, due to logging.Formatter() being called from within
logging.config.fileConfig().

Personally I think logging.config.fileConfig() is in need of some
serious refactoring... if only I had time to look into it!

-- Neil

0 new messages