gpt4 book ai didi

java - 如何配置我的 pom 以使用一个 log4j.properties 文件作为主文件和测试文件?

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:49 26 4
gpt4 key购买 nike

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

最佳答案

您可以将命令行设置/系统属性添加到测试运行程序。我假设你使用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>

关于java - 如何配置我的 pom 以使用一个 log4j.properties 文件作为主文件和测试文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com