From 3ef62be88b83dc48b08ceaeeebf7bad6f6f99895 Mon Sep 17 00:00:00 2001 From: Madison Swain-Bowden Date: Fri, 7 Jan 2022 15:23:49 -0800 Subject: [PATCH] Add logger configuration to allow prod exception handling --- api/catalog/logger.py | 82 +++++++++++++++++++++++++++++++++++++++++ api/catalog/settings.py | 25 +------------ 2 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 api/catalog/logger.py diff --git a/api/catalog/logger.py b/api/catalog/logger.py new file mode 100644 index 000000000..023816f3a --- /dev/null +++ b/api/catalog/logger.py @@ -0,0 +1,82 @@ +from logging import LogRecord + + +def health_check_filter(record: LogRecord) -> bool: + # Filter out health checks from the logs, they're verbose and happen frequently + return not ("GET /healthcheck" in record.getMessage() and record.status_code == 200) + + +# Logging configuration +LOGGING = { + # NOTE: Most of this is inherited from the default configuration + "version": 1, + "disable_existing_loggers": False, + "filters": { + "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}, + "require_debug_true": {"()": "django.utils.log.RequireDebugTrue"}, + "health_check": { + "()": "django.utils.log.CallbackFilter", + "callback": health_check_filter, + }, + }, + "formatters": { + "django.server": { + "()": "django.utils.log.ServerFormatter", + "format": "[{server_time}] {message}", + "style": "{", + }, + "console": { + "format": "[%(asctime)s - %(name)s - %(lineno)3d][%(levelname)s] %(message)s", # noqa + }, + }, + "handlers": { + # Default console logger + "console": { + "level": "INFO", + "filters": ["require_debug_true"], + "class": "logging.StreamHandler", + "formatter": "console", + }, + # Add a clause to log error messages to the console in production + "console_prod": { + "level": "WARNING", + "filters": ["require_debug_false"], + "class": "logging.StreamHandler", + "formatter": "console", + }, + # Handler for all other logging + "general_console": { + "level": "INFO", + "class": "logging.StreamHandler", + "formatter": "console", + }, + # Default server logger + "django.server": { + "level": "INFO", + "class": "logging.StreamHandler", + "formatter": "django.server", + }, + # Default mailing logger + "mail_admins": { + "level": "ERROR", + "filters": ["require_debug_false"], + "class": "django.utils.log.AdminEmailHandler", + }, + }, + "loggers": { + "django": { + "handlers": ["console", "console_prod", "mail_admins"], + "level": "INFO", + "propagate": False, + }, + "django.server": { + "handlers": ["django.server"], + # Filter health check logs + "filters": ["health_check"], + "level": "INFO", + "propagate": False, + }, + # Default handler for all other loggers + "": {"handlers": ["general_console"], "level": "INFO"}, + }, +} diff --git a/api/catalog/settings.py b/api/catalog/settings.py index 7a465269c..6ead0abaf 100644 --- a/api/catalog/settings.py +++ b/api/catalog/settings.py @@ -17,6 +17,8 @@ from decouple import config from sentry_sdk.integrations.django import DjangoIntegration +from .logger import LOGGING # noqa: F401 + # Build paths inside the project like this: BASE_DIR.join('dir', 'subdir'...) BASE_DIR = Path(__file__).resolve().parent.parent @@ -248,29 +250,6 @@ }, ] -LOGGING = { - "version": 1, - "disable_existing_loggers": False, - "handlers": { - "console": { - "level": "INFO", - "class": "logging.StreamHandler", - }, - }, - "loggers": { - "django": { - "handlers": ["console"], - "level": "INFO", - "propagate": True, - }, - # root logger - "": { - "level": "INFO", - "handlers": ["console"], - }, - }, -} - # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/