diff options
author | 2019-06-04 12:33:38 -0700 | |
---|---|---|
committer | 2019-06-04 14:46:11 -0700 | |
commit | a8fec66504a94c6b830681eef1c3276104b7ff25 (patch) | |
tree | 60f712bed41d29db65bcdba634aa125c72282895 | |
parent | Lint tests (diff) |
Configure gunicorn logging
-rw-r--r-- | Pipfile | 2 | ||||
-rw-r--r-- | snekbox/__init__.py | 32 |
2 files changed, 24 insertions, 10 deletions
@@ -30,7 +30,7 @@ lint = "flake8" precommit = "pre-commit install" test = "pytest tests --cov . --cov-report term-missing -v" report = "pytest tests --cov . --cov-report=html" -snekbox = "gunicorn -w 2 -b 0.0.0.0:8060 snekbox.site.snekapp:app" +snekbox = "gunicorn -w 2 -b 0.0.0.0:8060 --logger-class snekbox.GunicornLogger --access-logfile - snekbox.site.snekapp:app" buildbox = "docker build -t pythondiscord/snekbox:latest -f docker/Dockerfile ." pushbox = "docker push pythondiscord/snekbox:latest" buildboxbase = "docker build -t pythondiscord/snekbox-base:latest -f docker/base.Dockerfile ." diff --git a/snekbox/__init__.py b/snekbox/__init__.py index f14fc89..af8429b 100644 --- a/snekbox/__init__.py +++ b/snekbox/__init__.py @@ -1,10 +1,24 @@ import logging -import sys - -logformat = logging.Formatter(fmt="[%(asctime)s] [%(process)s] [%(levelname)s] %(message)s", - datefmt="%Y-%m-%d %H:%M:%S %z") -log = logging.getLogger(__name__) -log.setLevel(logging.DEBUG) -console = logging.StreamHandler(sys.stdout) -console.setFormatter(logformat) -log.addHandler(console) +import os + +from gunicorn import glogging + +DEBUG = os.environ.get("DEBUG", False) + + +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" + datefmt = "%Y-%m-%d %H:%M:%S" + + def setup(self, cfg): + """Set up loggers and set error logger's level to DEBUG if the DEBUG env var is set.""" + 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) |