Choosing log file destination in logging configuration file

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Apr 23 12:50:33 EDT 2008


On Apr 22, 9:48 pm, guyben... at gmail.com wrote:
> On Apr 22, 12:57 pm, Miki <miki.teb... at gmail.com> wrote:
>
>
>
> > 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 startlogging.
>
> > > 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 theloggingconfiguration 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.teb... at gmail.com>http://pythonwise.blogspot.com
>
> I think it misses the point of having a file to config..
> It looks as if there's no solution. A peek at theloggingmodule shows
> this:
>         klass = cp.get(sectname, "class")
>         ...
>         klass = eval(klass, vars(logging))
>         args = cp.get(sectname, "args")
>         args = eval(args, vars(logging))
>         h = apply(klass, args)
>
> Bah. I'll have to split them into two configuration files (or subclass
> and have the respective file and rotating handlers lazy evaluate until
> the actual log call is made).
> Thanks though.

Not sure if it's any help - the SVN version contains a new optional
"delay" parameter for FileHandler and subclasses (including the
rotating handlers) which delays opening the file until there's a need
to, i.e. when an emit() call occurs.

Regards,

Vinay Sajip



More information about the Python-list mailing list