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 loggingimport logging.config as lconfigimport jsonwith 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 fileinfo.log does not have the debug line, the output is this:
2020-01-03 15:32:23 : INFO - __main__ : info2020-01-03 15:32:23 : WARNING - __main__ : warning2020-01-03 15:32:23 : ERROR - __main__ : error2020-01-03 15:32:23 : CRITICAL - __main__ : criticalWhat should I do to fix this and print debug messages to theinfo.log file?
1 Answer1
You need to set the level on the root logger to DEBUG rather than INFO.
"root": { "level": "DEBUG",1 Comment
Explore related questions
See similar questions with these tags.

