DEV Community

Rumesh Madhusanka
Rumesh Madhusanka

Posted on

How to log everything?

It is possible to log everything happens in a software such as user clicks, database queries etc and then use those logs to find out when a bug arises. Is there a standard way to do that? What should be logged and what should be kept out of it?

Top comments (2)

Collapse
 
v6 profile image
πŸ¦„N BπŸ›‘ • Edited

Oh, yes, the way to do this is to use whatever the equivalent of a print statement is in your language of choice, and make sure there is one such statement for every line of non-logging code that you have.

Variable assignment? Log it.

Importing a library? Log that too.

And don't forget, any time an object is created, changed, or destroyed, each of those gets a log line too.

If you get enough logs like this, it becomes what they call a forest.

But keep it secret! Let no one branding themselves as an "architect" or a "tech lead" see the code!

In their arrogance, they have been known to start many "forest fires" of these logs, leaving only blackened stumps of the once rich foliage behind. They have a lot of various reasonings for this, but ultimately what's going through their heads is, "I always know what the code does. Why does anyone need logs for that? Hahaha, we will take these logs away, to encourage these less experienced programmers to learn what's really going on just by looking at the code, like we do."

But don't give in. We can't let them block us. Even if we have to temporarily remove the logs to get past code review, we can keep a version of the code with all the logs still in place.

This is where skills in code obfuscation become important. To help us keep our logs, and survive.

Collapse
 
brandinchiu profile image
Brandin Chiu

What you inevitably log is totally up to you: you're the programmer.

If you want to log everything, you can totally do that, but it would be a lot of work to maintain, and a lot of data to sift through if a bug comes up.

You want to be careful that you're not actually making your job harder by logging too much. Then you need to worry about looking for the needle in that haystack.

Another concern to keep in mind is the logging isn't free. Depending on where your logs are stored, there are going to be latency, storage, and financial costs to logging that must be considered.

Finally, there are legal ramifications to what you log. Depending on local and regional laws, logging personal or even some non-personal information may be prohibited.

For example, PCI compliance dictates what kind of things you can and can't log in regards to credit card numbers.