How to use logging with python's fileConfig and configure the logfile filename How to use logging with python's fileConfig and configure the logfile filename python python

How to use logging with python's fileConfig and configure the logfile filename


Change your handler_fileHandler section like so:

[handler_fileHandler]class=FileHandlerlevel=DEBUGformatter=fileFormatterargs=('%(logfilename)s',)

and then add a defaults argument to the fileConfig call

logging.config.fileConfig(loginipath, defaults={'logfilename': '/var/log/mylog.log'})


Both handlers worked for me:

(1)

logging.config.fileConfig( 'logging.ini' , disable_existing_loggers=False)[handler_myhandler1]class=FileHandlerlevel=DEBUGformatter=form01args=('python.log', 'w')

(2)

logging.config.fileConfig( 'logging.ini' , disable_existing_loggers=False, defaults={ 'logfilename' : getSomeName() } )[handler_myhandler2]class=FileHandlerlevel=DEBUGformatter=form01args=('%(logfilename)s','w')

after reading examples at https://docs.python.org/2/library/logging.config.html


Try calling logging.config.dictConfig() after fileConfig() and just setting the filename.