I am trying to use python logging module to output messages to a file, I found some configurations online and customized it to what I need but it isn't printing what I want it to print. No DEBUG level messages are appearing in the file handler with DEBUG level.
I am using this JSON configuration for the logger:
{
"version": 1,
"formatters": {
"simple": {
"format": "%(asctime)s : %(levelname)-8s - %(name)s : %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "info.log",
"maxBytes": 1E6,
"backupCount": 5,
"encoding": "utf8"
},
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "simple",
"filename": "errors.log",
"maxBytes": 1E6,
"backupCount": 5,
"encoding": "utf8"
}
},
"root": {
"level": "INFO", # EDIT - change this to DEBUG to fix
"handlers": ["file_handler", "error_file_handler"]
}
}
And I execute this code:
import logging
import logging.config as lconfig
import json
with open('logger.json', 'rt') as f:
config = json.load(f)
lconfig.dictConfig(config)
logger = logging.getLogger(__name__)
logger.info('info')
logger.warning('warning')
logger.debug('debug')
logger.error('error')
logger.critical('critical')
The problem here is the file info.log does not have the debug line, the output is this:
2020-01-03 15:32:23 : INFO - __main__ : info
2020-01-03 15:32:23 : WARNING - __main__ : warning
2020-01-03 15:32:23 : ERROR - __main__ : error
2020-01-03 15:32:23 : CRITICAL - __main__ : critical
What should I do to fix this and print debug messages to the info.log file?