aboutsummaryrefslogtreecommitdiffstats
path: root/snekbox/utils/logging.py
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2022-06-01 11:09:30 -0700
committerGravatar GitHub <[email protected]>2022-06-01 11:09:30 -0700
commitb0fb2bf8e52f9e340a8f77503c68daafeed4d41d (patch)
tree132d3b703c1a29f5cc6de484d6317220ffc94da5 /snekbox/utils/logging.py
parentMerge #139 - use pip-tools instead of Pipenv (diff)
parentFix typo in Dockerfile comment (diff)
Merge #140 - add pyproject.toml and versioning
Diffstat (limited to '')
-rw-r--r--snekbox/utils/logging.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/snekbox/utils/logging.py b/snekbox/utils/logging.py
new file mode 100644
index 0000000..c15e3f1
--- /dev/null
+++ b/snekbox/utils/logging.py
@@ -0,0 +1,35 @@
+import logging
+import os
+import sys
+
+__all__ = ("FORMAT", "init_logger", "init_sentry")
+
+FORMAT = "%(asctime)s | %(process)5s | %(name)30s | %(levelname)8s | %(message)s"
+
+
+def init_logger(debug: bool) -> None:
+ """Initialise the root logger with a handler that outputs to stdout."""
+ log = logging.getLogger("snekbox")
+ log.setLevel(logging.DEBUG if debug else logging.INFO)
+ log.propagate = True
+
+ formatter = logging.Formatter(FORMAT)
+ handler = logging.StreamHandler(sys.stdout)
+ handler.setFormatter(formatter)
+ log.addHandler(handler)
+
+
+def init_sentry(version: str) -> None:
+ """Initialise the Sentry SDK if it's installed."""
+ try:
+ import sentry_sdk
+ from sentry_sdk.integrations.falcon import FalconIntegration
+ except ImportError:
+ return
+
+ sentry_sdk.init(
+ dsn=os.environ.get("SNEKBOX_SENTRY_DSN", ""),
+ integrations=[FalconIntegration()],
+ send_default_pii=True,
+ release=f"snekbox@{version}"
+ )