-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to structlog to fix logging under gunicorn #203
Conversation
colorama could be a dev-only dependency, but I've ended up missing it when running scripts manually in deployment environments.
Instead of duplicating metrics logic inside and outside the exception handler, use finally to populate metrics once.
Silence the uvicorn per-request logs. In the renamed log_request_middleware, gather the data from the request before sending to FastAPI, and then gather the rest from the response, and send as a metric and a log "ctms.web".
@@ -38,6 +38,8 @@ sentry-sdk = "^1.0.0" | |||
pysilverpop = "^0.2.6" | |||
lxml = "^4.6.3" | |||
prometheus-client = "^0.10.1" | |||
structlog = "^21.1.0" | |||
colorama = "^0.4.4" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this enables some colorization for output, I'm interested how this plays with the structlog for console outputting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! 🚀
logging_config = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"dev_console": { | ||
"format": "%(asctime)s %(levelname)s:%(name)s: %(message)s" | ||
"()": structlog.stdlib.ProcessorFormatter, | ||
"processor": structlog.dev.ConsoleRenderer(colors=True), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bsieber-mozilla - colorized logs, with colorama, are used when the dev_console
logger is used. This is the default for development. There's no color option for the Mozlog JSON logs.
Implement web request logging in structlog with logger
ctms.web
, so that logs will be consistent between the development environment and running under gunicorn in stage and production. This should fix #169.This drops the
UvicornJsonLogFormatter
, which was not being used when run undergunicorn
. Instead, the now-duplicate uvicorn access logs are silenced by raising the minimal level toWARNING
.The data gathering is adjusted, to remove some duplication in the error handler, and to move request processing from metrics to logging, so that metrics works from the logging context rather than the
Request
itself.In the dev environment, with
make build start
, logs for a request to http://localhost:8000/ (root, then redirect to docs) look like this (but with color):If you set
CTMS_USE_MOZLOG=True
in.env
, the development logs look like this:Finally, if you set
target: production
indocker-compose.yml
, and runmake build start
, the logs will look like they will in stage and production: