6

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.

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

6 Comments

We can configure logging in three ways: 1. Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above. 2. Creating a logging config file and reading it using the fileConfig() function. 3. Creating a dictionary of configuration information and passing it to the dictConfig() function. you have mentioned the 1st way of logging. i want to do it using 3rd method(dictConfig)
@Arun: oh in this case maybe the loggers key should be a list? it is handlers not handler. so have 'extensive': { 'level': 'DEBUG', 'handlers': ['file']}
Thank You, it should be handlers not handler and loggers not logger.(Stupid mistake) and yes handlers should be a list. Thanks, you helped me getting a beer bottle . ;)
ohh what is the malted liquor, what gets you drunker quicker, what comes in bottles or in caaans? BEER!
at our workplace we keep chilled beer ready for those who finish there days work at the earlist and today its CARLSBERG in bottle
|

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.