Finding Bugs: Debugger versus Logging
Join the DZone community and get the full member experience.Join For Free
one common reaction to my post on writing debuggable code was: you don’t need logging, just use a debugger. while there are cases where a debugger is the best option, there are many reasons why having proper logging in place is superior to using a debugger for trouble shooting.
where do you start? the systems we develop at symsoft deliver text messages (sms) to mobile subscribers. to deliver one sms, there is quite a bit of processing, including external signaling and possibly a database lookup. if an sms is not delivered as expected, where do you start trouble shooting? not all failures cause an exception to be thrown, so there may not be an obvious starting point. even with moderately complex logic, it is quite difficult to know where to set the first breakpoint. furthermore, you will most likely not get it right on the first try. with a log, you instantly get an overview of the whole process.
a sequence, not a snapshot. to figure out why something is not working as expected, you often need more than just the current state. why is this variable null? why are we in state idle when receiving this response message? why did this timer time out? to be able to answer these questions, you need to see what happened before the error happened. with a debugger, you see the current state. with a log, you also see the events leading up to the current state. that makes a big difference.
saved, not fleeting. the log statements you put into the code remain there. you keep getting the benefits of them every time you trouble shoot that part of the code again. with a debugger, it is always a one-off effort. the next time you debug the same part of the code, you again have to figure out where to put a breakpoint, which variables to look at and so on. with the log statements, your efforts are saved and remain for the next time.
who can do it? to use a debugger, you need a certain level of expertise. you also need to know the code well enough to know where to look for the problem. this limits the number of people that can trouble shoot a problem. on the other hand, a lot more people can enable logging when there is a problem, collect the results and send them to the developer. some customers or technician may even be able to figure out what the problem is just by reading the logs, without involving a developer at all.
in a live system? are you comfortable with attaching a debugger in a busy system carrying live traffic? there is always the possibility of making a mistake, or stopping threads that will cause time-outs in other parts of the system. by comparison, enabling and disabling logging is a lot safer.
what about performance?
another argument against logging was that it is too slow and produces too much data. clearly, it is not possible to log everything, so here are the strategies that work for us:
use session-based logging. with session-based logging , you log everything that happens in the specified session, and nothing else. for example, you can specify a list of phone numbers, and when (and only when) one of those phone numbers sends an sms, a full log of the complete session is output. so even though the system may be processing several thousand smss per second, only a handful logs are output.
on and off at run-time. it must be possible to turn logging on and off while the system is running. if there is a problem, you enable logging to try to capture the problem. if there is no problem, the logging can be turned off.
buffer before outputting. there should be close to no performance penalty with logging. if logging is disabled, the code should just skip the logging statements. if logging is enabled, the log strings should be handed off to a low priority queue that can collect and output the log without a performance penalty for the traffic handling.
“we don’t need logging”
some people argue that as long as you write well structured, clean code, and test the code properly, then it should work, and there is no need to spend time adding logging statements. in my experience, this is just wishful thinking. however careful you are developing the software, there will always be bugs slipping through. the sooner you admit this, the better.
also, a problem may not even be caused by a bug. there are many cases where the software is working as it should, but the result is not what the customer expects. perhaps the system is not configured correctly. or maybe an external system is not responding as it should. or maybe there is a misunderstanding on how a certain feature works. in all those cases it is important to be able to see what is happening in order to get it to work the way the customer wants to, even though there is no bug in the software.
there are many cases where your only option is using a debugger in order to figure out what the problem is. even great log statements can’t cover all cases. but by using logging wisely, you can cut down substantially the number of times the debugger is needed, while at the same time getting all the benefits of logging.
Published at DZone with permission of Henrik Warne, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.