Sunday, January 16, 2011

LazyLogger for Optimal Logging

Logging has always been a concern when we are talking about application performance. What to log and which appender to use and when to flush the logs and where to log etc are some of the key concerns. One of the concern has been put a check such as isLoggingEnabled at a particular level. This is where something such as LazyLogger utility could prove very useful.

Following method does the magic in the LazyLogger:

public void log(Level level, Object... params) {
// Check logging level before continuing
if (logger.isLoggable(level)) {
String message;
if (params != null) {
if (params.length == 1) {
message = String.valueOf(params[0]);
} else {
StringBuilder buf = new StringBuilder();
for (Object param : params) {
buf.append(param);
}
message = buf.toString();
}
} else {
message = "null";
}
logger.log(level, message);
}
}

No comments:

Post a Comment