Java 如何配置我的pom,以便在main和test中使用一个log4j.properties文件?

Java 如何配置我的pom,以便在main和test中使用一个log4j.properties文件?,java,maven,log4j,pom.xml,Java,Maven,Log4j,Pom.xml,我在src/main/resources中有一个log4j.properties文件。当我运行测试时,只会发生控制台日志记录,而不会发生文件追加器日志记录。我想为我的测试指向相同的属性文件 我了解到,在pom文件中使用测试范围和a-D参数可以实现这一点。这是怎么做到的 这是我的log4j.properties文件: # ROOT CATEGORY is used to set level of logger and appender name log4j.rootCategory=DEBUG,

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

我了解到,在pom文件中使用测试范围和a-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 

您可以将命令行设置/系统属性添加到测试运行程序。我知道你用的是surefire,然后是这样做的:

<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>