Django View logging is back-the-front

Andy Bulka
Sep 4 · 5 min read
Photo by bruce mars on Unsplash
def diagramz_list(request, template_name="list.html"):
log.info("diagramz_list has been called")
...
return render(request, template_name, {"object_list": diagrams})

[2018–10–20 14:42:50] [INFO] diagramz_list has been called
[2018–10–20 14:42:51] [INFO] “GET /listz HTTP/1.1” 200 119585

IMO — the Django endpoint call should be logged immediately.

Anything that happens during the execution of that request should be logged afterwards.

The solution

MIDDLEWARE = [
...
"SOMEPATH.request-logging-middleware.RequestLoggingMiddleware",
...
[2018-10-21 14:48:21] [INFO] Endpoint /listz request 127.0.0.1
[2018-10-21 14:48:21] [INFO] diagramz_list has been called
[2018-10-21 14:48:22] [INFO] Endpoint completed, 127.0.0.1 GET /listz 200 119585 (0.26 seconds) (39 SQL queries, 1.0 ms)
[21/Oct/2018 14:48:22] "GET /listz HTTP/1.1" 200 119585

Production out of sequence issues too!

all Python and Django log messages go to stderr

and all gunicorn log messages that deal with view hits and static file serving go to stdout.

Conclusion

My Other Cool Software

GitUML — generate UML diagrams instantly from any GitHub repository containing Python code (web app). A revolution in documentation — diagrams automatically update when you push code using git

About Andy Bulka

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade