Overriding Logging Config FileHandler Filename

K

Kenneth Love

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/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
V

Vinay Sajip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top