Logging in Django with raven and sentry - django

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!')

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)?

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)

Django logging not working in views

I have logging set-up in my Django 1.9 project. I am getting the logs in django_request.log automatically as expected but not in mylog.log when I use logging in views.py. Where am I wrong?
views.py
import logging
logr = logging.getLogger(__name__)
def sample_view(request):
logr.debug('Ran Sample view')
return HttpResponse("Done!")
settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/mylog.log',
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'standard'
},
'request_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/django_request.log',
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'standard'
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'django.request': {
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False
},
}
}
I found out this "If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the default) then all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger."
on https://docs.djangoproject.com/en/1.7/topics/logging/#configuring-logging
So if you set 'disable_existing_loggers': False, you will be able to see all logs.

Sentry logging in Django/Celery stopped working

I have no idea whats wrong. So far logging worked fine (and I was relying on that) but it seems to have stopped. I wrote a little test function (which does not work either):
core.tasks.py
import logging
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)
#app.task
def log_error():
logger.error('ERROR')
settings.py
INSTALLED_APPS += (
'raven.contrib.django.raven_compat',
)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'INFO', #If set to DEBUG, prints ALL DJANGO debug logs.
'handlers': ['console', 'sentry'],
},
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
#log everything to the console
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'simple'
},
#logs directly to sentry
'sentry': {
'level': 'ERROR',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
#'class': 'raven.contrib.django.handlers.SentryHandler', #I have tried both
},
},
'loggers': {
'django.request':{
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
'celery.task':{
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
#this is the logger for celery itself
'celery':{
'handlers': ['console', 'sentry'],
'level': 'ERROR',
'propagate': False,
},
},
}
from logging.config import dictConfig
dictConfig(LOGGING)
Executing the following in the Django shell logs to the console but it doesn't reach Sentry:
from core import tasks
tasks.log_error.delay()
It works when executing the task synchronously:
tasks.log_error()
Help!?
Django==1.6.2, raven==5.0.0, sentry==6.3.2, Python 2.7.3
I had to add
CELERYD_HIJACK_ROOT_LOGGER=False
to my Django settings.py.
I don't really understand why I explicitely have to tell celery to not hijack the root logger.