logback 多环境配置

在 logback.xml中使用:-

1
2
<!-- ${变量名:-默认值} -->
<property name="logger.path" value="${catalina.home:-./..}/logs"/>

application.yml 配置 profiles

1
2
3
spring:
profiles:
active: dev

在使用 logback-spring.xml 中使用 springProfile 配置,使用 logback-spring.xml 可以获得更多spring boot个性化配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="UTF-8"?>
<!-- For assistance related to logback-translator or configuration -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user -->
<!-- -->
<!-- For professional support please see -->
<!-- http://www.qos.ch/shop/products/professionalSupport -->
<!-- -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="logger.charset" value="UTF-8"/>
<property name="logger.path" value="${catalina.home}/logs"/>
<property name="logger.pattern" value="[%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5p)] %yellow(%t) %cyan(%c.%M\\(%L\\)) | %m%n"/>
<property name="logger.maxHistory" value="15"/>

<!--&lt;!&ndash; https://logback.qos.ch/manual/layouts.html#coloring &ndash;&gt;
&lt;!&ndash; 彩色日志依赖的渲染类 &ndash;&gt;
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
&lt;!&ndash; 彩色日志格式 &ndash;&gt;
<property name="logger.pattern" value="${CONSOLE_LOG_PATTERN:-%clr([%d{yyyy-MM-dd HH:mm:ss} %clr(${LOG_LEVEL_PATTERN:-%5p})]){faint} %clr(%c.%M\\(%L\\)){cyan} | %m%n}" />-->

<springProfile name="dev">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="${logger.charset}">
<pattern>${logger.pattern}</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="stdout"/>
</root>
</springProfile>
<springProfile name="prod">
<appender name="stdout"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${logger.path}/info.log</File>
<encoder charset="${logger.charset}">
<pattern>${logger.pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logger.path}/info.log.%d{yyyy.MM.dd}
</fileNamePattern>
<maxHistory>${logger.maxHistory}</maxHistory>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="stdout"/>
</root>
</springProfile>
</configuration>
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/7bd4851.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!