0

我想用 log4j2 配置文件实现 spring 5,比如 dev、qa 和 prod。

目前,这就是我使用 log4j2 运行 spring 5 的方式,我在命令行中加载 log4j2 属性

mvn tomcat8:run -Dlog4j.configurationFile=file:%path_to_log4j2_prop%/log4j2.properties

这是我的 log4j2 属性:

status = debug
name = PropertiesConfig
 
#Make sure to change log file path as per your need
property.environment=D:/WhiteCoats-New-Workspace/spring-5
property.servicesfilename = ${environment}/wc.out
 
filters = threshold
 
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
 
appenders = services
 
appender.services.type = RollingFile
appender.services.name = ServiceFile
appender.services.fileName = ${servicesfilename}
appender.services.filePattern = ${environment}/wc.out-%d{yyyy-MM-dd}-%i
appender.services.layout.type = PatternLayout
appender.services.layout.pattern = [ %d{yyyy-MMM-dd HH:mm:ss a} ] - [%t] %-5level %logger{36} - %msg%n
appender.services.policies.type = Policies
appender.services.policies.time.type = TimeBasedTriggeringPolicy
appender.services.policies.time.interval = 1
appender.services.policies.time.modulate = true
appender.services.policies.size.type = SizeBasedTriggeringPolicy
appender.services.policies.size.size=5MB
appender.services.strategy.type = DefaultRolloverStrategy
appender.services.strategy.max = 10

loggers = services
 
#services logs
 
logger.services.name = com.vm
logger.services.level = debug
logger.services.additivity = false
logger.services.appenderRef.services.ref = ServiceFile

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = ServiceFile

在 spring 3 中,我使用了 log4j 并在 web.xml 中配置了路径。

我如何为 spring 5 实现 log4j2 的配置文件?

4

1 回答 1

0

在 Spring Boot 中,如果您需要更改属性log4j.configurationFile,在application.yml. 如果您使用配置文件,您可以定义默认值和覆盖值。例如:

log4j:
    configurationFile: %path_to_log4j2_prop%
---
spring:
    profiles: development
log4j:
    configurationFile: %path_to_dev_log4j2_prop%
---
spring:
    profiles: production
log4j:
    configurationFile: %path_to_prod_log4j2_prop%

如果您只使用 Spring,那么您可以使用@Configuration@Profile根据@PropertySource您的配置文件加载不同的属性文件,并且在这些文件中您可以log4j.configurationFile根据需要设置属性。

于 2020-06-25T13:00:01.767 回答