Is there a log4j or commons logging extension or other logging framework that is designed for Java 5 or higher?

StackOverflow https://stackoverflow.com/questions/560588

  •  05-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

Log5j is exacly what you need.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top