Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostfilter for CW logging #2769

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/app/log_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""Define custom log filters for Gitcoin logging.

Copyright (C) 2018 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
import logging
import socket


class HostFilter(logging.Filter):
"""Define the logging filter to add the current hostname to log output."""

hostname = socket.gethostname()

def filter(self, record):
"""Add the HostFilter derived hostname to log output."""
record.hostname = HostFilter.hostname
return True
10 changes: 8 additions & 2 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'host_filter': {
'()': 'app.log_filters.HostFilter',
}
},
'root': {
'level': 'WARNING',
'handlers': ['sentry', 'console', 'watchtower'],
Expand All @@ -245,7 +250,7 @@
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'cloudwatch': {
'format': '%(name)-12s [%(levelname)-8s] %(message)s',
'format': '%(hostname)s %(name)-12s [%(levelname)-8s] %(message)s',
},
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
Expand All @@ -259,14 +264,15 @@
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
'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',
},
},
Expand Down