如何配置我的pom以使用一个log4j.properties文件进行main和test?

时间:2014-11-18 17:36:44

标签: java maven log4j pom.xml

我在src / main / resources中有一个log4j.properties文件。当我运行我的测试时,仅发生控制台记录,但不记录文件追加器记录。我想为我的测试指向相同的属性文件。

我已经了解到这可以在我的pom文件中使用测试范围和-D参数。这是怎么做到的?

这是我的log4j.properties文件:

# ROOT CATEGORY is used to set level of logger and appender name 
log4j.rootCategory=DEBUG, CA, RA 
# DEBUG is a root level and FA, CA, DA are appender name (appender name can be different) 

# ---------- CA is set to be a ConsoleAppender ---------- 

# Write to Console(stdout or stderr). 
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
# This appender will only log messages with priority equal to or higher than the one specified here 
# hierarchy of level is (from lower to higher): DEBUG,INFO,WARN,ERROR,FATAL 
log4j.appender.CA.Threshold=DEBUG 
# appender layouts (log formats) 
#log4j.appender.CA.layout=org.apache.log4j.SimpleLayout 
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
# For a pattern layout, specify the pattern (Default is %m%n which is fastest) 
log4j.appender.CA.layout.ConversionPattern=[%-5p] %c - %m%n 

# ---------- RA is set to be a RollingFileAppender ---------- 

# Write log to a file, roll the file after some size 
log4j.appender.RA=org.apache.log4j.RollingFileAppender 
# This appender will only log messages with priority equal to or higher than the one specified here 
log4j.appender.RA.Threshold=DEBUG 
# The name of log file 
log4j.appender.RA.File= \log4j.log 
# The maximum log file size 
log4j.appender.RA.MaxFileSize=10MB 
# Don't append, overwrite 
#log4j.appender.RA.Append=false 
# Keep backup file(s) (backups will be in filename.1, .2 etc.) 
log4j.appender.RA.MaxBackupIndex=1 
# appender layouts (log formats) 
#log4j.appender.RA.layout=org.apache.log4j.SimpleLayout 
log4j.appender.RA.layout=org.apache.log4j.PatternLayout 
# For a pattern layout, specify the pattern (Default is %m%n which is fastest) 
log4j.appender.RA.layout.ConversionPattern=[%-5p] %c - %m%n 

1 个答案:

答案 0 :(得分:0)

您可以向测试运行器添加命令行设置/系统属性。我假设您使用surefire,然后就像this

一样
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18</version>
    <configuration>
      <systemPropertyVariables>
        <log4j.configuration>log4j.properties</ log4j.configuration>
      </systemPropertyVariables>
    </configuration>
  </plugin>