Reputation: 1790
Maybe my question will seem silly, but I can't understand how to properly setup logging from celery tasks to Sentry.
For example, I have a module tasks.py
with two tasks: foo
and bar
.
I want to get logger inside task and send all logger.info
and logger.error
messages directly to Sentry, not to file.
Please tell me how it is done correctly, and if you want with some simple examples.
Upvotes: 2
Views: 5263
Reputation: 7032
Here is what I did and passes my smoke test. Place this at the top of one of your tasks.py
@setup_logging.connect
def project_setup_logging(loglevel, logfile, format, colorize, **kwargs):
import logging.config
from django.conf import settings
logging.config.dictConfigClass(settings.LOGGING).configure()
And my relevant settings.LOGGING
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'formatter': 'standard',
'stream': sys.stdout
},
'logfile': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': './django.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter': 'standard',
},
'sentry': {
'level': 'WARNING',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
},
'loggers': {
'django': {
'handlers': ['sentry'],
'propagate': True,
'level': 'ERROR',
},
'django.request': {
'handlers': ['sentry'],
'level': 'ERROR',
'propagate': False,
},
'django.db.backends': {
'handlers': ['sentry'],
'level': 'ERROR',
'propagate': False,
},
'raven': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'': {
'handlers': ['console', 'sentry', 'logfile'],
'level': 'INFO',
},
}
}
Then my test is this..
@celery.task()
def dummy_logging_test(**kwargs):
"""Call this as dummy_test.delay()"""
from .models import User
log = dummy_logging_test.get_logger()
if settings.DEBUG:
log.setLevel(logging.DEBUG)
log.debug("Debug DUMMY TEST")
log.info("Info DUMMY TEST")
log.warning("Warning DUMMY TEST")
log.error("Error DUMMY TEST")
log.critical("Critical DUMMY TEST")
try:
User.objects.get(id=9999999)
except Exception as err:
log.exception(err)
raise
Upvotes: 0
Reputation: 2553
look at sentry logging
or replace logging configuration in your settings.py with the following
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'console': {
'format': '[%(asctime)s][%(levelname)s] %(name)s %(filename)s:%(funcName)s:%(lineno)d | %(message)s',
'datefmt': '%H:%M:%S',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'console'
},
'sentry': {
'level': 'ERROR',
'class': 'raven.handlers.logging.SentryHandler',
'dsn': 'http://public:[email protected]/1',
},
},
'loggers': {
'': {
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
'your_app': {
'level': 'DEBUG',
'propagate': True,
},
'celery': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False,
},
}
}
Upvotes: 1