简体   繁体   中英

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM