基于Spring Boot Profile的日志记录示例样本

我们希望在Spring Boot中为不同的配置文件使用不同的日志记录配置,例如在本地运行中,我们只希望控制台日志记录和用于生产,我们希望文件记录日志支持每天滚动日志文件。

我提出了一个示例logback配置,该配置将在所有应用程序中使用。 在src/main/resources创建一个名为logback-spring.xml的文件,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
  <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOG_FILE}.%d</fileNamePattern>
    </rollingPolicy>
  </appender>

  <springProfile name="local">
    <root level="INFO">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="FILE" />
    </root>
  </springProfile>
  <springProfile name="test,prod">
    <root level="INFO">
      <appender-ref ref="FILE" />
    </root>
  </springProfile>

</configuration>

我们使用的是Spring Boot提供的默认控制台附加程序,但是提供了我们自己的基于日常滚动的文件附加程序。 我主要复制了base.xml并对其进行了更新以满足我的需求。

翻译自: https://www.javacodegeeks.com/2018/01/sample-logback-configuration-spring-boot-profile-based-logging.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值