471,330 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Overriding Logging Config FileHandler Filename

I have a Python logging config 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?

Here is a bit of sample code that (hopefully) clarifies my question.

--------------- log.ini -----------------
[loggers]
keys=root,processor

; Lots more stuff here: handlers, formatters, and loggers.......

[handlers]
keys=consoleHandler,fileHandler

[handler_fileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=standard
args=('D:\Projects\Python\src\log.log', 'a', 99999, 10)

-------------- mymain.py -----------------
import logging.config

logging.config.fileConfig('log.ini')

# TODO: Override log filename with new name (e.g. 'd:\\logs\\mymain.py.log')

log = logging.getLogger('mymain')

log.info('START: mymain.py')
log.info('FINISH: mymain.py')
------------------------------------------

adTHANKSvance,
Kenneth Love

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Kenneth Love | Oklahoma Tax Commission
DP Programmer/Analyst | Information Technology
(405) 522 - 5864 | http://www.tax.ok.gov/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Sep 25 '07 #1
1 3402
On Sep 25, 9:15 pm, "Kenneth Love" <kl...@tax.ok.govwrote:
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

Sep 26 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.