Errors not being logged to txt file - django

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.

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,
},
},
}

Django unittest - How to disable console logging only

I have a test in django (v2.2) that generates an error on purpose to check that errors are logged in the proper log files.
But I get also a print in the console which is kind of annoying, I was looking for a way to hide the message.
If I disable logging with logging.disable(logging.CRITICAL), it will not log the error in the files.
I also tried to use with self.settings(LOGGING=logging) after removing the console handler from the loggers (see settings below) but it does not seem to be taken into account.
Also, setting disable_existing_logger to True does not help.
Do you have some ideas how to do that?
my settings:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'msg_filter': {
'()': MessageFilter,
'strings_to_be_ignored': [
'Not Found: /favicon.ico'
],
}
},
'formatters': {
'verbose': {
'format': '_____________________________________________________________'
'\n%(asctime)s - [%(levelname)s|%(name)s] %(message)s\n'
},
'simple': {
'format': '%(asctime)s - [%(levelname)s|%(name)s] %(message).50s'
},
},
'handlers': {
'warning': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': '/some_folder/warning.log',
'formatter': 'verbose',
'filters': ['msg_filter']
},
'warning_simple': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': '/some_folder/warning.log',
'formatter': 'simple',
'filters': ['msg_filter']
},
'info': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/some_folder/info.log',
'maxBytes': 1024 * 1024 * 2,
'backupCount': 9,
'formatter': 'simple',
'filters': ['msg_filter']
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'filters': ['msg_filter']
},
'console_simple': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'simple',
'filters': ['msg_filter']
}
},
'loggers': {
'': {
'handlers': ['console', 'info', 'warning'],
'level': 'DEBUG',
'propagate': False,
},
'django': {
'handlers': ['console_simple', 'info', 'warning_simple'],
'level': 'DEBUG',
'propagate': False,
}
}
}
You should set disable_existing_loggers variable True. This variable being true means the loggers is disabled.
Read this:
https://docs.djangoproject.com/en/3.0/topics/logging/#configuring-logging
you can add the --logging-clear-handlers option.
python manage.py test --logging-clear-handlers

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)

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