views:

78

answers:

2

Due to recent events, i am trying to figure out how much debugging logs i should use for code in general.

What i have been doing is using debugging logs pretty sparingly, and just in cases where i wanted some extra information or what have you. This made sense to me, as it seems like you shouldn't log every little thing your code does, as that could make flood you with so much information that it would be easier to miss something that was actually important (or go crazy from digging through and verifying logs).

On the other hand, i present an example: I just started using logback/slf4j for my java project, and to test that i had the .xlm file set up correctly i added a debugging log statement to the end of a method that initializes gui components. Normally i would have never put a log statement there, because it is pretty obvious if your gui components don't initialize correctly when you run the program. However this time i ran the program, and low and behold the logs showed that the gui components were being initialized twice, even though only one set of them were being displayed. A decent sized bug, but something i likely wouldn't have caught without without those debugging statements.

So my question: Is there any "best practices" out there when it comes to debugging logs? I have seen many best practice questions when it comes to info logs, exceptions, errors, etc, but haven't found much out there in regards to debugging logs.

Thanks :)

+2  A: 

Some thoughts:

  1. Don't just log what's happening, but take care to log the available parameters/method arguments etc. It's easy to overlook this.
  2. It's easy to disable debug logging via configuration rather than logging in after the fact.
  3. Don't worry about logging overhead until it really becomes an issue.
  4. You can automate some logging (entry/exit of methods) by using an AOP framework (Spring / AspectJ etc.)
Brian Agnew
Yea, it's not the overhead/disabaling specific logs that i am worried about, as like you said that is pretty easy to do. It's more just how much do i clutter up my code with these log statements. For example, should i log whenever any method gets called, just when some methods get called, constructors, etc? What about when i change a given variable (that wouldn't be able to crash the program), should i log that to insure it has the expected value? I think your first point sorta sums it up, but I'm just trying to really wrap my head around all of it.
kyeana
+1  A: 

I don't think there are any "best practices" in deciding what / how much to log. It is one of those catch-22 situations. If you need to look at the logs, there is "never" enough information there, but if you don't then "all" logging is just code clutter and an unnecessary runtime overhead. You need to make a separate judgement about where to draw the line for each application.

One point to bear in mind though. If you and your customers are able to hack the code to add temporary debugging statements, then you don't need as much permanent logging code in place. But if you don't have the option of hacking on the (almost) production code to debug things, then you need a certain level of logging code in place, just in case ...

Stephen C