Hoegje
Hoegje

Reputation: 75

logging.config.dictConfig does not seem to work

What is wrong with this code? It prints out Whatsup to the stdOut, and nothing to the file in /tmp. It creates the file, but nothing is ever written to it, not even when loggging loads of data ('w'*20000). And there doesn't seem to be any handler specified either after I check.

>>> logger.handlers
[]
>>> logger.warning("Whatsup")
Whatsup
>>>

import logging
import logging.config
import multiprocessing

import threadfilter

VERBOSE_LOGGING = 1
directory = '/tmp/'



configDict = {
                'version': 1,
                'disable_existing_loggers': False,
                'formatters': {
                    'detailed': {
                        'class': 'logging.Formatter',
                        'format': '%(asctime)s - %(levelname)s =%(threadName)s= - Completer: %(message)s'
                    }
                },
                'handlers': {
                    'fileH': {
                        'class': 'logging.FileHandler',
                        'filename': '%s/ZZZZZZZZZZ_dispatcher_jobComplete3r.log' % (directory),
                        'formatter': 'detailed'
                    }
                },
                'loggers': {
                    'root': {
                        'handlers': ['fileH'],
                        'level': VERBOSE_LOGGING
                    }
                }
}

logging.config.dictConfig(configDict)
logger = logging.getLogger()
logger.handlers
logger.warning("Whatsup")

No errors are thrown either, it just seems to silently ignore my config.

Python 3.6.1

Upvotes: 4

Views: 2544

Answers (2)

Hoegje
Hoegje

Reputation: 75

Posted 2 second too soon.

I had to explicitely call the name of the root logger in my getLogger call. This did the trick:

logging.config.dictConfig(configDict)
logger = logging.getLogger('root')
logger.handlers
logger.warning("Whatsup")

Sorry!

Upvotes: 3

David Nugent
David Nugent

Reputation: 387

This turned up in a search for the same problem. Like you, I solved it, but did so differently. I think it is worth mentioning as google will turn it up fro others...

The default "root" handler isn't called "root", it is actually blank, or None.

Replacing 'root' with None (which is a valid dict key) makes it work for all loggers, not just the "root" one.

Upvotes: 5

Related Questions