feroze
feroze

Reputation: 7594

How to forward java.util.log to apache commons logging?

we use apache commons logging for our logging. However now we are consuming an OSS lib that is using java.util logging.

how do I get java.util log statements invoked by the lib to show up in our apache commons log4j log file?

Upvotes: 0

Views: 395

Answers (1)

jmehrens
jmehrens

Reputation: 11045

This is covered in the Apache Commons FAQ Can calls to java.util.logging be redirected via commons-logging?.

Yes. The java.util.logging classes present in java since 1.4 are both an API and a (primitive) logging implementation. It is possible to install an "implementation" that redirects messages back to commons-logging, which will then in turn direct the calls to the appropriate concrete logging library that commons-logging is sending other messages to.

Alternatively, have your java.util.logging "implementation" send messages directly to the same implementation that commons-logging is bound to. This will be faster - although if you change your commons-logging configuration to use a different logging library then the java.util.logging implementation would need to be changed too.

See here for details:

http://wiki.apache.org/myfaces/Trinidad_and_Common_Logging

What they are doing is just creating a java.util.logging.Hander to adapt the output of JUL to commons logging. In the example, they should probably handle log levels int values that are in between other named log levels so that you are handing any custom log level in JUL. An example patch would be:

@Override
public void publish(LogRecord record) {
    Log log = getLog(record.getLoggerName());
    String message = record.getMessage();
    Throwable exception = record.getThrown();
    int level = record.getLevel().intValue();
    if (level >= Level.SEVERE.intValue()) {
        log.error(message, exception);
    } else if (level >= Level.WARNING.intValue()) {
        log.warn(message, exception);
    } else if (level >= Level.INFO.intValue()) {
        log.info(message, exception);
    } else if (level >= Level.CONFIG.intValue()) {
        log.debug(message, exception);
    } else {
        log.trace(message, exception);
    }
}

Upvotes: 1

Related Questions