Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this Django logging work?

Tags:

logging

django

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'],
    },
    ...
like image 917
Jim Avatar asked Aug 01 '16 22:08

Jim


People also ask

How do I enable logging in Django?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

Is Python logging built in?

Python has a built-in module logging which allows writing status messages to a file or any other output streams.

Where do Django logs go?

The Django One-Click application employs Gunicorn and Upstart. Application level logging can be found in /var/log/upstart/gunicorn. log By default, Gunicorn logs to stderr and Upstart will collect output to stderr/stdout in /var/log/upstart/$JOB_NAME.


1 Answers

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)
like image 121
rafalmp Avatar answered Oct 24 '22 06:10

rafalmp