diff options
author | 2022-05-30 19:57:45 -0700 | |
---|---|---|
committer | 2022-05-30 19:57:45 -0700 | |
commit | 8f62c90a96e3634c2d41cda96afefaff5c1fb54d (patch) | |
tree | 07f9a21a57306267a864123558b1a00ed32bd680 | |
parent | Move coverage config into pyproject.toml (diff) |
Move logging code to separate utility modules
-rw-r--r-- | config/gunicorn.conf.py | 2 | ||||
-rw-r--r-- | snekbox/__init__.py | 49 | ||||
-rw-r--r-- | snekbox/utils/__init__.py | 4 | ||||
-rw-r--r-- | snekbox/utils/gunicorn.py | 33 | ||||
-rw-r--r-- | snekbox/utils/logging.py | 33 |
5 files changed, 72 insertions, 49 deletions
diff --git a/config/gunicorn.conf.py b/config/gunicorn.conf.py index 5ab11f4..5812c07 100644 --- a/config/gunicorn.conf.py +++ b/config/gunicorn.conf.py @@ -1,5 +1,5 @@ workers = 2 bind = "0.0.0.0:8060" -logger_class = "snekbox.GunicornLogger" +logger_class = "snekbox.utils.gunicorn.GunicornLogger" access_logformat = "%(m)s %(U)s%(q)s %(s)s %(b)s %(L)ss" access_logfile = "-" diff --git a/snekbox/__init__.py b/snekbox/__init__.py index 42628d4..1525ebd 100644 --- a/snekbox/__init__.py +++ b/snekbox/__init__.py @@ -1,51 +1,8 @@ -import logging import os -import sys -import sentry_sdk -from gunicorn import glogging -from gunicorn.config import Config -from sentry_sdk.integrations.falcon import FalconIntegration +from snekbox.utils.logging import init_logger, init_sentry DEBUG = os.environ.get("DEBUG", False) -GIT_SHA = os.environ.get("GIT_SHA", "development") -sentry_sdk.init( - dsn=os.environ.get("SNEKBOX_SENTRY_DSN", ""), - integrations=[FalconIntegration()], - send_default_pii=True, - release=f"snekbox@{GIT_SHA}" -) - - -class GunicornLogger(glogging.Logger): - """Logger for Gunicorn with custom formatting and support for the DEBUG environment variable.""" - - error_fmt = "%(asctime)s | %(process)5s | %(name)30s | %(levelname)8s | %(message)s" - access_fmt = error_fmt - datefmt = None # Use the default ISO 8601 format - - def setup(self, cfg: Config) -> None: - """ - Set up loggers and set error logger's level to DEBUG if the DEBUG env var is set. - - Note: Access and syslog handlers would need to be recreated to use a custom date format - because they are created with an unspecified datefmt argument by default. - """ - super().setup(cfg) - - if DEBUG: - self.loglevel = logging.DEBUG - else: - self.loglevel = self.LOG_LEVELS.get(cfg.loglevel.lower(), logging.INFO) - - self.error_log.setLevel(self.loglevel) - - -log = logging.getLogger("snekbox") -log.setLevel(logging.DEBUG if DEBUG else logging.INFO) -log.propagate = True -formatter = logging.Formatter(GunicornLogger.error_fmt) -handler = logging.StreamHandler(sys.stdout) -handler.setFormatter(formatter) -log.addHandler(handler) +init_sentry() +init_logger(DEBUG) diff --git a/snekbox/utils/__init__.py b/snekbox/utils/__init__.py index 5a7b632..6d6bc32 100644 --- a/snekbox/utils/__init__.py +++ b/snekbox/utils/__init__.py @@ -1,3 +1,3 @@ -from . import cgroup, swap +from . import cgroup, logging, swap -__all__ = ("cgroup", "swap") +__all__ = ("cgroup", "logging", "swap") diff --git a/snekbox/utils/gunicorn.py b/snekbox/utils/gunicorn.py new file mode 100644 index 0000000..68e3ed9 --- /dev/null +++ b/snekbox/utils/gunicorn.py @@ -0,0 +1,33 @@ +import logging + +from gunicorn import glogging +from gunicorn.config import Config + +from snekbox import DEBUG +from .logging import FORMAT + +__all__ = ("GunicornLogger",) + + +class GunicornLogger(glogging.Logger): + """Logger for Gunicorn with custom formatting and support for the DEBUG environment variable.""" + + error_fmt = FORMAT + access_fmt = error_fmt + datefmt = None # Use the default ISO 8601 format + + def setup(self, cfg: Config) -> None: + """ + Set up loggers and set error logger's level to DEBUG if the DEBUG env var is set. + + Note: Access and syslog handlers would need to be recreated to use a custom date format + because they are created with an unspecified datefmt argument by default. + """ + super().setup(cfg) + + if DEBUG: + self.loglevel = logging.DEBUG + else: + self.loglevel = self.LOG_LEVELS.get(cfg.loglevel.lower(), logging.INFO) + + self.error_log.setLevel(self.loglevel) diff --git a/snekbox/utils/logging.py b/snekbox/utils/logging.py new file mode 100644 index 0000000..e5afd0c --- /dev/null +++ b/snekbox/utils/logging.py @@ -0,0 +1,33 @@ +import logging +import os +import sys + +import sentry_sdk +from sentry_sdk.integrations.falcon import FalconIntegration + +__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() -> None: + """Initialise the Sentry SDK.""" + git_sha = os.environ.get("GIT_SHA", "development") + sentry_sdk.init( + dsn=os.environ.get("SNEKBOX_SENTRY_DSN", ""), + integrations=[FalconIntegration()], + send_default_pii=True, + release=f"snekbox@{git_sha}" + ) |