Log4j 在简单应用程序中找不到属性文件
我有一个非常简单的应用程序来说明日志记录的功能。我使用log4j。但我在设置方面遇到了一些问题。我下载了文件log4j-1.2.16.jar。并通过属性 -> 将其连接到我的项目Java 构建路径 ->图书馆 ->添加外部 JAR...。这是我的类:
package ru.log4j.log4jhelloworld;
import org.apache.log4j.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
public static void main(String[] args) {
logger.info("Hello World!");
}
}
我的 log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="aa %p %c: %m%n"/>
</layout>
</appender>
<!--Root logger-->
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
和我的 log4j.properties:
log4j.rootCategory=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %p %c: %m%n
在控制台中显示这些异常:
log4j:WARN No appenders could be found for logger (ru.log4j.log4jhelloworld.MyClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我不知道我应该如何创建和连接其他支持文件,例如 log4j.xml 和 log4j.properties。并确定他们是否需要?它们应该位于什么目录中?我还应该注册什么?这是我的项目结构
我猜想这个错误。我的问题是什么?请帮助我!
I have a very simple application that illustrates the function of logging. I use log4j. But I have some trouble with the settings. I downloaded the file log4j-1.2.16.jar. And connect it to my project by Properties -> Java Build Path -> Libraries -> Add External JARs.... Here are my class:
package ru.log4j.log4jhelloworld;
import org.apache.log4j.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class);
public static void main(String[] args) {
logger.info("Hello World!");
}
}
My log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="aa %p %c: %m%n"/>
</layout>
</appender>
<!--Root logger-->
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
And my log4j.properties:
log4j.rootCategory=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %p %c: %m%n
In the console displays these exceptions:
log4j:WARN No appenders could be found for logger (ru.log4j.log4jhelloworld.MyClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I do not know how I was supposed to create and connect other supporting files, such as log4j.xml and log4j.properties. And be sure whether they need? In what directory they should be located? What should I still register? Here is my project structure
I'm guessing that the error in this. What is my problem? Help me please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要解决您的问题,请将 log4j 配置文件(即
log4j.xml
或log4j.properties
)放在src
文件夹中,它应该可以正常工作。此外,您不需要这两个文件(
log4j.xml
和log4j.properties
),只需使用其中之一。To solve your issue place your your log4j configuration file(i.e.
log4j.xml
orlog4j.properties
) in thesrc
folder and it should work fine.Moreover you don't need both files(
log4j.xml
andlog4j.properties
), just use one of them.问题是记录器系统找不到您的配置文件。要修复它,您有多种选择:
如果您需要将记录器配置保留在打包类之外(您通常在实际生产系统中这样做),那么
/etc/my_great_app/log4j.xml
)<代码>
-Dlog4j.configuration=
例如
<代码>
-Dlog4j.configuration=文件:///etc/my_great_app/log4j.xml
请注意,路径必须包含前缀
file://
。如果您想将 log4j 配置与代码一起保留(这对于单元测试来说是典型的),那么您所需要做的就是 - 只需确保 log4 配置位置是类路径的一部分。例如,如果您使用 maven,只需将
log4j.properties
存储在src/main/resources
目录(
关于
log4j.properties
与log4j.xml
。它们只是为 log4j 记录器定义相同配置的两种不同方法。您不需要这两个文件,只需决定您更喜欢哪种格式并使用它:)The problem is that the logger system cannot find your config file. To fix it you have several options:
If you need to keep logger config outside of your packaged classes (which you usually do in real production system) then
/etc/my_great_app/log4j.xml
)-Dlog4j.configuration=<path log4j config>
e.g.
-Dlog4j.configuration=file:///etc/my_great_app/log4j.xml
Note that path must include prefix
file://
.If you'd like to keep your log4j configuration along with your code (which is typical for unit tests), then all you need to do - just make sure that log4 config location is part of the classpath. For example if you're using maven, just store your
log4j.properties
insrc/main/resources
directoryRegarding difference between
log4j.properties
vslog4j.xml
. They are just 2 different ways to define same configuration for log4j logger. You don't need both files, just decide what format you like more and use it :)使用 -Dlog4j.debug VM 参数运行应用程序,系统将提示您输入 log4j 想要的位置。
Run the application with the -Dlog4j.debug VM argument and you will be prompted with the place where log4j want it to be.
我有类似的问题。并通过将以下行放入 Run AS--> 来解决它运行配置-->参数选项卡
-Dcvp.property.path=/home/local/Project/property/
-->您的财产文件的位置谢谢!
I had the similar problem. And solved it by putting the following lines in Run AS--> Run Configurations --> Arguments tab
-Dcvp.property.path=/home/local/Project/property/
--> location of your property fileThanks!
您可以将
log4j.properties
文件保存在项目中的任何位置。然后加载你的文件
它类似于在eclipse中设置VM参数,如
-Dlog4j.configurationFile =“C:\\ Users \\ jhaas \\ Workspace_3.0.30 \\ RestClient \\ src \\ main \\ resources \\log4j.properties"
更好的方法是将其放在代码中,而不是设置到 eclipse VM 参数中。
You can keep your
log4j.properties
file any location in your project.And then load your file
It is similar to set the VM argument in eclipse like
-Dlog4j.configurationFile= "C:\\Users\\jhaas\\Workspace_3.0.30\\RestClient\\src\\main\\resources\\log4j.properties"
Better approach is to have it in your code rather than setting into the eclipse VM argument.
log4j.xml
或log4j.properties
文件应位于根源文件夹src/main/resources
下。创建新的源文件夹
src/main/resources
并将property
或xml
文件放置在其中。应该可以解决问题log4j.xml
orlog4j.properties
files should be under root source foldersrc/main/resources
.Create new source folder
src/main/resources
and place theproperty
orxml
file there. It should solve the problem