2010-10-15 81 views
0

我有一個簡單的log4j.property文件和一個使用log4j的簡單程序。但是,當我跑我的罐子我只看到這個log4j無法用我的log4j.properties初始化

log4j:WARN No appenders could be found for logger (package com.mycompany.hellolog4j). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

這是我如何使用記錄儀:

package com.mycompany.hellolog4j; 

import java.io.File; 
import java.io.IOException; 
import org.apache.log4j.*; 

public class SimpleLogging 
{ 

    public static void main(String args[]) throws IOException { 

     Logger logger = Logger.getLogger(SimpleLogging.class.getPackage().toString()); 
     // SimpleLogging demo = new SimpleLogging(rbName); 
     //demo.doLogging(name); 
     logger.info("printing the message..."); 
    } 
} 

這是我的pom.xml的一部分

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.3.1</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <classpathLayoutType>custom</classpathLayoutType> 
          <mainClass>com.mycompany.hellolog4j.SimpleLogging</mainClass> 
          <customClasspathLayout>../lib/log4j.jar ../log4j.properties</customClasspathLayout> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 

這裏是我的log4j.properties

log4j.rootLogger = DEBUG, testAppender 

log4j.logger.com.mycompany.hellolog4j=DEBUG, testAppender 

log4j.appender.testAppender=org.apache.log4j.ConsoleAppender 

log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.testAppender.layout.conversionPattern=%p-%m%n 

現在從我的項目目錄中運行jar文件。這是我的項目目錄

drwxr-xr-x 5 proger users 4096 2010-10-15 22:59 ./ 
drwxr-xr-x 3 proger users 4096 2010-10-15 22:27 ../ 
drwxr-xr-x 2 proger users 4096 2010-10-14 22:54 lib/ 
-rw-r--r-- 1 proger users 291 2010-10-15 23:00 log4j.properties 
-rw-r--r-- 1 proger users 1689 2010-10-15 22:59 nbactions.xml 
-rw-r--r-- 1 proger users 1987 2010-10-15 22:56 pom.xml 
drwxr-xr-x 4 proger users 4096 2010-10-13 23:01 src/ 
drwxr-xr-x 6 proger users 4096 2010-10-15 22:59 target/ 

回答

2

把log4j.properties文件中的src/main /資源使得它打包到你的JAR文件,從而在classpath。