From ab84bcb598b44e8c45181970db7197f637e177cb Mon Sep 17 00:00:00 2001 From: Mark Beacom Date: Mon, 12 Nov 2018 10:28:47 -0500 Subject: [PATCH] Adjust django logging to output to CW --- app/app/settings.py | 47 +++++++++++++++++++++++++++++++++---------- requirements/base.txt | 2 +- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index 6c49931d819..de7f7b33fa6 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -25,6 +25,7 @@ import environ import raven +from boto3.session import Session from easy_thumbnails.conf import Settings as easy_thumbnails_defaults import warnings @@ -218,18 +219,36 @@ if DEBUG and ENV == 'stage': ELASTIC_APM['DEBUG'] = True +AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID', default='') +AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY', default='') +AWS_DEFAULT_REGION = env('AWS_DEFAULT_REGION', default='us-west-2') +AWS_LOG_GROUP = env('AWS_LOG_GROUP', default='Gitcoin') +AWS_LOG_LEVEL = env('AWS_LOG_LEVEL', default='DEBUG') +AWS_LOG_STREAM = env('AWS_LOG_STREAM', default=f'{ENV}-web') + if ENV not in ['local', 'test', 'staging', 'preview']: + 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, 'root': { 'level': 'WARNING', - 'handlers': ['sentry'], + 'handlers': ['sentry', 'console', 'watchtower'], }, 'formatters': { + 'simple': { + 'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S' + }, + 'cloudwatch': { + 'format': '%(name)-12s [%(levelname)-8s] %(message)s', + }, 'verbose': { - 'format': '%(levelname)s %(asctime)s %(module)s ' - '%(process)d %(thread)d %(message)s' + 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' }, }, 'handlers': { @@ -241,22 +260,30 @@ 'level': 'DEBUG', 'class': 'logging.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, + 'formatter': 'cloudwatch', + }, }, 'loggers': { 'django.db.backends': { - 'level': 'WARN', - 'handlers': ['console'], + 'level': 'WARNING', + 'handlers': ['console', 'watchtower'], 'propagate': False, }, 'raven': { 'level': 'DEBUG', - 'handlers': ['console'], + 'handlers': ['console', 'watchtower'], 'propagate': False, }, 'sentry.errors': { 'level': 'DEBUG', - 'handlers': ['console'], + 'handlers': ['console', 'watchtower'], 'propagate': False, }, }, @@ -275,6 +302,7 @@ 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: @@ -551,9 +579,6 @@ IGNORE_COMMENTS_FROM = ['gitcoinbot', ] # optional: only needed if you run the activity-report management command -AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID', default='') -AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY', default='') - AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME', default='') AWS_S3_CACHE_MAX_AGE = env.str('AWS_S3_CACHE_MAX_AGE', default='15552000') AWS_QUERYSTRING_EXPIRE = env.int('AWS_QUERYSTRING_EXPIRE', default=3600) diff --git a/requirements/base.txt b/requirements/base.txt index 2bd3559bb96..7dceeb5fe64 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -77,4 +77,4 @@ pydoc-markdown==2.0.4 oauth2client==4.1.3 pyvips==2.1.3 redis-semaphore - +watchtower==0.5.4