日志是在eclipse中运行project时创建的,但是当创建了可运行的jar(库处理选项:将所需库提取到生成的jar中)时,无法找到配置文件,也不会生成日志。
当通过命令行运行jar时(cd到包含可运行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.
消息的最后一位告诉我找不到配置文件,尽管它正在从某处读取配置模式布局。将选项-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值指定为“c:/path/to/config/log4j2.xml”和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")
我还尝试过从原始log4j(log4j-1.2.17.jar)导入import org.apache.log4j.basicconfigurator和propertyconfigurator时使用basicconfigurator.configure()和propertyconfigurator.configure()方法。
考虑到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选项,则应用程序将启动,但不会生成日志。
请帮助我解决这个问题,让我知道如果你需要我提供任何进一步的信息。我知道以前也有人问过类似的问题,但我无法找到一个适合我的问题的解决方案;可运行jar、eclipse、外部log4j2配置文件。不使用ant或maven。
2条答案
按热度按时间hec6srdp1#
我不知道你做错了什么,但这里有一个mcve。
Test.java
```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`
lymnna712#
我的问题是我在初始化日志之前调用了它。
所有的东西都被正确地打包,并且主类有适合log4j的代码。罪魁祸首是在执行main之前初始化的类级对象。该对象的构造函数记录了它的创建。在eclipse中调试时,这不会导致任何错误,但会停止可执行文件的运行。
感谢@andreas给我一个新项目重新开始的理想。