diff options
Diffstat (limited to 'backend')
-rw-r--r-- | backend/__init__.py | 3 | ||||
-rw-r--r-- | backend/constants.py | 2 | ||||
-rw-r--r-- | backend/logs.py | 45 |
3 files changed, 50 insertions, 0 deletions
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) |