Django View logging is back-the-front

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)
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
  • Pynsource — UML for Python (desktop app)
  • Python to RPN — Run Python 3 on vintage HP calculators (web app)
  • Print42 — Electron-Python app, non-invasive log file annotation tool that supports thermal printers

About Andy Bulka

--

--

--

Software developer in Australia.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to self-check if you work in developer relations

Setting Python environment with multiple python versions

RageFan announces 100k $RAGE Leaderboard Prize pool for 2022

Studying your craft is essential as a Software Developer

The Flutter Dev Shop | Part 2

AWS IoT Case Study: Amway

Migrate Data From Standalone Redis to Redis Cluster

Computer code on two screens

Feature Flags and Test-Driven Design: Practical Tips

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andy Bulka

Andy Bulka

Software developer in Australia.

More from Medium

Containerized Django PostgreSQL Application

mysqlclient with Python 3.10 / Docker

How to Make a Webhook Receiver in Django

Exploring Full-Text Search with Django/PostgreSQL