Skip to content

Commit

Permalink
cleanup logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Lipert authored and danlipert committed Mar 19, 2019
1 parent 11e7825 commit b423f5b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 117 deletions.
185 changes: 76 additions & 109 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import environ
import raven
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

from boto3.session import Session
from easy_thumbnails.conf import Settings as easy_thumbnails_defaults

Expand Down Expand Up @@ -75,7 +78,6 @@
'autotranslate',
'django_extensions',
'easy_thumbnails',
'raven.contrib.django.raven_compat',
'health_check',
'health_check.db',
'health_check.cache',
Expand Down Expand Up @@ -131,7 +133,6 @@
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'app.middleware.drop_accept_langauge',
'django.middleware.locale.LocaleMiddleware',
Expand Down Expand Up @@ -255,67 +256,93 @@
RELEASE = raven.fetch_git_sha(os.path.abspath(os.pardir)) if ENV == 'prod' else ''
RAVEN_JS_VERSION = env.str('RAVEN_JS_VERSION', default='3.26.4')
if SENTRY_DSN:
sentry_sdk.init(
SENTRY_DSN,
integrations=[DjangoIntegration()]
)
RAVEN_CONFIG = {
'dsn': SENTRY_DSN,
}
if RELEASE:
RAVEN_CONFIG['release'] = RELEASE


LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'host_filter': {
'()': 'app.log_filters.HostFilter',
}
},
'root': {
'level': 'INFO',
'handlers': ['console'],
},
'formatters': {
'simple': {
'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
'django.db.backends': {
'level': 'INFO',
'handlers': ['console'],
'propagate': False,
},
'django.security.*': {
'handlers': ['console'],
'level': DEBUG and 'DEBUG' or 'INFO',
},
'django.request': {
'handlers': ['console'],
'level': DEBUG and 'DEBUG' or 'INFO',
},
},
}

# Production logging
if ENV not in ['local', 'test', 'staging', 'preview']:
# add AWS monitoring
boto3_session = Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION
)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'host_filter': {
'()': 'app.log_filters.HostFilter',
}
},
'root': {
'level': 'INFO',
'handlers': ['console', 'watchtower', ],
},
'formatters': {
'simple': {
'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'cloudwatch': {
'format': '%(hostname)s %(name)-12s [%(levelname)-8s] %(message)s',
},
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'loggging.StreamHandler',
'formatter': 'verbose',
},
'watchtower': {
'level': AWS_LOG_LEVEL,
'class': 'watchtower.django.DjangoCloudWatchLogHandler',
'boto3_session': boto3_session,
'log_group': AWS_LOG_GROUP,
'stream_name': AWS_LOG_STREAM,
'filters': ['host_filter'],
'formatter': 'cloudwatch',
},
},
'loggers': {
'django.db.backends': {
'level': AWS_LOG_LEVEL,
'handlers': ['console', 'watchtower'],
'propagate': False,
},
},

LOGGING['formatters']['cloudwatch'] = {
'format': '%(hostname)s %(name)-12s [%(levelname)-8s] %(message)s',
}
LOGGING['handlers']['watchtower'] = {
'level': AWS_LOG_LEVEL,
'class': 'watchtower.django.DjangoCloudWatchLogHandler',
'boto3_session': boto3_session,
'log_group': AWS_LOG_GROUP,
'stream_name': AWS_LOG_STREAM,
'filters': ['host_filter'],
'formatter': 'cloudwatch',
}
LOGGING['root']['handlers'].push('watchtower')
LOGGING['loggers']['django.db.backends']['handlers'].push('watchtower')
LOGGING['loggers']['django.db.backends']['level'] = AWS_LOG_LEVEL

LOGGING['loggers']['django.request'] = LOGGING['loggers']['django.db.backends']
LOGGING['loggers']['django.security.*'] = LOGGING['loggers']['django.db.backends']
for ia in INSTALLED_APPS:
LOGGING['loggers'][ia] = LOGGING['loggers']['django.db.backends']

# add elasticsearch monitoring
if ENABLE_APM:
LOGGING['handlers']['elasticapm'] = {
'level': 'WARNING',
Expand All @@ -328,66 +355,6 @@
}
LOGGING['root']['handlers'] = ['sentry', 'elasticapm']

LOGGING['loggers']['django.request'] = LOGGING['loggers']['django.db.backends']
LOGGING['loggers']['django.security.*'] = LOGGING['loggers']['django.db.backends']
for ia in INSTALLED_APPS:
LOGGING['loggers'][ia] = LOGGING['loggers']['django.db.backends']
else:
LOGGING = {}

if SENTRY_DSN:
if ENV == 'prod':
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'host_filter': {
'()': 'app.log_filters.HostFilter',
}
},
'formatters': {
'simple': {
'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
'sentry': {
'level': 'INFO', # To capture more than ERROR, change to WARNING, INFO, etc.
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
},
'loggers': {
'django.db.backends': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'root': {
'level': 'WARNING',
'handlers': ['sentry'],
},
'raven': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
},
}

GEOIP_PATH = env('GEOIP_PATH', default='/usr/share/GeoIP/')

# Static files (CSS, JavaScript, Images)
Expand Down
4 changes: 1 addition & 3 deletions docs/ENVIRONMENT_VARIABLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ For further information, please check out the [Gitcoin Bot Documentation](https:
| Variable | Description | Type | Default |
| --- | --- | --- | --- |
| SENTRY_DSN | The [Sentry](https://sentry.io) DSN. | `str` | '' |
| SENTRY_TOKEN | A bearer token with project:releases permissions. | `str` | '' |
| SENTRY_ORG | The name of your Sentry organization. | `str` | '' |
| SENTRY_PROJECT | The name of your Sentry project. | `str` | '' |
| SENTRY_JS_DSN | The DSN for Javascript Sentry reporting (defaults to SENTRY_DSN). | `str` | '' |

## SendGrid

Expand Down
5 changes: 1 addition & 4 deletions docs/THIRD_PARTY_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,5 @@ If you would like to track errors of your local environment, setup an account at
Once you have access to your project secrets, you can enable Sentry error tracking for both the backend and frontend by adding the following environment variables to `web/app/app/.env`:

```shell
SENTRY_USER=xxx
SENTRY_PASSWORD=xxx
SENTRY_ADDRESS=https://sentry.example.xxx
SENTRY_PROJECT=2
SENTRY_DSN=xxx
```
3 changes: 2 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ ecdsa
pysha3
oyaml
pygithub==1.43.2
raven==6.9.0
django-cacheops==4.0.7
django-redis==4.10.0
collectfast==0.6.2
Expand All @@ -80,3 +79,5 @@ redis-semaphore
watchtower==0.5.4
Wand==0.4.4
wagtail==2.3
raven==6.9.0
sentry-sdk

0 comments on commit b423f5b

Please sign in to comment.