logger.log
July 6, 2007 · Programming
logger.log("...")
All this time i never programmed in Java something that needed detailed logging. Recently, in one of my working projects, a logging facility became a necessity. The first thing i looked was the java.util.logging API.Although i heard many people complaining about it, i found it pretty ok, at least for my needs. So i started digging the javadoc and some examples. After 20 mins i was nearly there, my logger instance was initialized, wrapped inside a singleton class that added a few more lines of tweaking in its behavior.
And then i started adding logging code inside my app. A typical class looked like:
import org.renegade.log.*;
public class Z {
private Logger logger = Logger.getInstance();
public void methodX() {
[...]
logger.log("...");
[...]
}
}
I had to keep with me all the time this Logger reference. After a minute thought i wrote something with a public
static attribute that looked like:
import org.renegade.log.*;
public class Z {
public void methodX() {
[...]
Logger.log("...");
[...]
}
}
That was a minor improvement, at least i got rid of the reference. When i made this change i remembered the static import.
So here is my final approach ...
import static org.renegade.log.Logger.*;
public class Z {
public void methodX() {
[...]
log("...");
[...]
}
}
Not bad at all :).