Jim
Jim

Reputation: 14290

Why doesn't this Django logging work?

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'],
    },
    ...

Upvotes: 28

Views: 11751

Answers (1)

rafalmp
rafalmp

Reputation: 4068

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)

Upvotes: 30

Related Questions