Dajax log warning - django

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

Related

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.

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

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)

Logging in Django

I'm trying to figure out logging in Django (checking out Python logging itself for the first time too for that matter).
I have the following in my settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/home/afzalsh/logs/debug.log',
},
},
'loggers': {
'django.request': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
This is the exact code as what Django documentation has as the first example.
Then I have a log message in a class based view.
import logging
.
.
.
class StationHome(View):
.
.
.
def post(self,request, pk):
station = Station.objects.get(pk=pk)
form = StationForm(request.POST, instance=station)
if form.is_valid():
logger = logging.getLogger(__name__)
logger.error("Yep!")
form.save()
return HttpResponseRedirect(reverse("home_station",
kwargs={'pk':pk},
)
)
else:
return HttpResponse("Form Invalid")
.
.
.
The debug.log file has got created but it doesn't get populated with any message on going through the particular code. The expected process do happen.
You've requested the logger associated with __name__, so you get one named the same as the current Python module. However, the only one you've actually configured is "django.request". You should use that in your call to getLogger instead.
In your settings your handler dict contains level 'debug' for file where as in your code you are using 'error' level to log.
logger.error("Yep!")
Try using 'debug' level in your code. Something like this:
logger.debug("Yep!")
also posting my settings here so that you can use that as a reference.
{
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard' : {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'console':{
'level': 'DEBUG',
'class': 'logging.StreamHandler'
},
'null' : {
'level':'DEBUG',
'class':'django.utils.log.NullHandler',
},
'file' : {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'debug.log',
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'django': {
'handlers': ['file'],
'propogate': True,
'level': 'DEBUG',
},
'matchroom_app':{
'handlers' : ['file'],
'level': 'DEBUG',
},
'matchroom_post':{
'handlers' : ['file'],
'level' : 'DEBUG',
},
'matchroom_social':{
'handlers': ['file'],
'level' : 'DEBUG',
},
}
}

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

Why does my Django 1.3 logging setup cause all messages to be output twice?

My Django 1.3 logging setup causes all log messages to be output twice. I've read that importing settings.py two or more times has caused this problem in the past, but Django 1.3 has new logging features, and I don't think I'm importing settings.py twice anywhere.
settings.py config:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'custom': {
'handlers': ['console'],
'level': 'DEBUG',
},
}
}
Code:
import logging
log = logging.getLogger('custom')
log.debug('message1')
log.debug('message2')
Output:
DEBUG: message1
DEBUG:custom:message1
Thanks for your help.
Have you tried setting propagate = False? Along with disable_existing_loggers = True?
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(levelname)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'custom': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
}
}
You might want to try 'disable_existing_loggers': True?
I've been suffered by the same issue. And I fixed it by redirecting the root logs to a file, and project logs to both the file and the console.
I grep my code, and could not find anywhere basicConfig() exists, also tried to set disable_existing_loggers to True, It doesn't help, finally solved the problem by set a file logger. I guess it maybe a problem by design in some cases.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
},
},
'handlers': {
'console': {
#'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
# Add Handler for mail_admins for `warning` and above
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
},
'file': {
#'level': 'INFO',
'class': 'logging.FileHandler',
'formatter': 'simple',
'filename': os.path.join(os.path.dirname(PROJECT_ROOT), 'crawler.admin.log'),
},
},
'loggers': {
# root logger
'': {
'level': 'INFO',
'handlers': ['file', 'mail_admins'],
},
'scrapy': {
'level': 'WARNING',
'handlers': ['console', 'mail_admins'],
'propagate': False,
},
'crawleradmin': {
'level': 'INFO',
'handlers': ['console', 'file', 'mail_admins'],
# required to avoid double logging with root logger
'propagate': False,
},
},
}