Log4j 在简单应用程序中找不到属性文件

发布于 2025-01-05 21:30:07 字数 2035 浏览 1 评论 0原文

我有一个非常简单的应用程序来说明日志记录的功能。我使用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.xmllog4j.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

screen
.

I'm guessing that the error in this. What is my problem? Help me please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

琉璃繁缕 2025-01-12 21:30:07

要解决您的问题,请将 log4j 配置文件(即 log4j.xmllog4j.properties)放在 src 文件夹中,它应该可以正常工作。

此外,您不需要这两个文件(log4j.xmllog4j.properties),只需使用其中之一。

To solve your issue place your your log4j configuration file(i.e. log4j.xml or log4j.properties) in the src folder and it should work fine.

Moreover you don't need both files(log4j.xml and log4j.properties), just use one of them.

感受沵的脚步 2025-01-12 21:30:07

问题是记录器系统找不到您的配置文件。要修复它,您有多种选择:

  1. 如果您需要将记录器配置保留在打包类之外(您通常在实际生产系统中这样做),那么

    • 将记录器配置文件放在类路径之外的文件系统上的某个位置(例如 /etc/my_great_app/log4j.xml
    • 在 Java 应用程序命令行中包含配置路径,如下所示
      <代码>
      -Dlog4j.configuration=

      例如
      <代码>
      -Dlog4j.configuration=文件:///etc/my_great_app/log4j.xml

      请注意,路径必须包含前缀 file://

  2. 如果您想将 log4j 配置与代码一起保留(这对于单元测试来说是典型的),那么您所需要做的就是 - 只需确保 log4 配置位置是类路径的一部分。例如,如果您使用 maven,只需将 log4j.properties 存储在 src/main/resources 目录

关于 log4j.propertieslog4j.xml。它们只是为 log4j 记录器定义相同配置的两种不同方法。您不需要这两个文件,只需决定您更喜欢哪种格式并使用它:)

The problem is that the logger system cannot find your config file. To fix it you have several options:

  1. If you need to keep logger config outside of your packaged classes (which you usually do in real production system) then

    • put your logger config file somewhere on file system outside of your classpath (e.g. /etc/my_great_app/log4j.xml )
    • Include path to your config in your Java application command line, like this

      -Dlog4j.configuration=<path log4j config>

      e.g.

      -Dlog4j.configuration=file:///etc/my_great_app/log4j.xml

      Note that path must include prefix file://.
  2. 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 in src/main/resources directory

Regarding difference between log4j.properties vs log4j.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 :)

茶底世界 2025-01-12 21:30:07

log4j.properties 文件放入 src 文件夹文件中,如下所示

com.org.slk.LogExample.java
com.org.slk.log4j.properties

然后在LogExample.java中设置PropertyConfigurator

PropertyConfigurator.configure(LogExample.class.getResourceAsStream("log4j.properties"));

和属性文件设置配置,如下面的 log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= fullpath /myLog.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.properties file put in src folder file like bellow

com.org.slk.LogExample.java
com.org.slk.log4j.properties

and then set PropertyConfigurator in LogExample.java

PropertyConfigurator.configure(LogExample.class.getResourceAsStream("log4j.properties"));

and property file set configuration like bellow log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= fullpath /myLog.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
浅语花开 2025-01-12 21:30:07

使用 -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.

高速公鹿 2025-01-12 21:30:07

我有类似的问题。并通过将以下行放入 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 file

Thanks!

╄→承喏 2025-01-12 21:30:07

您可以将 log4j.properties 文件保存在项目中的任何位置。
然后加载你的文件

private static Logger logger = null;

static {
    System.setProperty("log4j.configurationFile", "C:\\Users\\jhaas\\Workspace_3.0.30\\RestClient\\conf\\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

private static Logger logger = null;

static {
    System.setProperty("log4j.configurationFile", "C:\\Users\\jhaas\\Workspace_3.0.30\\RestClient\\conf\\log4j.properties");
}

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.

眼眸 2025-01-12 21:30:07

log4j.xmllog4j.properties 文件应位于根源文件夹 src/main/resources 下。

创建新的源文件夹 src/main/resources 并将 propertyxml 文件放置在其中。应该可以解决问题

log4j.xml or log4j.properties files should be under root source folder src/main/resources.

Create new source folder src/main/resources and place the property or xml file there. It should solve the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文