How to exclude 404 error messages from logging to email? - django

Good afternoon. I have this type of logs in the settings:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
},
'mail_admins': {
'level': 'WARNING',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
},
},
'loggers': {
'': {
'handlers': ['console', 'mail_admins'],
'level': 'DEBUG',
'propagate': False,
},
}
}
I receive all messages of the level of warning and above in the email.
Due to various robots, spammers or other users looking for hidden files and folders, I get a lot of 404 (page not found) error messages.
How can I stop receiving information about 404 errors?

This is thanks to the mail_admins handler, that sends you any WARNING level and above error by email, see doc
As suggested in docs, you can subclass this handler and use your new custom handler instead.
class MyCustomHandler(AdminEmailHandler):
"""Custom Handler that ignores 404 errors instead of sending them by email"""
def handle(self, record):
if record.exc_info:
exc_type, exc_value, tb = record.exc_info
if isinstance(exc_value, Http404):
return
super().handle(record)

Related

Django not logging all errors on production

My Django app is only properly logging and emailing errors when Debug=True.
On production, I'm only getting 'Invalid HTTP_HOST header' errors in both the log file and my inbox. Every other error is not logged in the django log nor is it emailed to me.
I've tried some things with this test request:
def test(request):
logging.debug('debug test message') // Always appears in log file
from django.core.mail import mail_admins
mail_admins('test error', 'test error via call to mail_admins()') // Always arrives in my inbox just fine
raise Appointment.DoesNotExist // When Debug=False (both locally and in production), does not appear in log file or in inbox.
My logging settings for both local and production:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'csv_formatter': {
'format': '%(levelname)s;%(asctime)s;%(message)s'
}
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'maxBytes': 1024 * 1024 * 50,
'backupCount': 10,
'formatter': 'csv_formatter',
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'verbose',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True,
},
'asyncio': {
'level': 'WARNING',
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'django.security': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
How do I make sure all Django errors are logged and emailed in production (when Debug=False)?

Can't get Django to send me debug mail

I have a Django site that I have deployed to Heroku, and I'm having trouble getting it to send me error notifications via email. I've logged into the Heroku console and used django.core.mail.send_mail() to send emails, so I know it's configured to send correctly.
I've got ADMINS and MANAGERS configured, and the django.middleware.common.BrokenLinkEmailsMiddleware Middleware so I should be getting emails whenever someone gets a 404 error on the page. I have the following Logging configuration in my settings.py:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': ('%(asctime)s [%(process)d] [%(levelname)s] ' +
'pathname=%(pathname)s lineno=%(lineno)s ' +
'funcname=%(funcName)s %(message)s'),
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'simple': {
'format': '%(levelname)s %(message)s'
}
},
'handlers': {
'mail_admins': {
'level': 'DEBUG',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
},
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'testlogger': {
'handlers': ['console'],
'level': 'INFO',
}
}
}
What am I missing?

Sentry server not responding on Celery logs

I have a problem related with sending log messages to Sentry server while using Django and Celery async tasks. I utilize Django 1.11, Raven 6.1, Celery 4.0.2.
The log message shows in Sentry server when task is launching synchronous, for example:
#celery.shared_task()
def test_worker():
logger.warning('Logger works!')
raise Exception('---Worker works!')
$ from api.tasks import test_worker
$ test_worker()
When trying launch async task using:
$ res = test_worker.delay()
Sentry server does not contain log about logger.
My settings.py is:
RAVEN_CONFIG = {
'dsn': 'http://my_dsn',
}
SENTRY_AUTO_LOG_STACKS = True
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'DEBUG',
'handlers': ['sentry'],
},
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s - %(message)s'
},
'verbose': {
'format': '%(asctime)s [%(levelname)s] %(module)s:%(funcName)s - %(message)s'
},
},
'handlers': {
# logs directly to Sentry
'sentry': {
'level': 'DEBUG',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
# logs everything to the console
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
'raven': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'processing': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False
},
'celery': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False
}
},
}
Where I made a mistake, or what I am doing wrong?
Thanks in advance.

Django Raven only logs once to Sentry

I have set up my logging to go to Sentry in my app. When I run ./manage.py raven test, it works every time. When I call the log in lets say a view, it works once. After that it stops logging at all. There are no errors or output at all.
The weird thing is if remove the logging settings and set the logger as:
Logger = logging.getLogger('django.request')
It works every time
settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'DEBUG',
'handlers': ['sentry'],
},
'formatters': {
'verbose': {
'format': ('%(levelname)s %(asctime)s %(module)s %(process)d '
'%(thread)d %(message)s')
},
},
'handlers': {
'sentry': {
'level': 'DEBUG',
'class': ('raven.contrib.django.raven_compat.handlers.'
'SentryHandler'),
},
},
'loggers': {
'log': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False
},
},
}
views.py:
import logging
Logger = logging.getLogger('log')
def first_view(request):
Logger.error('First Error Works', exc_info=True)
def second_view(request):
Logger.error('Second Error Does Not', exc_info=True)

Logging in Django with raven and sentry

I may be a bit late on the train, but I wanted to use Sentry and Raven for logging in Django.
I set up sentry and raven to the point, where I ran the test for raven and it works.
So now I want to send my debug messages over to sentry, but how would I do this?
settings.py
RAVEN_CONFIG = {
'dsn': 'http://code4#mydomain:9000/2',
# If you are using git, you can also automatically configure the
# release based on the git info.
'release': raven.fetch_git_sha(BASE_DIR),
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'sentry': {
'level': 'WARNING',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
'console': {
'level': 'WARNING',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'django': {
'handlers': ['sentry'],
'level': 'DEBUG',
'propagate': True,
},
'raven': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False,
},
}
}
view.py
import logger
from raven.contrib.django.models import get_client
client = get_client()
client.captureException()
logger = logging.getLogger(__name__)
def my_view(request):
[...]
logger.error('There was some crazy error', exc_info=True, extra={
# Optionally pass a request and we'll grab any information we can
'request': request,
})
[...]
At this point it just logs errors and exceptions but wont send me this error message...
How is the right way to use raven and sentry? The docs are totaly not helping and my google foo left me also. Any tips or helpfull tutorials?
You have 3 loggers defined: django, raven and sentry.errors. When you call logging.getLogger(__name__) you actually create a "throw-away" one, because your ___name__ doesn't match any of above.
You should either use the raven logger...
logger = logging.getLogger('raven')
logger.debug('Hola!')
...or setup your own:
LOGGING = {
# ...
'loggers': {
# ...
'yourapp': {
'level': 'debug',
'handlers': ['sentry'],
'propagate': False,
},
}
}
and then in yourapp/views.py:
logger = logging.getLogger(__name__)
logger.debug('Hola!')