Debug Log – Daily WTF

One would imagine that logging is mostly settled at this point. Simple tasks like, “Only print this message when we’re in debug mode,” seem like obvious, well-understood features for any logging library.

LostLozz offers us… a different approach to this problem.

if ( LOG.isDebugEnabled() ) 
	try 
		Integer i = null;
		i.doubleValue();
	
	catch ( NullPointerException e ) 
		LOG.debug(context.getIdentity().getToken() + " stopTime:"
				+ instrPoint.getDescription() + " , "
				+ instrPoint.getDepth(), e);
	

If we are in debug mode, throw a null pointer exception and catch it. We can then log our message, including the exception – probably because we want a stack trace. Because there is already no method for that (there is).

I really like how much code this takes to get to a really simple result. And this code does not appear in the codebase once, this is a standardized snippet for all cutting. Our submitter did not include any insight into what instrPoint maybe it is, but I suspect it’s a tracking object that will only complicate things. getDescription and getDepth seem to be information about what our execution state is, and since this snippet is widely reused, I suspect it’s a property on a common base class that many objects inherit from, but I’m just guessing. A guess based on a real gut feeling about where things could go wrong, but still a guess.

[Advertisement]

Otter – Secure your servers automatically without the need to log into the command line. Get started today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *