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 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?

1 Answer 1

1

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

"root": { 
  "level": "DEBUG",
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

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy 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.