服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - logback配置中变量和include的应用方式

logback配置中变量和include的应用方式

2021-12-09 12:54SonOfWind0311 Java教程

这篇文章主要介绍了logback配置中变量和include的应用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

logback配置中变量和include的应用

logback配置在实际应用中往往会遇到appender的配置中存在大量的重复内容,但是logback又不像编程语言一样支持继承,采用include和变量结合可以一定程度上解决这个问题。

Example:

?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name=WarnLogFile">
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
    </encoder>
    <file>${LOGS_DIR}/warn.log</file>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>40MB</maxFileSize>
    </triggeringPolicy>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOGS_DIR}/warn%i.log.gz</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>21</maxIndex>
    </rollingPolicy>
</appender>
<appender class="ch.qos.logback.classic.AsyncAppender" name="Warn">
    <queueSize>2048</queueSize>
    <includeCallerData>true</includeCallerData>
    <discardingThreshold>0</discardingThreshold>
    <appender-ref ref="WarnLogFile" />
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>WARN</level>
    </filter>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="InfoLogFile">
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
    </encoder>
    <file>${LOGS_DIR}/info.log</file>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOGS_DIR}/info%i.log.gz</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>21</maxIndex>
    </rollingPolicy>
</appender>
<appender class="ch.qos.logback.classic.AsyncAppender" name="info">
    <queueSize>2048</queueSize>
    <includeCallerData>true</includeCallerData>
    <discardingThreshold>0</discardingThreshold>
    <appender-ref ref="infoLogFile" />
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="DebugLogFile">
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
    </encoder>
    <file>${LOGS_DIR}/debug.log</file>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>5MB</maxFileSize>
    </triggeringPolicy>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOGS_DIR}/debug%i.log.gz</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>21</maxIndex>
    </rollingPolicy>
</appender>
<appender class="ch.qos.logback.classic.AsyncAppender" name="Debug">
    <queueSize>2048</queueSize>
    <includeCallerData>true</includeCallerData>
    <discardingThreshold>0</discardingThreshold>
    <appender-ref ref="DebugLogFile" />
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>DEBUG</level>
    </filter>
</appender>

可以看到以上的三个定义存在大量的重复内容会导致logback文件很大,不容易维护,结合变量和include则可以一定程度上解决这个问题。

首先把配置内容抽取一个公共文件default.xml

把不同的内容用变量定义:

?
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
<included>
    <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="${FILE_APPENDER}">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
        </encoder>
        <file>${LOGS_DIR}/${LOGFILE}.log</file>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>5MB</maxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_DIR}/${LOGFILE}%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>21</maxIndex>
        </rollingPolicy>
    </appender>
    <appender class="ch.qos.logback.classic.AsyncAppender" name="${ASYNC_APPENDER}">
        <queueSize>2048</queueSize>
        <includeCallerData>true</includeCallerData>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="${FILE_APPENDER}" />
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${LOG_LEVEL}</level>
        </filter>
    </appender>
</included>

然后基于公共文件定义各自的配置

warn.xml:

?
1
2
3
4
5
6
7
8
<included>
    <property scope="local" name="LOGFILE" value="warn" />
    <property scope="local" name="FILE_APPENDER" value="WarnLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="warn" />
    <property scope="local" name="LOG_LEVEL" value="WARN" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

info.xml:

?
1
2
3
4
5
6
7
8
<included>
    <property scope="local" name="LOGFILE" value="info" />
    <property scope="local" name="FILE_APPENDER" value="InfoLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="info" />
    <property scope="local" name="LOG_LEVEL" value="INFO" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

debug.xml:

?
1
2
3
4
5
6
7
8
<included>
    <property scope="local" name="LOGFILE" value="debug" />
    <property scope="local" name="FILE_APPENDER" value="DebugLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="debug" />
    <property scope="local" name="LOG_LEVEL" value="DEBUG" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

最后在logback.xml中就可以include上面定义的文件

?
1
2
3
4
5
6
7
8
9
<include file="${LOGS_CUSTOM_DIR}/warn.xml"/>
<include file="${LOGS_CUSTOM_DIR}/info.xml"/>
<include file="${LOGS_CUSTOM_DIR}/debug.xml"/>
<root level="DEBUG">
    ...
    <appender-ref ref="warn"/>
    <appender-ref ref="info"/>
    <appender-ref ref="debug"/>
</root>

logback.xml导入(include)其他项目配置的logback.xml文件

1、其他项目存在的logback.xml文件需要下面这种形式的配置 ​

下面的这个配置文件的意思是定义了两个appender,这两个appender中名为ASYNC-LOG的appender是将com.xx.cloud.trail.client.async路径下面产生的级别为error的日志进行收集,保存在定义的文件夹下面,另一个appender同理,如果这个logback.xml文件想在其他的项目中被引用那么就需要用在iucluded标签将定义的appender跟logger包围起来,表示这个是可以被导入其他项目的

?
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"?>
    <included>
    <appender name="ASYNC_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>/var/log/xx/cloud/trail/async/error.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <!-- 日志输出编码 -->
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                </pattern>
            </layout>
        </encoder>
        <!--日志文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <appender name="AOP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>/var/log/xx/cloud/trail/aop/error.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <!-- 日志输出编码 -->
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                </pattern>
            </layout>
        </encoder>
        <!--日志文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!--需要将日志单独输出的包路径-->
    <logger name="com.xx.cloud.trail.client.async" level="ERROR" additivity="false">
        <appender-ref ref="ASYNC_LOG"/>
    </logger>
    <!--需要将日志单独输出的包路径-->
    <logger name="com.xx.cloud.trail.client.aop" level="ERROR" additivity="false">
        <appender-ref ref="AOP_LOG"/>
    </logger>
    </included>

2、其他项目的配置

假如1中的配置logback.xml文件的名称为logback-trail.xml,那么需要在其他的项目的logback.xml文件中加入一句

表示导入其他项目的logback文件,这样就能将其他项目中的配置文件进行导入

?
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
<?xml version="1.0" encoding="UTF-8"?>
<!--
    scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
    scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
    debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
-->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <include resource="logback-trail.xml"/>
    <!--配置日志输出到的路径-->
    <springProperty scop="context" name="logging.path" source="logging.path"
                    defaultValue="logging-path-not-find"/>
    <property name="log.path" value="${logging.path}"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="TRAIL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${logging.path}/trail/trail.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder
                class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <!-- 日志输出编码 -->
            <layout class="ch.qos.logback.classic.PatternLayout">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                </pattern>
            </layout>
        </encoder>
        <!--日志文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!-- 日志输出级别 -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="TRAIL_LOG"/>
    </root>
</configuration>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/SonOfWind0311/article/details/86751159

延伸 · 阅读

精彩推荐