diff --git a/app/app/log_filters.py b/app/app/log_filters.py new file mode 100644 index 00000000000..36c670d9237 --- /dev/null +++ b/app/app/log_filters.py @@ -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 . + +""" +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 diff --git a/app/app/settings.py b/app/app/settings.py index de7f7b33fa6..850b0b7056d 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -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'], @@ -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' @@ -259,7 +264,7 @@ 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', - 'formatter': 'verbose' + 'formatter': 'verbose', }, 'watchtower': { 'level': AWS_LOG_LEVEL, @@ -267,6 +272,7 @@ 'boto3_session': boto3_session, 'log_group': AWS_LOG_GROUP, 'stream_name': AWS_LOG_STREAM, + 'filters': ['host_filter'], 'formatter': 'cloudwatch', }, },