Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Unable to configure handler 'console'

I have a simple logging definition in the settings.py file:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '[%(asctime)s] %(levelname)s %(message)s',
        },
        'verbose': {
            'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatters': 'simple',
            'propagate': False
        }
    },
    'loggers': {
        'myapp': {
            'handlers': ['console'],
            'level': 'DEBUG'
         },
    }
}

When I run my program, I get the error:

ValueError: Unable to configure handler 'console'

What is going wrong?

like image 989
conquester Avatar asked Oct 15 '25 03:10

conquester


2 Answers

propagate is not an option for handlers but for loggers. So your configuration should be.

'handlers': {
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'simple',
    }
},
'loggers': {
    'myapp': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': False
     },
}
like image 67
Nafees Anwar Avatar answered Oct 17 '25 02:10

Nafees Anwar


I changed and tested your LOGGING dictionary bit by bit, the following version works

{
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '[%(asctime)s] %(levelname)s %(message)s',
    },
    'verbose': {
        'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s',
    },
},
'handlers': {
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'simple',
    }
},
'loggers': {
    '': {
        'handlers': ['console'],
        'level': 'DEBUG',
        "propagate": True
     },
}

}

There are three differences between LOGGING and the above dictionary:

  1. In LOGGING, there is a key value pair 'propagate': False in LOGGING.handlers.console. It was removed in the above dictionary
  2. In LOGGING.loggers, the unique key is 'myapp'. In the above dictionary, the 'myapp' is replaced by an empty string ''
  3. In LOGGING.loggers, there is a key value pair "propagate": False in the unique sub dictionary. It was replaced by "propagate": True.

Hope it helps.

like image 41
BoHuang Avatar answered Oct 17 '25 00:10

BoHuang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!