logging - Python- How to configure the log to a file and print to console using ini file -
i'm new python , i'm trying log file , console, saw question: logger configuration log file , print stdout
which helpful saw there way configure ini file , use in every module.
problem seems doesn't take properties ini file , if don't explicitly define formatter in code uses default logging without format gave him in ini file:
configfolder = os.getcwd() + os.sep + 'configuration' fileconfig(configfolder + os.sep + 'logging_config.ini') logger = logging.getlogger(__name__) # create file handler handler = logging.filehandler('logger.log') handler.setlevel(logging.info) # add handlers logger logger.addhandler(handler) logger.info('hello')
output just:
hello
instead of :
2016-08-08 15:16:42,954 - __main__ - info - hello
this ini file:
[loggers] keys=root [handlers] keys=stream_handler [formatters] keys=formatter [logger_root] level=info handlers=stream_handler [handler_stream_handler] class=streamhandler level=info formatter=formatter args=(sys.stderr,) [formatter_formatter] format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
Comments
Post a Comment