Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Logging is not working on django website?

Tags:

python

django

This is what i tried . In my view.py file ,

import logging
logger = logging.getLogger("mylog")
logging.basicConfig(format='%(name)s:%(levelname)s:%(message)s',level=logging.INFO,datefmt='%d/%m/%y %I:%M:%S')

Then inside a function ,

logger.debug("this is an error")
logger.warning("This is a warning")
print "This is a test line '

I have not touched the settings.py file .its pretty much the same.

LOGGING = {

    'version': 1,

    'disable_existing_loggers': False,

    'handlers': {

        'mail_admins': {

            'level': 'ERROR',

            'class': 'django.utils.log.AdminEmailHandler'

        }

    },

    'loggers': {

        'django.request': {

            'handlers': ['mail_admins'],

            'level': 'ERROR',

            'propagate': True,

        },

    }

When i run the server , and call the function , nothing happens . no error , nothing .

I just want to see a log line on the console .

like image 437
Arindam Roychowdhury Avatar asked Jun 11 '12 07:06

Arindam Roychowdhury


People also ask

How do I enable logs 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.

How do I add a logger to Django project?

First Steps With Logging Begin by updating the app/views.py file with the following code: import logging from django. http import HttpResponse # This retrieves a Python logging instance (or creates it) logger = logging. getLogger(__name__) def index(request): # Send the Test!!

Is logging blocking in Python?

Sometimes you have to get your logging handlers to do their work without blocking the thread you're logging from. This is common in Web applications, though of course it also occurs in other scenarios. So, although not explicitly mentioned yet logging does seem to be blocking. For details see Python Docs.

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

Try adding this to handlers:

'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
           }

and in loggers:

    'django': {
        'handlers':['console'],
        'propagate': True,
        'level':'INFO',
    },

The log level could be the same or different - In fact the handler defines the minimum level it will log, while the logger defines the minimum level it will send to handler. If one handler is used by two or more loggers - it should ideally has the lowest level from both loggers.

EDIT: Thanks to @jpic for pointing the loggers section.

like image 166
Tisho Avatar answered Sep 20 '22 20:09

Tisho