如何将 java.util.log 转发到 apache commons 日志记录?
How to forward java.util.log to apache commons logging?
我们使用 apache commons logging 进行日志记录。但是现在我们正在使用一个使用 java.util 日志记录的 OSS 库。
如何让 lib 调用的 java.util 日志语句显示在我们的 apache commons log4j 日志文件中?
Apache Commons 常见问题解答中对此进行了介绍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:
他们所做的只是创建一个 java.util.logging.Hander 以使 JUL 的输出适应公共日志记录。在示例中,它们可能应该处理其他命名日志级别之间的日志级别 int 值,以便您在 JUL 中处理任何自定义日志级别。一个示例补丁是:
@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);
}
}
我们使用 apache commons logging 进行日志记录。但是现在我们正在使用一个使用 java.util 日志记录的 OSS 库。
如何让 lib 调用的 java.util 日志语句显示在我们的 apache commons log4j 日志文件中?
Apache Commons 常见问题解答中对此进行了介绍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:
他们所做的只是创建一个 java.util.logging.Hander 以使 JUL 的输出适应公共日志记录。在示例中,它们可能应该处理其他命名日志级别之间的日志级别 int 值,以便您在 JUL 中处理任何自定义日志级别。一个示例补丁是:
@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);
}
}