java 未找到 log4j2 配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39676284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j2 configuration file not found
提问by geek4079
I'm struggling to understand what is wrong with this issue.
我正在努力理解这个问题有什么问题。
I have a java project (no Eclipse/Maven) structured like the following:
我有一个 java 项目(没有 Eclipse/Maven),结构如下:
├── build
│?? ├── classes
│?? │?? └── oata
│?? │?? └── HelloWorld.class
│?? └── jar
│?? └── HelloWorld.jar
├── build.xml
├── lib
│?? ├── log4j-api-2.3.jar
│?? └── log4j-core-2.3.jar
├── myManifest
└── src
├── log4j2.xml
└── oata
└── HelloWorld.java
when building with ant I keep getting that the log4j2 config file cannot found. I already looked at similar questions and the common issues were regarding that the xml file was not in the classpath, which is not my case. Anyway this is the error:
使用 ant 构建时,我不断发现找不到 log4j2 配置文件。我已经看过类似的问题,常见的问题是 xml 文件不在类路径中,这不是我的情况。无论如何,这是错误:
[java] ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
This is my config file
这是我的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="oata"
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>
<Logger name="oata.HelloWorld" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
回答by Amit Bhoraniya
I am assuming you are executing java class using Ant build script.
我假设您正在使用 Ant 构建脚本执行 java 类。
No log4j2 configuration file found
means log4j2.xml
configuration file is not in your classpath.
No log4j2 configuration file found
意味着log4j2.xml
配置文件不在您的类路径中。
Please verify HelloWorld.jar includes log4j confuguration file or not.
请验证 HelloWorld.jar 是否包含 log4j 配置文件。
Alternate way set log4j confuguration file path as system property in your build script.
另一种方法是在构建脚本中将 log4j 配置文件路径设置为系统属性。
<sysproperty key="log4j.configurationFile" value="file:///${basedir}/src/log4j2.xml" />
Here is a documentation for log4j configuration.
这是log4j 配置的文档。