29

I'm having trouble getting going with Django logging. I've read both the Python and Django documentation on logging but I still don't see what I'm doing wrong. To start, I'm just trying to log a message to the console where my Django development server is running when I execute this simple view:

# demo/views.py import logging logger = logging.getLogger(__name__) def demo_logging(request, template): logger.error("Got some error") return render(request, template) 

I'm using Django's default logging setting as specified in django/utils/log.py in my settings file so that I (hopefully) know exactly what's happening (which, clearly I don't):

# settings.py DEBUG = True ... LOGGING_CONFIG = None LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, }, 'handlers': { 'console': { 'level': 'INFO', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', }, 'null': { 'class': 'logging.NullHandler', }, 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' } }, 'loggers': { 'django': { 'handlers': ['console'], }, 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': False, }, 'django.security': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': False, }, 'py.warnings': { 'handlers': ['console'], }, } } import logging.config logging.config.dictConfig(LOGGING) 

When I execute the view, I don't see anything in the console except the message,

No handlers could be found for logger "demo.views" 

I don't understand what I'm doing wrong. I would think calling logger.error would hit the django logger which is linked to the console handler which is defined.

Thanks.

FOLLOW UP I solved this problem by adding a default, "catch-all" logger that would be triggered when creating a logger using the "__name__" argument:

'loggers': { '': { 'handlers': ['console'], }, ... 

1 Answer 1

31

Calling logger = logging.getLogger(__name__) causes the logging module to search for a logger named as your module (demo.views); as you have no logger defined by that name, it fails. To simply log to console, you can use the django logger defined in 'loggers' key of your LOGGING configuration:

import logging logger = logging.getLogger('django') def demo_logging(request, template): logger.error("Got some error") return render(request, template) 
Sign up to request clarification or add additional context in comments.

4 Comments

This is true. However, a better approach which I just discovered might be to add a default logger as shown above. Given that the Django doc examples shows the use of "__name__" when specifying a logger, you'd think it would discuss creating a default, "catch-all" logger.
Yes I agree with you, makes one wonder why the docs don't mention that, or why isn't a 'catch-all' logger added by default.
So it simply fails. Without raising an exception.
@rafalmp I'm a bit late and also with a silly question, merely setting up django logger in settings.py stores all the logs from console into the file with a file handler, or it stores those only that we store from the views or relevant places. Same settings work fine on local machine, but don't log all INFO level logs on production with APACHE.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.