Choosing log file destination in logging configuration file

Miki miki.tebeka at gmail.com
Tue Apr 22 15:57:18 EDT 2008


Hello,

> So far so good. In the relevant applications, the code looks something
> like this:
> logging.config.fileConfig('log.ini')
> logger = logging.getLogger('log.regular') <or> logger =
> logging.getLogger('log.daemonic')
> .. and start logging.
>
> The thorn in my side is that after the fileConfig call, BOTH handlers
> are instantiated, meaning both types of files are created for every
> component, even if I don't need it: component.log (for the rotating
> handler) and compenent.date.log (for the regular file handler).
>
> ..So finally, here's my question:
> Apart from splitting the logging configuration into two separate
> files, is there any way to NOT create the file until you actually use
> it?
You can generate the .ini file on the fly and then load it:
>>> log_ini = gen_log_ini(type) # type is either "deamon" or "regular"
>>> atexit.register(lambda: remove(log_ini))
>>> logging.config.fileConfig(log_ini)

HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com



More information about the Python-list mailing list