Skip to content

Commit

Permalink
Merge pull request #561 from openstax/logging
Browse files Browse the repository at this point in the history
Update Django logging to use Sentry
  • Loading branch information
edwoodward authored Mar 29, 2018
2 parents 10bd10a + 63116f9 commit b968451
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 26 deletions.
110 changes: 85 additions & 25 deletions openstax/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Django settings for openstax project.

import os
import raven
import logging.config
from django.utils.log import DEFAULT_LOGGING

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
BASE_DIR = PROJECT_ROOT
Expand Down Expand Up @@ -162,6 +165,7 @@
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'raven.contrib.django.raven_compat',
#'social.apps.django_app.default',
'social_django',
'storages',
Expand Down Expand Up @@ -223,45 +227,101 @@
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
# LOGGING = {
# 'version': 1,
# 'disable_existing_loggers': False,
# 'filters': {
# 'require_debug_false': {
# '()': 'django.utils.log.RequireDebugFalse'
# }
# },
# 'formatters': {
# 'simple': {
# 'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
# },
# },
# 'handlers': {
# 'mail_admins': {
# 'level': 'ERROR',
# 'filters': ['require_debug_false'],
# 'class': 'django.utils.log.AdminEmailHandler'
# },
# 'file': {
# 'level': 'ERROR',
# 'class': 'logging.FileHandler',
# 'filename': 'server.log',
# 'formatter': 'simple'
# },
# },
# 'loggers': {
# 'django.request': {
# 'handlers': ['mail_admins'],
# 'level': 'ERROR',
# 'propagate': True,
# },
# 'accounts.salesforce': {
# 'handlers': ['file'],
# 'level': 'ERROR',
# 'propagate': True,
# 'formatter': 'simple',
# },
# },
# }

LOGGING_CONFIG = None
LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper()
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'formatters': {
'simple': {
'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
'default': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
},
'django.server': DEFAULT_LOGGING['formatters']['django.server'],
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
# console logs to stderr
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
},
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': 'server.log',
'formatter': 'simple'
# Add Handler for Sentry for `warning` and above
'sentry': {
'level': 'WARNING',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
'django.server': DEFAULT_LOGGING['handlers']['django.server'],
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
# default for all undefined Python modules
'': {
'level': 'WARNING',
'handlers': ['console', 'sentry'],
},
# Our application code
'openstax': {
'level': LOGLEVEL,
'handlers': ['console', 'sentry'],
# Avoid double logging because of root logger
'propagate': False,
},
'accounts.salesforce': {
'handlers': ['file'],
# Prevent noisy modules from logging to Sentry
'noisy_module': {
'level': 'ERROR',
'propagate': True,
'formatter': 'simple',
'handlers': ['console'],
'propagate': False,
},
# Default runserver request logging
'django.server': DEFAULT_LOGGING['loggers']['django.server'],
},
})
# this is what sends tracebacks to Sentry
RAVEN_CONFIG = {
'dsn': 'https://2e1ecafc60684f86b59c654de3032d83:[email protected]/11',
# If you are using git, you can also automatically configure the
# release based on the git info.
'release': raven.fetch_git_sha(os.path.dirname(os.pardir)),
}


Expand Down
2 changes: 1 addition & 1 deletion openstax/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Disable Python Social Auth Warnings
LOGGING['disable_existing_loggers'] = True
LOGGING_CONFIG = None

# Cloudfront static file settings
DEFAULT_FILE_STORAGE = 'storages.S3Storage.S3Storage'
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Pillow==4.3.0
psycopg2==2.7.3.2
pytz==2018.3
python-dateutil==2.4.2
raven==6.6.0
requests==2.18.4
six==1.11.0
sphinx-me==0.3
Expand Down

0 comments on commit b968451

Please sign in to comment.