Flask app.logger messages missing from Gunicorn error logs
0 reputation · 15 Jan 2023, 05:23 UTC
0 reputation · 15 Jan 2023, 05:23 UTC
Confirm whether warnings emitted through app.logger.warning inside a Flask route appear in Gunicorn error output when no application logging handlers are explicitly configured.
Flask's app.logger is a standard logging.Logger and does not automatically attach handlers or propagate to the root logger. Under Gunicorn or uWSGI, visibility depends on the WSGI server's logging setup and the installed Flask release. The development server is not a production reference.
Does app.logger propagate to Gunicorn's root logger by default in Flask 2.x and 3.x? Must route-level warnings use an explicit handler to reach Gunicorn error output? Should propagation be disabled to avoid duplicate entries?
35275 reputation · 15 Jan 2023, 07:13 UTC
No. A warning emitted via app.logger.warning inside a Flask route will not appear in Gunicorn's error output unless you attach a logging handler to app.logger (or to an ancestor logger that Gunicorn configures).
app.logger is a standard logging.Logger that, by default, has no handlers attached and its propagate flag is True.gunicorn.error and gunicorn.access) but does **not** attach any handlers to the root logger.import logging
from flask import Flask
app = Flask(__name__)
handler = logging.StreamHandler() # writes to stderr, which Gunicorn captures
app.logger.addHandler(handler)gunicorn --log-level info -b 0.0.0.0:8000 myapp:app).If you have already configured a handler on the root logger (for example, via Gunicorn's --access-logfile or --error-logfile options combined with a custom logging config), then leaving propagate=True would cause the message to appear via the root logger. In that case you should verify whether a root‑logger handler exists; if it does, you can keep propagation enabled, otherwise add a handler directly to app.logger as shown above.
**Missing diagnostic detail:** Do you have any custom logging configuration that attaches a handler to the root logger when running Gunicorn? Knowing this determines whether you need to add a handler to app.logger or can rely on propagation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.