From a423419eca285d0b23ad3f31956297851a608d22 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> Date: Sun, 14 Mar 2021 10:45:36 +0300 Subject: Adds Logging Support Adds formatters and handlers for logging. Signed-off-by: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> --- .dockerignore | 1 + .gitignore | 3 +++ backend/__init__.py | 3 +++ backend/constants.py | 2 ++ backend/logs.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 backend/logs.py diff --git a/.dockerignore b/.dockerignore index b87d345..6d587e9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -9,3 +9,4 @@ __pycache__ .github .gitlab .gitignore +logs/ diff --git a/.gitignore b/.gitignore index f2bc6c8..4b518e7 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,6 @@ dmypy.json # VSCode Settings .vscode/settings.json + +# Logs +logs/ diff --git a/backend/__init__.py b/backend/__init__.py index 220b457..f8b0afe 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -1,3 +1,5 @@ +import logging + import sentry_sdk from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from starlette.applications import Starlette @@ -6,6 +8,7 @@ from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.cors import CORSMiddleware from backend import constants +from backend import logs # This has to be imported before other logging statements from backend.authentication import JWTAuthenticationBackend from backend.middleware import DatabaseMiddleware, ProtectedDocsMiddleware from backend.route_manager import create_route_map diff --git a/backend/constants.py b/backend/constants.py index d90fd9a..50c09ea 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -15,6 +15,8 @@ SNEKBOX_URL = os.getenv("SNEKBOX_URL", "http://snekbox.default.svc.cluster.local PRODUCTION = os.getenv("PRODUCTION", "True").lower() != "false" PRODUCTION_URL = "https://forms.pythondiscord.com" +LOG_FILES = os.getenv("LOG_FILES", "False").lower() != "false" + OAUTH2_CLIENT_ID = os.getenv("OAUTH2_CLIENT_ID") OAUTH2_CLIENT_SECRET = os.getenv("OAUTH2_CLIENT_SECRET") OAUTH2_REDIRECT_URI = os.getenv( diff --git a/backend/logs.py b/backend/logs.py new file mode 100644 index 0000000..6b4a300 --- /dev/null +++ b/backend/logs.py @@ -0,0 +1,45 @@ +""" +Patch the uvicorn, watchgod, and global loggers. +""" +import logging +import sys +from logging import handlers +from pathlib import Path + +from backend import constants + +# Setup constants +LOGGING_LEVEL = logging.INFO if constants.PRODUCTION else logging.DEBUG +FORMATTER = logging.Formatter("%(asctime)s | %(name)s | %(levelname)s | %(message)s") + +# Setup the project logger +handler = logging.StreamHandler(sys.stdout) +handler.setFormatter(FORMATTER) + +logger = logging.getLogger("backend") +logger.setLevel(LOGGING_LEVEL) +logger.addHandler(handler) + +# Format uvicorn logging +try: + logging.getLogger("uvicorn").handlers[0].setFormatter(FORMATTER) +except KeyError: + logger.warning("Could not patch uvicorn logger, continuing.") + +# Add file handlers for local development +if constants.LOG_FILES: + # Setup uvicorn handler + uvicorn_log = Path("logs", ".uvicorn.log") + uvicorn_log.parent.mkdir(exist_ok=True) + # Store two copies of 100KB Files + uvicorn_handler = handlers.RotatingFileHandler(uvicorn_log, maxBytes=100000, backupCount=1) + uvicorn_handler.setFormatter(FORMATTER) + + # Setup app handler + backend_log = Path("logs", ".backend.log") + file_handler = logging.FileHandler(backend_log) + file_handler.setFormatter(FORMATTER) + + # Add all handlers + logging.getLogger("uvicorn").addHandler(uvicorn_handler) + logger.addHandler(file_handler) -- cgit v1.2.3