且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-22 15:43:28

要解决您的问题,请将您的log4j配置文件(即 log4j.xml log4j.properties )在 src 文件夹中,它应该可以正常工作。另外你不需要这两个文件( 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

screen http://s017.radikal.ru/i433/1202/81/b2739aedc39d.jpg.

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

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.