【发布时间】:2020-09-24 05:47:58
【问题描述】:
在我的 logback-spring.xml 中添加 <springProfile> 时遇到问题。就我而言,我需要从 application.properties 文件中获取文件名和路径,并提供基于日期的滚动策略。所以无法在logback-spring.xml中指定文件名和路径。
因此,每当我从 IDE 或通过可执行 jar 文件运行特定于配置文件的项目时,我都会创建两个日志文件,一个位于当前工作目录,另一个位于应用程序中提到的指定文件路径位置。属性文件。
我有三个基于三种不同环境的配置文件,即本地、开发和生产。
对于本地,我使用的是 spring OOTB application.properties 文件。
spring.profiles.active=local
server.port=8080
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
对于 dev 和 prod,我分别使用 application-dev.properties 和 application-prod.properties。
应用程序-dev.properties
spring.profiles.active=dev
server.port=9090
#logging
logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs/dev
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml
这是我的 logback-spring.xml。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include
resource="org/springframework/boot/logging/logback/base.xml" />
<springProfile name="prod">
<property resource="application-prod.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %X{clientapp} %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="uat">
<property resource="application-uat.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
<springProfile name="local">
<property resource="application.properties" />
<appender name="SAVE-TO-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file.path}/${logging.file.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="SAVE-TO-FILE" />
</root>
</springProfile>
</configuration>
【问题讨论】:
标签: java spring-boot slf4j spring-logback