Sometimes things don’t work exactly as planned.
That’s just something you have to take as given, on the road and when you’re operating a production web application.
Instead the question is, how do you know what’s going on?
It’s fair to say the driver of that truck has a pretty clear understanding now of what happened, when, and hopefully why. But when a Django application is running in a data center somewhere and customers are interacting with it, you don’t have that same level of obvious feedback.
No need to bury the lede too far, the answer is that you need to use logging.
For those unfamiliar with application logging, it is very much like it sounds. It’s a way of using messages sent from and outside of the application in a stream (often to a file) with timestamped messages or structured data to log what happened when an application was running.
Logging in it’s various forms, allows us to learn different things about an application that aren’t always obvious:
Before you start adding logging statements, the place to start is your questions.
Log data should be informative. To that end, you want signal, not noise. Like useless or bad tests, excessive log entries make it harder to find what you’re looking for.
That being said, when your’e starting out err on the side of more rather than less. Pare down logging later, rather than holding back on what should be logged.
There’s some overlap between both the purpose and coverage of logging on the one hand and monitoring on the other. They are different things however, and one does not preclude the other. In fact, a monitoring system may be keyed off your logging data.
The main purpose of monitoring is to proactively alert you to issues with your application. This is different than logging in that logging is _primarily _a tool for assessing after the fact. This true even in the presence of real time logging. But if nothing logging, by itself, requires you to come to it, and monitoring is designed to proactively reach out.
With regard to performance, most monitoring solutions provide something like response time analysis, but don’t always provide the kind of deep reach into your code to find out how long, say, one specific function in a module is performing. Most. Some do, and when available take advantage of these tools! They can be quite expensive, and if you really only care about one area of your code, the right logging will give you what you need.
To repeat: you want logging even when you have a monitoring solution in place.
STDOUT yours,
Ben
Learn from more articles like this how to make the most out of your existing Django site.