如何在Runnable jar中指定外部log4j2配置文件使用Eclipse创建

时间:2017-01-03 23:39:54

标签: java eclipse logging jar log4j2

在Eclipse中运行项目时会创建日志,但是当创建可运行的jar(库处理选项:将所需的库提取到生成的JAR中)时,找不到配置文件,也不会生成日志。

当通过命令行运行jar时(cd进入包含runnable jar的目录),输出如下:

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

该消息的最后一位告诉我无法找到配置文件,尽管它正在从某处读取配置PatternLayout。当选项-Dlog4j.configurationFile选项添加到命令时,会发生以下异常(除了以前的错误消息):

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException: No Configuration was provided
        at java.util.Objects.requireNonNull(Unknown Source)
        at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java
:477)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:561)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:577)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:212)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextF
actory.java:242)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextF
actory.java:45)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
        at test.SmokeTest.<clinit>(SmokeTest.java:41)

我已尝试将-Dlog4j.configurationFile值指定为&#34; c:/path/to/config/log4j2.xml"和&#34; file:// c:/path/to/config/log4j2.xml"并得到相同的错误。异常消息中的最后一行是记录器的初始化。 在代码中,我尝试使用以下选项初始化记录器:

 - private static final Logger logger =
   LogManager.getLogger("c:/path/to/config/log4j2.xml");
 - private static final Logger logger = LogManager.getLogger("logger name specified in config file");

在写入文件之前,我设置了log4j.configurationFile系统属性。尝试了以下选项:

- System.setProperty("log4j.configurationFile", "C:\\path\\to\\config\\log4j2.xml")
- System.setProperty("log4j.configurationFile", "file://c:/path/to/config/log4j2.xml")

我还尝试使用BasicConfigurator.configure()和PropertyConfigurator.configure()方法从原始log4j(log4j-1.2.17.jar)导入org.apache.log4j.BasicConfigurator和PropertyConfigurator。

认为log4j2可能无法从外部文件获取其配置,我将log4j2.xml文件的副本放在项目的src文件夹中。然后尝试使用以下命令初始化记录器:

- private static final Logger logger = LogManager.getLogger(MyClass.class.getName());
- private static final Logger logger = LogManager.getLogger(SmokeTest.class);

两者都给了我相同的错误,但也许这就是为什么它在从Eclipse运行时有效?

此外,如果我双击程序图标或从命令行运行而不使用-Dlog4j.configurationFile选项,则应用程序将启动但不会生成日志。

请帮我解决此问题,如果您需要我提供任何进一步的信息,请告诉我。我之前已经问过类似的问题,但是我无法找到适用于我的问题的解决方案; runnable jar,Eclipse,外部log4j2配置文件。不使用Ant或Maven。

2 个答案:

答案 0 :(得分:1)

我的问题是我在初始化之前调用了日志   一切都正确打包,主类有适当的log4j代码。罪魁祸首是一个在主要执行之前初始化的类级别对象。该对象的构造函数记录了它的创建。在Eclipse中进行调试时,这不会导致任何错误,但会阻止可执行文件运行 感谢@Andreas给了我一个从新项目开始的理想。

答案 1 :(得分:0)

不知道你做错了什么,但这里有MCVE

import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Test { private static transient Logger log = LogManager.getLogger(Test.class); public static void main(String[] args) { log.info("Hello World"); } }

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>

log4j-api-2.7.jar

Apache Log4j 2添加到Eclipse构建路径:log4j-core-2.7.jarFile

使用Eclipse Export...&gt;创建jar文件Runnable JAR file&gt; Extract required libraries into generated JAR&gt;名为Test.jar的{​​{1}}。

使用以下命令运行:

java -Dlog4j.configurationFile=C:\path\to\log4j2.xml -jar Test.jar

输出

14:06:23.728 [main] INFO  Test - Hello World