0

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__ : critical

What should I do to fix this and print debug messages to theinfo.log file?

askedJan 3, 2020 at 18:38
Gustavo's user avatar

1 Answer1

1

You need to set the level on the root logger to DEBUG rather than INFO.

"root": {   "level": "DEBUG",
answeredJan 3, 2020 at 18:47
Dan D.'s user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

How to do this? Where should I add this configuration?

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.