Here’s what I’m looking for:
- Java
- Log library that could replace commons logging or slf4j
- Parameter expansion only when log level requires it (i.e. no
if (log.isDebugEnabled())blocks) - String formatting the same as
java.lang.String.format - Detect java.lang.Throwable as final parameter and log stack trace
The goal is to write code like this
private static final Log log = new Log.getLog(MyClass.class);
// ...
String value = "test";
log.debug("The value is [%s]", value); // [1]
// ...
} catch (Exception e) {
log.error("Caught Exception: %s", e.getMessage(), e); // [2]
}
[1] would print a log statement like
The value is [test]
If the log level were INFO, the string format operation would not happen.
[2] would print
Caught Exception: [value of e.getMessage()]
java.lang.Exception
at com.my.org.MyClass.myMethod(MyClass.[line number])
at [...]
This seems like pretty sane behavior to me. I’m surprised I can’t easily find a library that does this.
EDIT: I should have specified, but slf4j does not meet all these requirements.
JBoss has a logging implementation that does printf- and MessageFormat-style formatting. Supposedly it can be used on it’s own, but I can’t find a project for it, other than its integration with Seam Solder. There are standalone artifacts though: https://repository.jboss.org/nexus/content/groups/public/org/jboss/logging/jboss-logging/