2013-11-26 72 views
1

我試圖運行Log4j的groovy腳本。 腳本運行正常,但log4j未初始化。我已經將log4j.properties放置在src/main/resources中,並且還將資源包含在pom.xml的build部分中。下面是運行maven時未初始化log4j執行

 <plugin> 
      <groupId>org.codehaus.gmaven</groupId> 
      <artifactId>gmaven-plugin</artifactId> 
      <version>1.5</version> 
      <dependencies> 
       <dependency> 
        <groupId>org.codehaus.gmaven.runtime</groupId> 
        <artifactId>gmaven-runtime-2.0</artifactId> 
        <version>1.5</version> 
       </dependency> 
       <dependency> 
        <groupId>org.codehaus.groovy</groupId> 
        <artifactId>groovy</artifactId> 
        <version>2.1.9</version> 
       </dependency> 
       <dependency> 
        <groupId>log4j</groupId> 
        <artifactId>log4j</artifactId> 
        <version>1.2.17</version> 
       </dependency> 
      </dependencies> 
      <configuration> 
       <providerSelection>2.0</providerSelection> 
       <source> 
        ${pom.basedir}/src/main/groovy/Hello.groovy 
       </source> 
      </configuration> 


      <executions> 
       <execution> 
        <goals> 
         <goal>testCompile</goal> 
         <goal>compile</goal> 
         <goal>generateStubs</goal> 
         <goal>execute</goal> 
        </goals> 
       </execution> 

      </executions> 
     </plugin> 

我用下面的命令來執行腳本的pom.xml的一部分:在腳本執行結束

mvn org.codehaus.gmaven:gmaven-plugin:1.5:execute 

我得到以下警告:

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

回答

0

我做了類似的事情,必須在我的腳本的頂部顯式初始化log4j。

import org.apache.log4j.PropertyConfigurator 

def config = new ConfigSlurper().parse(new File( 
        "${basedir.canonicalPath}/src/main/scripts/log4j.groovy").toURL()) 
PropertyConfigurator.configure(config.toProperties()) 

然後正常的日誌記錄(在Java調用我)工作正常。

這裏是我的測試log4j.groovy文件。

log4j.appender.stdout = "org.apache.log4j.ConsoleAppender" 
log4j.appender."stdout.layout" = "org.apache.log4j.PatternLayout" 
log4j.appender."stdout.layout.ConversionPattern" = "%-5p [%c{1}]: %m%n" 
log4j.rootLogger="error,stdout" 
log4j.logger.com.myCompany = "info,stdout" 
log4j.additivity.com.myCompany = false