java 未找到 Log4J 属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6178029/
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 properties file not found
提问by RedEagle
I have a Java Project that was added to the Java Build Path of a Web Project.
我有一个已添加到 Web 项目的 Java 构建路径的 Java 项目。
On the first Java project I added the Log4J JAR file to the Java Build Path and since this project was added to the Java Build Project of the Web Project, the JAR file was automatically added to the Web Project Java Build Path also.
在第一个 Java 项目中,我将 Log4J JAR 文件添加到 Java 构建路径中,并且由于该项目已添加到 Web 项目的 Java 构建项目中,因此 JAR 文件也自动添加到 Web 项目 Java 构建路径中。
On the Web Project I have a Web Service that instantiates a class of the Java Project. That class has a simple Logger, and it works correctly.
在 Web 项目上,我有一个 Web 服务,用于实例化 Java 项目的一个类。该类有一个简单的 Logger,并且可以正常工作。
Now, I am trying to create a properties file named log4j.properties to configure the Logger, Appender and LayoutPattern.
现在,我正在尝试创建一个名为 log4j.properties 的属性文件来配置 Logger、Appender 和 LayoutPattern。
Whenever I try to call a method of the instantiaded class I get this error on the console:
每当我尝试调用实例化类的方法时,我都会在控制台上收到此错误:
log4j:ERROR Could not read configuration file [log4j.properties].
log4j:ERROR 无法读取配置文件 [log4j.properties]。
What am I doing wrong?
我究竟做错了什么?
Here's the log4j properties file:
这是 log4j 属性文件:
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Sorry but it was a false alarm...
抱歉,这是误报...
Like I said, the project where the class that instantiates the logger resides is added as a dependency o a main project where the web services are defined.
就像我说的,实例化记录器的类所在的项目被添加为定义 Web 服务的主项目的依赖项。
As a result that project is published on a JAR file and with the suposed solution I mentioned:
结果,该项目发布在 JAR 文件中,并使用我提到的 suposed 解决方案:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
I get a path like:
我得到一条类似的路径:
C:/project_path.jar/log4j.properties.
C:/project_path.jar/log4j.properties。
Obviously the propertied files still isn't found...
显然,仍然没有找到属性文件......
Sory... Still working on a solution
Sory...仍在研究解决方案
采纳答案by RedEagle
Ok, sometimes the obvious answer is the one you least expect.
好吧,有时显而易见的答案是您最不期望的。
As it turned out I simply needed to remove the PropertyConfigurator.configure(xxx) line and place the log4j.properties file on the src folder of the dependency project.
事实证明,我只需要删除 PropertyConfigurator.configure(xxx) 行并将 log4j.properties 文件放在依赖项目的 src 文件夹中。
Thanks
谢谢
回答by phil294
If using log4j 2.x, you need to have a file called log4j2.xml
.
如果使用 log4j 2.x,您需要有一个名为log4j2.xml
.
log4j.properties
won't do it.
log4j.properties
不会做。
回答by P?l Brattberg
Place your log4j.properties
file in your classes
directory if using unpacked WAR, else place it in the src
folder (root folder for your java classes).
如果使用解压的 WAR,请将log4j.properties
文件放在classes
目录中,否则将其放在src
文件夹中(Java 类的根文件夹)。
回答by RedEagle
The only way I found to solve this was to put the log4j.properties file in the src root folder.
我发现解决此问题的唯一方法是将 log4j.properties 文件放在 src 根文件夹中。
Then on the class that instantiates the logger the following line:
然后在实例化记录器的类上添加以下行:
PropertyConfigurator.configure("log4j.properties")
PropertyConfigurator.configure("log4j.properties")
... had to be changed to:
...必须改为:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
And finally the file was found.
最后找到了文件。
Thanks for the insight P?l
感谢您的洞察力 P?l