One would imagine that logging has been largely solved 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’re in debug mode, trigger a null pointer exception, and catch it. Then we can log our message, including the exception- presumably because we want the stack trace. Because there’s not already a method for doing that (there is).

I really “love” how much code this is to get to a really simple result. And this code doesn’t appear in the codebase once, this is a standardized snippet for all logging. Our submitter didn’t include any insight into what instrPoint may be, but I suspect it’s a tracing object that’s only going to make things more complicated. getDescription and getDepth seem to be information about what our execution state is, and since this snippet was widely reused, I suspect it’s a property on a common-base class that many objects inherit from, but I’m just guessing. Guessing based on a real solid sense of where things can go wrong, but still a guess.

[Advertisement] Otter – Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

Remy Porter

Source link

You May Also Like

We never got that goalie fight, but at least we still have NHL memes (43 Photos)

Stephen Source link

Woooahh Woah Woah, Hold Up! (30 Photos)

Hendy Source link

Amtrak Passengers Stranded On Train For 29 Hours Feared They Were Being Kidnapped

Hundreds of Amtrak passengers in South Carolina were stranded on a train…

Easy

Tags: Funny 5724 points, 137 comments. Source link