Log4j在简单的应用程序中找不到属性文件 [英] Log4j cannot find properties file in a simple application

查看:183
本文介绍了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.0encoding =UTF-8?> 
<!DOCTYPE log4j:configuration SYSTEMlog4j.dtd>

< log4j:configuration xmlns:log4j =http://jakarta.apache.org/log4j/>

< appender name =consoleclass =org.apache.log4j.ConsoleAppender>
< param name =Targetvalue =System.out/>
< layout class =org.apache.log4j.PatternLayout>
< param name =ConversionPatternvalue =aa%p%c:%m%n/>
< / layout>
< / appender>
<! - 根记录器 - >
< 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没有找到appender用于记录器(ru.log4j.log4jhelloworld.MyClass)。 
log4j:WARN请正确初始化log4j系统。
log4j:WARN有关详细信息,请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig。

我不知道我应该如何创建和连接其他支持文件,例如 log4j.xml log4j.properties 。并确定他们是否需要?他们应该在什么目录中找到?我还应该注册什么?这是我的项目结构



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



我猜这个错误。我的问题是什么?请帮助我!

解决方案

要解决您的问题,请将您的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.

这篇关于Log4j在简单的应用程序中找不到属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆