I'm new to Python and I'm trying to log to file and to console, I saw this question: logger configuration to log to file and print to stdout
which was very helpful but I Saw that there is a way to configure an ini file and then use it in every module.
Problem is that it seems that it doesn't take the properties from the ini file and if I don't explicitly define the formatter in the code it uses the default logging without the format that I gave him in the ini file:
configFolder = os.getcwd() + os.sep + 'Configuration'
fileConfig(configFolder + os.sep + 'logging_config.ini')
logger = logging.getLogger(__name__)
# create a file handler
handler = logging.FileHandler('logger.log')
handler.setLevel(logging.INFO)
# add the handlers to the logger
logger.addHandler(handler)
logger.info('hello')
output would be just:
hello
instead of :
2016-08-08 15:16:42,954 - __main__ - INFO - hello
This is my 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