【发布时间】:2015-10-09 21:03:54
【问题描述】:
使用 Java 1.8 和 Log4J 2.4 JUL 适配器(log4j-jul-2.4.jar、log4j-core-2.4.jar、log4j-api-2.4.jar)
public class SimpleTest {
public static void main(String[] args) throws Exception {
// writing the configuration file on the fly
try (java.io.PrintStream p = new java.io.PrintStream("./log4j-jul-test.xml")) {
p.println("<Configuration>");
p.println(" <Appenders>");
p.println(" <Console name='Console' target='SYSTEM_OUT'>");
p.println(" <PatternLayout pattern='JUL: %d %t %-5level %m%n' />");
p.println(" </Console>");
p.println(" </Appenders>");
p.println(" <Loggers>");
p.println(" <Root level='debug'>");
p.println(" <AppenderRef ref='Console' />");
p.println(" </Root>");
p.println(" </Loggers>");
p.println("</Configuration>");
}
System.setProperty("log4j.configurationFile", "log4j-jul-test.xml");
// specifying the java.util.logging.LogManager implementation
// COMMENT THE LINE BELOW TO SEE HOW THE STANDARD JAVA LOGGER REACTS
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
// getting the plain Java logger
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("test");
// works with both standard and JUL loggers
logger.info(() -> "This 1st line will be logged");
// works only with standard Java logger. JUL is attempting to MessageFormat it but shouldn't
logger.info(() -> "This 2nd line will {NOT} be logged by JUL");
// JUL will report that An exception occurred processing Appender Console
// java.lang.IllegalArgumentException: can't parse argument number: NOT
// at java.text.MessageFormat.makeFormat(MessageFormat.java:1429)
// at java.text.MessageFormat.applyPattern(MessageFormat.java:479)
// works with both standard and JUL logger
logger.log(java.util.logging.Level.INFO, "This 3rd line will {0} be logged", "successfully");
}
}
上面的代码非常简单,可以快速运行。试试看!
有谁知道为什么 Log4J JUL 试图 MessageFormat 提供的 String 显然不应该,因为标准 Java 记录器没有? (现在无论如何都可以传递任何参数)
注意方法签名是:
java.util.logging.Logger.info(SupplierString >)
【问题讨论】:
-
我在 JUL 案例中没有发现任何错误。它只是打印
INFO: This 2nd line will {NOT} be logged by JUL。 -
嗨 Sotirios,我在 Log4J 模式布局的代码/配置文件中添加了一个指示符,日志条目“JUL: ...”的前缀,你能再试一次看看是否输出有那个前缀?
-
我在评论
setProperty行时指的是。默认记录器会记录它。 -
嗨 Sotirios,是的,Java 标准实现确实显示了第 2 行,问题是使用 Log4J2 JUL LogManager 时,不会输出同一行。
标签: java logging lambda log4j2 java.util.logging