Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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=
like image 866
Micha Avatar asked Nov 30 '12 17:11

Micha


People also ask

How do I use logging config in Python?

The logger's level filters every message before it can reach its handlers and the default is WARNING and above (as you can see). Setting the root logger's level to NOTSET as you have, as well as setting it to DEBUG (or whatever is the lowest level you wish to log) should solve your issue. Show activity on this post.

What is logging configuration?

Configuration dictionary schema. Describing a logging configuration requires listing the various objects to create and the connections between them; for example, you may create a handler named 'console' and then say that the logger named 'startup' will send its messages to the 'console' handler.


2 Answers

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'}) 
like image 113
Navin Avatar answered Sep 19 '22 23:09

Navin


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

like image 32
MG2 Avatar answered Sep 18 '22 23:09

MG2