aboutsummaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-03-14 10:45:36 +0300
committerGravatar Hassan Abouelela <[email protected]>2021-03-15 02:16:24 +0300
commita423419eca285d0b23ad3f31956297851a608d22 (patch)
treeda008db807af16fc58c1910fad6367d4e91aef66 /backend
parentUse .gitattributes to normalise line endings (diff)
Adds Logging Support
Adds formatters and handlers for logging. Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'backend')
-rw-r--r--backend/__init__.py3
-rw-r--r--backend/constants.py2
-rw-r--r--backend/logs.py45
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)