0

I am trying to configure my logger using dictConfig to use a different format but it does not seem to be taking effect. Following is the code that I have (I am also simultaneously trying to suppress the logs from imported modules)-

import logging.config
import requests

logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': True,
    'formatters':{'standard': { 'format': "[%(asctime)s] [%(levelname)8s] - %(message)s", 'datefmt':"%d-%b-%Y %I:%M:%S %p"}},
    'handlers': {'default': {'level': 'DEBUG', 'formatter': 'standard', 'class': 'logging.StreamHandler', 'stream': 'ext://sys.stdout'}},
    'loggers':{'__main__': {'handlers': ['default'], 'level': 'DEBUG', 'propagate': False }}
})

req = requests.get('https://www.google.com')

logging.debug("Only thing that should be printed")

Output -

DEBUG:root:Only thing that should be printed

Expected Output -

[2020-04-04 22:46:24,866] [   DEBUG] - Only thing that should be printed

I learnt how to use dictConfig from this SO post.

jar
  • 2,646
  • 1
  • 22
  • 47

1 Answers1

0

If you look at the post post that you've mentioned, you will see that you forget a line :)

log = logging.getLogger(__name__)

log.debug("Only thing that should be printed")

The logger hierarchy must be defined explicitly in the logger name, using dot-notation.

When using the __name__ :

This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.

For more explanations, the docs are pretty complete.

andreis11
  • 1,133
  • 1
  • 6
  • 10