Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add logger configuration to allow prod exception handling (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
AetherUnbound authored Jan 17, 2022
1 parent aa95c54 commit 6a65899
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
82 changes: 82 additions & 0 deletions api/catalog/logger.py
Original file line number Diff line number Diff line change
@@ -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"},
},
}
25 changes: 2 additions & 23 deletions api/catalog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/

Expand Down

0 comments on commit 6a65899

Please sign in to comment.