How to *always* log exceptions and stacktraces in django with DEBUG = False - django

How do I set DEBUG = False, but ensure exceptions (wherever they may be thrown) are properly logged, including a stack trace.
Here is my logging configuration:
LOGGING = {
'version': 1,
'filters': {
'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'},
'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue'}
},
'formatters': {
'django.server': {
'()': 'django.utils.log.ServerFormatter',
'format': '[%(server_time)s] %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'filters': ['require_debug_true'],
'level': 'DEBUG'
},
'django.server': {
'class': 'logging.StreamHandler',
'formatter': 'django.server',
'level': 'INFO'
},
'mail_admins': {
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['require_debug_false'],
'level': 'ERROR'
}
},
'root': {
'level': 'DEBUG',
'handlers': ['console']
},
'loggers': {
'django.request': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG',
},
'django': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG',
},
'django.template': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'django.security': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
},
}

Though there is no default config where all exceptions are being recorded in a log file, there are couple of options for you to try out in django:
Configure the ADMINS setting, to receive emails about all exceptions in your site
You can write a custom middleware, that has a process_exception method that writes to the logger with logger.error('Exception info)

Related

How to log Django warnings and errors to log file in Production?

What I want to achieve is that warning and errors that happen in production (i.e. DEBUG=False) are logged into to a rotating log file.
I tried this
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'handlers': {
'file': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': 'staging.log',
},
},
'loggers': {
'': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
'django': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
'django.request': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
},
}
However, the above logs everything, not just messages of log level WARNING and above.
You can follow the django logger here.
In your views.py:
import logging
logger = logging.getLogger(__name__)
then you can record using logger.error() or logger.warning() or logger.info().
It will create a logger file in your main project directory and it will list out all the logging details.
See this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'logfile': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': "yourproject_name.log",
'maxBytes': 100000,
'backupCount': 2,
'formatter': 'verbose',
},
},
'loggers': {
'django': {
'handlers': ['logfile'],
'level': 'INFO',
'propagate': True,
},
'apps': {
'handlers': ['logfile'],
'level': 'DEBUG',
'propagate': True,
},
},
}

Elastic APM: How to turn off logging for django

flushing due to time since last flush 9.060s > max_flush_time 9.060s
I 'm getting tone of those message in django debug.
I tried changing to their default setting
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'elasticapm': {
'level': 'WARNING',
'class': 'elasticapm.contrib.django.handlers.LoggingHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'django.db.backends': {
'level': 'ERROR',
'handlers': ['console'],
'propagate': False,
},
'mysite': {
'level': 'WARNING',
'handlers': ['elasticapm'],
'propagate': False,
},
# Log errors from the Elastic APM module to the console (recommended)
'elasticapm.errors': {
'level': 'ERROR',
'handlers': ['console'],
'propagate': False,
},
},
}
Still getting lots of logs.
How to turn this off?

Errors not being logged to txt file

Hi have the following code in my setting.py file.
Upon a 500 error, an email gets sent out with the details of the error, an error.txt file gets created in the directory noted below, but the txt file is empty. No errors are being written to this txt file. Any thoughts?
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': '/home/jasonhoward/webapps/myproject/jason/errors.log'
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': False,
},
}
}
In your 'django.request' logger, add 'file', like this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': '/home/jasonhoward/webapps/myproject/jason/errors.log'
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins','file'],
'level': 'ERROR',
'propagate': False,
},
'django': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': False,
},
}
}
This error is a request error.

django-datables error - No handler could be found?

I am using the django-databtables-view app, but I get this following error in my console. what am I missing here?
No handlers could be found for logger "django_datatables_view.mixins"
I included the logging in my settings.py
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,
},
}
}
You should add this to your handlers definition
'null': {
'level':'DEBUG',
'class':'django.utils.log.NullHandler',
}
And this to your loggers
'django_datatables_view.mixins': {
'handlers': ['null'],
'level': 'ERROR',
'propagate': True,
},
You could also set some other handler that actually logs something, check out the answer here

Dajax log warning

I'm install both Dajaxice and Dajax with sucess (they both works)
But i get some warning in dev server log
The 'request' object must be accesible within the context. You must add 'django.contrib.messages.context_processors.request' to your TEMPLATE_CONTEXT_PROCESSORS and render your views using a RequestContext.
If i try to add to TEMPLATE_CONTEXT_PROCESSORS django.contrib.messages.context_processors.request
it will raise error about no context_processors.request in django.contrib.messages
Also i render my templates with context like
return render_to_response('root.html', {'news' : news, 'author' : author }, context_instance=RequestContext(request))
My log section in settings.py
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'
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'dajaxice': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
'dajaxice.DajaxiceRequest': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
}
}
You have to change
django.contrib.messages.context_processors.request
to
django.contrib.messages.context_processors.messages
It's messages not request