aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2019-06-15 22:34:08 -0700
committerGravatar MarkKoz <[email protected]>2019-06-15 22:34:08 -0700
commit9dc011a01215cb908d490efb3f9329df19a0dc48 (patch)
tree5cd3466437988ff589857a36ff1e3ade12f7706f
parentUse system site instead of relying on virtual environment activation (diff)
Use a custom Gunicorn access log format
The handler now shares formats with the rest of the handlers. The message is formatted to show the request method, URL + query, response code, response size in bytes, and the request time in seconds. * Use the default ISO 8601 date format for all handlers
-rw-r--r--Pipfile10
-rw-r--r--snekbox/__init__.py12
2 files changed, 18 insertions, 4 deletions
diff --git a/Pipfile b/Pipfile
index 6b6aa24..986116d 100644
--- a/Pipfile
+++ b/Pipfile
@@ -31,7 +31,15 @@ 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 --logger-class snekbox.GunicornLogger --access-logfile - snekbox.api.app"
+snekbox = """
+ gunicorn
+ -w 2
+ -b 0.0.0.0:8060
+ --logger-class snekbox.GunicornLogger
+ --access-logformat '%(m)s %(U)s%(q)s %(s)s %(b)s %(L)ss'
+ --access-logfile -
+ snekbox.api.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 a48abd5..40b76db 100644
--- a/snekbox/__init__.py
+++ b/snekbox/__init__.py
@@ -11,10 +11,16 @@ 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"
+ access_fmt = error_fmt
+ datefmt = None # Use the default ISO 8601 format
def setup(self, cfg):
- """Set up loggers and set error logger's level to DEBUG if the DEBUG env var is set."""
+ """
+ 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:
@@ -28,7 +34,7 @@ class GunicornLogger(glogging.Logger):
log = logging.getLogger("snekbox")
log.setLevel(logging.DEBUG if DEBUG else logging.INFO)
log.propagate = True
-formatter = logging.Formatter(GunicornLogger.error_fmt, GunicornLogger.datefmt)
+formatter = logging.Formatter(GunicornLogger.error_fmt)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
log.addHandler(handler)