23👍
Here is a full working logging configuration. Critical errors are logged to sentry, warnings are sent to admins by emails, normal notice errors are logged to syslog, and debug messages are prompted on the standard output.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'formatters': {
'verbose': {
'format': '[contactor] %(levelname)s %(asctime)s %(message)s'
},
},
'handlers': {
# Send all messages to console
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
# Send info messages to syslog
'syslog':{
'level':'INFO',
'class': 'logging.handlers.SysLogHandler',
'facility': SysLogHandler.LOG_LOCAL2,
'address': '/dev/log',
'formatter': 'verbose',
},
# Warning messages are sent to admin emails
'mail_admins': {
'level': 'WARNING',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler',
},
# critical errors are logged to sentry
'sentry': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'raven.contrib.django.handlers.SentryHandler',
},
},
'loggers': {
# This is the "catch all" logger
'': {
'handlers': ['console', 'syslog', 'mail_admins', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
}
}
20👍
Django uses a logging filter to decide wither the console
handler is used or not by default. See also django.utils.log on Github.
To keep the same behavior without filtering messages to console, just disable filtering in your settings.py
like this:
from django.utils.log import DEFAULT_LOGGING
DEFAULT_LOGGING['handlers']['console']['filters'] = []
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-POST jQuery array to Django
5👍
Here’s another way of doing it with complexity half-way between the other answers, logs both to console and file
# settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'log.django',
},
},
'loggers': {
'django': {
'handlers': ['console','file'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-Substring in a django template?
- [Django]-Django: guidelines for speeding up template rendering performance
4👍
Here is the minimal valid Django LOGGING
configuration snippet required to log exception tracebacks to stderr.
Put this at the end of your settings.py
:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"loggers": {
"django": {"handlers": ["console"], "level": "INFO"},
},
}
- [Django]-Substring in a django template?
- [Django]-Django character set with MySQL weirdness
- [Django]-Http POST drops port in URL
0👍
Use ExeptBot for a log stored in your database and accessible within your site. A plus is it hooks into ChatGPT to give you quick suggestions for how to fix the exceptions.
To add it to your site:
- Install the ExceptBot library
pip3 install exceptbot
- Add ‘ExceptBot’ to INSTALLED_APPS in your project’s
settings.py
:
INSTALLED_APPS = [
# ...
'exceptbot',
# ...
]
- Include the middleware in the MIDDLEWARE list in your
settings.py
:
MIDDLEWARE = [
# ...
'exceptbot.middleware.ExceptBotMiddleware',
# ...
]
- Include the ExceptBot URLs in your project’s
urls.py
:
from django.urls import path, include
urlpatterns = [
# ...
path('', include('exceptbot.urls')),
# ...
]
Run migrations to create the necessary database tables and set up static files
$ python3 manage.py makemigrations exceptbot
$ python3 manage.py migrate exceptbot
$ python3 manage.py collectstatic --no-input
- [Django]-Substring in a django template?
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-POST jQuery array to Django