java log4j:ERROR 无法解析文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16911535/
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
log4j:ERROR Could not parse file
提问by ?т?
I waste a lot of time to solve this problem. but can't. may be it is simple.
我浪费了很多时间来解决这个问题。但不能。也许很简单。
The jar file of application cant load the log4j.xml file which is the configuration file of log4j.
Here is the code i am using
应用程序的jar 文件无法加载log4j.xml 文件,它是log4j 的配置文件。
这是我正在使用的代码
import org.apache.log4j.xml.DOMConfigurator;
public class LoggerConfig {
public void configLogger()
{
DOMConfigurator.configure("log4j.xml");
}
}
Its a maven project. and my dir strucure is:
它是一个 Maven 项目。我的目录结构是:
src
main
java
| com
| | my
| | | abc
| | | | test
| | | | | LoggerConfig.java
resource
| com
| | my
| | | abc
| | | | test
| | | | | log4j.xml
My classpath file looks like:
我的类路径文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
My log4j.xml looks like
我的 log4j.xml 看起来像
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="File" value="target/test.log" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] [%d{HH:mm:ss.SSS}] %m%n" />
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="[%p] [%d{HH:mm:ss.SSS}] %m%n" />
</layout>
</appender>
<root>
<priority value="INFO" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
Thanks for any help..
谢谢你的帮助..
回答by Chetan
As per the DOMConfiguratorcode the argument passed to it is treated as a File name. Instead of that try using the classpath resource
根据DOMConfigurator代码,传递给它的参数被视为文件名。而不是尝试使用类路径资源
public void configLogger()
{
URL u = getClass().getClassLoader().getResource("com/my/abc/test/log4j.xml");
DOMConfigurator.configure(u);
}