views:

415

answers:

3

Java 5 has introduced many features that can be make logging statements less cluttering, such as variable number of arguments and printf. That can alleviate all the message building code that happens when something is logged, as well as the surrounding if.

For example, instead of writing:

if (log.isDebugEnabled()
{
    log.debug("User id: "+uid+", Request id:"
    + rid +", Client IP: "+ip+" blah blah blah");
}

I would like to write:

log.debug("User id: %s, Request id: %s, Client IP: %s blah blah blah",
uid, rid, ip);

or something like that.

Do you know a logging framework or an extension to a logging framework that can help with that?

+3  A: 

Log5j is exacly what you need.

GvS
+1  A: 

It is easy enough to write your own wrapper methods for that.

Dev er dev
+2  A: 

Simple Logging Facade for Java (SLF4J)

The Simple Logging Facade for Java or (SLF4J) is intended to serve as a simple facade for various logging APIs allowing to the end-user to plug in the desired implementation at deployment time. SLF4J also supports a bridging legacy APIs as well as a source code migration tool.

SLF4J API offers an advanced abstraction of various logging systems, including JDK 1.4 logging, log4j and logback. Features include parameterized logging and MDC support.

Example:

logger.info("{} {}!", "Hello", "world");

produces "Hello world!" but only if info level is enabled.

Peter Štibraný
SLF4J does not have a variable-argument log statement like the OP requests. (Only 1 and 2 arguments, the rest are Object[]
krosenvold
Hmmm, true. I never realized that. (Bug for this problem is at: http://bugzilla.slf4j.org/show_bug.cgi?id=31)
Peter Štibraný