简体   繁体   中英

Issue in Python Logging using dictConfig

Below is the configuration dict i am using:

LOGGING_CONF = {
'version': 1,
'disable_existing_loggers': False,
'formatters': { 
    'verbose': {
        'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
    },
    'simple': {
        'format': '%(levelname)s %(message)s'
    },
},
'handler': { 
    'file': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',
        'formatter': 'verbose',
        'filename': 'D:\workspace\yogasync\codes\code_svn\YogaSync\log\workspacelogconfig.log',
        'maxBytes': 20480,
        'backupCount': 3, 
     },
},
'logger': {
    'extensive': {
        'level':'DEBUG',
        'handlers': 'file'
    },
},

}

in a module where i need to create logs i have inserted the below code:

import logging
import logging.config 
logging.config.dictConfig(LOGGING_CONF)

logger = logging.getLogger('extensive')

logger.info("This is my first log")

When i execute this, no errors are raised, however no logs get created. I am not sure what is missing out. I want to use dictCongif only Please help me with this.

You have to add a handler so that something happens with all those loggers. For example,

logfile = logging.handlers.FileHandler("mylog.log")
logfile.setLevel(logging.DEBUG)
logfile.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(module)s: %(message)s'))
logging.getLogger('').addHandler(logfile)

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