59

I have a logging configuration file for logging to console and a file with different formats and levels. In my python script I can load this configuration and basically console and file output are ok.

I set the file name in the config file as shown below.

Is it possible to set that file name in the python script itself?

python code:

# set up logging
logging.config.fileConfig(loginipath)
logger = logging.getLogger('sLogger')

# log something
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

logging config file:

[loggers]
keys=root,sLogger

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=fileFormatter,consoleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_sLogger]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=sLogger
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=consoleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('logfile.log',)

[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_consoleFormatter]
format=%(levelname)s - %(message)s
datefmt=
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Micha
  • 591
  • 1
  • 4
  • 4

4 Answers4

73

Change your handler_fileHandler section like so:

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('%(logfilename)s',)

and then add a defaults argument to the fileConfig call

logging.config.fileConfig(loginipath, defaults={'logfilename': '/var/log/mylog.log'})
YDF
  • 365
  • 5
  • 9
Navin
  • 1,401
  • 10
  • 16
  • 3
    I know this is old, but is it possible to specify the filename in the config file name and path rather than in the script that is calling it? – frei Mar 21 '17 at 08:23
  • https://stackoverflow.com/questions/9484232/what-is-the-correct-way-of-configuring-pythons-logging-filehandler – LetsOMG Apr 30 '18 at 17:31
14

Both handlers worked for me:

(1)

logging.config.fileConfig( 'logging.ini' , disable_existing_loggers=False)


[handler_myhandler1]
class=FileHandler
level=DEBUG
formatter=form01
args=('python.log', 'w')

(2)

logging.config.fileConfig( 'logging.ini' , disable_existing_loggers=False, defaults={ 'logfilename' : getSomeName() } )

[handler_myhandler2]
class=FileHandler
level=DEBUG
formatter=form01
args=('%(logfilename)s','w')

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

MG2
  • 141
  • 1
  • 2
0

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

For those of you wanting to only specify the directory, not the filename, keep in mind you can use python expressions in the config file. So the config file should look something like:

[handler_myhandler2]
class=FileHandler
level=DEBUG
formatter=form01
args=(os.path.join('%(logdirpath)s', 'yourfilename.log'),'w')

and your caller should look like:

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

be mindfulf that the ConfigParser expects single forward slashes for filepaths.

cefect
  • 88
  • 6