logging module: problem with custom handlers

Peter Otten __peter__ at web.de
Mon Oct 27 13:17:08 EST 2003


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






More information about the Python-list mailing list