Overriding Logging Config FileHandler Filename

Kenneth Love klove at tax.ok.gov
Mon Oct 1 13:29:43 EDT 2007


> On Sep 26, 1:07 am, "Vinay Sajip" <vinay_sajip at yahoo.co.uk> wrote:
>> On Sep 25, 9:15 pm, "Kenneth Love" <kl... at tax.ok.gov> wrote:
>> I have a Pythonloggingconfig file that contains a RotatingFileHandler
>> handler.  In the args key, I have hard-coded the log filename. 
>> Everything
>> works great.
>>
>> However, I find that I now need to override this filename at application
>> runtime.  Is there a good way to do this?
>>
>
> Are you using the original file name mentioned in the config file at
> all? Why and under what conditions do you need to switch? You could
> easily do this programmatically - have two handlers and when you need
> to, remove one from the logger and add the other. I see you are using
> a rotating handler, which itself does rollover to new log files every
> so often.
>
> Regards,
>
> Vinay Sajip


Sorry for the delay in my response.

This post is a little long.  Just bear with me and remember... You did
ask.  :-)

We want to move all of my logging, command line processing, and other 
generic
application initialization to a module (or set of modules) that can be used
in roughly 90% of our processing tasks. In other words, we are developing a
framework that is highly tailored to our needs.  We have most of what we 
need
already done.  Logging is one of the last steps to be completed because of
this issue.

One of the requirements for our processing is a log file name that is
specific to each task.  The format and behavior must be the same across all
tasks.  We determined that using a common logging configuration would be the
most expedient way of implementing this.  This method has the added benefit
of being easily modifiable outside of code (when our needs change).

The logging filename follows a specific behavior by "falling back" to a less
desirable method when the current one fails.  This determination is made
ONLY at application initialization.  In order of most to least
desirable, below is a list of the desired behavior:

1) Retrieve a predetermined key that is stored in an INI file passed via
   the command line.  The fully qualified path should be specified in the
   INI file, but it could be relative.
2) The name of the INI file passed via the command line with ".log" 
appended.
   Preferably stored in the same location as the INI file.  The INI file 
will
   not be stored in the same location as the Python script.
3) The name of the original calling script with ".log" appeneded (from the
   example in my original post this would be "mymain.py.log").  Submodules
   do not override this name (e.g. "mymain.py" calls "mysub01.py" and the
   log filename used is still "mymain.py.log").  This log file would be
   stored in the same directory as the main script.
4) Use the default log name stored in the INI file.  Again this should be
   the fully qualified path, but could be relative.
5) Raise an error (resulting in a message being sent to the console and via
   an e-mail).

My (probably erroneous) speculation is that I can retrieve a handler
specified in the INI file and dynamically change the filename argument to
the desired value.  So far, I have been unable to figure out how to do this.

In summary, we need a way to override, at runtime, the filename stored in
the logging configuration file.

adTHANKSvance,
Kenneth Love





More information about the Python-list mailing list