100

I've set up django server with nginx, and it gets 403 error in some of the pages.

Where can I find the django logs? where can I see the errors in detail?

arogachev
  • 33,150
  • 7
  • 114
  • 117
Yoni Doe
  • 1,003
  • 2
  • 8
  • 4
  • What are you running between nginx and Django? gunicorn? Are you using supervisor? – Joseph Oct 08 '13 at 19:49
  • 1
    Have you set DEBUG = True in your settings.py file? If so, in the case that the errors look like http://i.imgur.com/TWL6f.png, then it is probably a django problem. If the 403 error is not a verbose message, then I would guess it is an nginx problem. If it is a Django problem, start your django server using the console, and go to the page which gives you the error. Hopefully the console output will be enough for you to figure out your issue. – user1876508 Oct 08 '13 at 19:58

3 Answers3

89

Logs are set in your settings.py file. A new, default project, looks like this:

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

By default, these don't create log files. If you want those, you need to add a filename parameter to your handlers

    'applogfile': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': os.path.join(DJANGO_ROOT, 'APPNAME.log'),
        'maxBytes': 1024*1024*15, # 15MB
        'backupCount': 10,
    },

This will set up a rotating log that can get 15 MB in size and keep 10 historical versions.

In the loggers section from above, you need to add applogfile to the handlers for your application

'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'APPNAME': {
            'handlers': ['applogfile',],
            'level': 'DEBUG',
        },
    }

This example will put your logs in your Django root in a file named APPNAME.log

Andy
  • 49,085
  • 60
  • 166
  • 233
  • 4
    do `logger = logging.getLogger('APPNAME')` in this case – Bob Yoplait Jan 04 '18 at 11:04
  • You have to add this line to each `views.py` – Baschdl Mar 22 '20 at 12:05
  • Interestingly, I have two sites both configured to log like this, and oddly one of them creates them owned by the uwsgi uid:gid and the other by root:root (causing access problems when uwsgi is later restarted). Wondering how and where that was/is configured. – Bernd Wechner Apr 02 '23 at 20:49
50

Add to your settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

And it will create a file called debug.log in the root of your. https://docs.djangoproject.com/en/1.10/topics/logging/

chezwhite
  • 700
  • 7
  • 8
  • 9
    On Apache2, wsgi will try to create a file in `/var/www/debug.log`. This will create 2 separate files if your Django project is outside `/var/www/` directory. Please use `'filename': os.path.join(BASE_DIR, 'debug.log'),` instead. – Suraj Apr 16 '17 at 16:25
  • Accepted answer didn't work for me. This works on Django 2 ! – caravana_942 Dec 04 '19 at 09:51
  • Im using django 3.1, and I get `Internal Server Error`. any new solution? – Shahriar.M Nov 24 '20 at 15:14
13

Setup https://docs.djangoproject.com/en/dev/topics/logging/ and then these error's will echo where you point them. By default they tend to go off in the weeds so I always start off with a good logging setup before anything else.

Here is a really good example for a basic setup: https://ian.pizza/b/2013/04/16/getting-started-with-django-logging-in-5-minutes/

Edit: The new link is moved to: https://github.com/ianalexander/ianalexander/blob/master/content/blog/getting-started-with-django-logging-in-5-minutes.html

sgauri
  • 694
  • 8
  • 18
slumtrimpet
  • 3,159
  • 2
  • 31
  • 44
  • 1
    Note that Ian renamed his domain to `ian.pizza`, so here's the actual link: https://ian.pizza/b/2013/04/16/getting-started-with-django-logging-in-5-minutes/ – Jan Jul 03 '16 at 08:54