SpringBoot 使用 AOP 来抽取日志

这些代码我没亲自写过,之前很少碰日志之类的东西。
是我的大哥 Philips.Li 写的。

功能概要:
1、拦截方法参数1,和方法返回值
2、把这些关键的数据拦截下来,传入elk,方便后续问题查看

在这里插入图片描述

关于日志采取配置,完成 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<contextName>${HOSTNAME}</contextName>
<springProperty scope="context" name="appName" source="spring.application.name"/>

<springProfile name="local">
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} %X{correlationId} - %logger - %F:%L%n</pattern>
</layout>
</appender>

<appender name="logFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 一天一个日志文件 -->
<fileNamePattern>elklog/sfc_order_bff_${HOSTNAME}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>5MB</maxFileSize> <!-- 日志最大尺寸,日志文件过大会使的编辑器打开非常慢 -->
<maxHistory>3</maxHistory> <!-- 保存3天 -->
<totalSizeCap>2GB</totalSizeCap> <!-- 总日志大小 -->
</rollingPolicy>
<!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- <FileNamePattern>elklog/%d{yyyy-MM-dd_HH-mm}.log</FileNamePattern>-->
<!-- <timeBasedFileNamingAndTriggeringPolicy class="com.decathlon.sfc.order.bff.core.config.FileNamePatternTimeBasedFileNamingAndTriggeringPolicy">-->
<!-- <multiple>10</multiple>-->
<!-- </timeBasedFileNamingAndTriggeringPolicy>-->
<!--</rollingPolicy>-->
<encoder>
<pattern>{"@timestamp": "%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}", "logger_name": "%X{logger_name}","thread_name": "%thread","level": "%level", "instance_id": "%contextName", "module_name": "${appName}", "message": %marker%m%ex{full}, "uuid": "%X{uuid}"} %n</pattern>
</encoder>
</appender>

<logger name="com.decathlon.sfc.order.bff.core.aspect" additivity="false">
<appender-ref ref="logFileAppender"/>
</logger>

<logger name="org" level="off"/>
<logger name="org.springframework.boot.web.embedded.tomcat.TomcatWebServer" level="info"/>
<logger name="springfox" level="off"/>
<logger name="com.jd" level="off"/>
<root level="INFO">
<appender-ref ref="consoleAppender"/>
</root>
</springProfile>

<springProfile name="preprod">
<!--<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>DEBUG</level>-->
<!-- </filter>-->
<!-- <layout class="ch.qos.logback.classic.PatternLayout">-->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} %X{correlationId} - %logger - %F:%L%n</pattern>-->
<!-- </layout>-->
<!--</appender>-->

<appender name="consolelogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>consolelog/%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} %X{correlationId} - %logger - %F:%L%n</pattern>
</encoder>
</appender>

<appender name="logFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 一天一个日志文件 -->
<fileNamePattern>elklog/sfc_order_bff_${HOSTNAME}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>5MB</maxFileSize> <!-- 日志最大尺寸,日志文件过大会使的编辑器打开非常慢 -->
<maxHistory>3</maxHistory> <!-- 保存3天 -->
<totalSizeCap>2GB</totalSizeCap> <!-- 总日志大小 -->
</rollingPolicy>
<!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- <FileNamePattern>elklog/%d{yyyy-MM-dd_HH-mm}.log</FileNamePattern>-->
<!-- <timeBasedFileNamingAndTriggeringPolicy class="com.decathlon.sfc.order.bff.core.config.FileNamePatternTimeBasedFileNamingAndTriggeringPolicy">-->
<!-- <multiple>10</multiple>-->
<!-- </timeBasedFileNamingAndTriggeringPolicy>-->
<!--</rollingPolicy>-->
<encoder>
<pattern>{"@timestamp": "%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}", "logger_name": "%X{logger_name}","thread_name": "%thread","level": "%level", "instance_id": "%contextName", "module_name": "${appName}", "message": %marker%m%ex{full}, "uuid": "%X{uuid}"} %n</pattern>
</encoder>
</appender>

<logger name="com.decathlon.sfc.order.bff.core.aspect" additivity="false">
<appender-ref ref="logFileAppender"/>
</logger>

<logger name="org" level="off"/>
<logger name="org.springframework.boot.web.embedded.tomcat.TomcatWebServer" level="info"/>
<logger name="springfox" level="off"/>
<logger name="com.jd" level="off"/>
<root level="INFO">
<!--<appender-ref ref="consoleAppender"/>-->
<appender-ref ref="consolelogAppender"/>
</root>
</springProfile>

<springProfile name="production">
<!--<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>DEBUG</level>-->
<!-- </filter>-->
<!-- <layout class="ch.qos.logback.classic.PatternLayout">-->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} %X{correlationId} - %logger - %F:%L%n</pattern>-->
<!-- </layout>-->
<!--</appender>-->

<appender name="consolelogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>consolelog/%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} %X{correlationId} - %logger - %F:%L%n</pattern>
</encoder>
</appender>

<appender name="logFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 一天一个日志文件 -->
<fileNamePattern>elklog/sfc_order_bff_${HOSTNAME}_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>5MB</maxFileSize> <!-- 日志最大尺寸,日志文件过大会使的编辑器打开非常慢 -->
<maxHistory>3</maxHistory> <!-- 保存3天 -->
<totalSizeCap>2GB</totalSizeCap> <!-- 总日志大小 -->
</rollingPolicy>
<!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- <FileNamePattern>elklog/%d{yyyy-MM-dd_HH-mm}.log</FileNamePattern>-->
<!-- <timeBasedFileNamingAndTriggeringPolicy class="com.decathlon.sfc.order.bff.core.config.FileNamePatternTimeBasedFileNamingAndTriggeringPolicy">-->
<!-- <multiple>10</multiple>-->
<!-- </timeBasedFileNamingAndTriggeringPolicy>-->
<!--</rollingPolicy>-->
<encoder>
<pattern>{"@timestamp": "%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}", "logger_name": "%X{logger_name}","thread_name": "%thread","level": "%level", "instance_id": "%contextName", "module_name": "${appName}", "message": %marker%m%ex{full}, "uuid": "%X{uuid}"} %n</pattern>
</encoder>
</appender>

<logger name="com.decathlon.sfc.order.bff.core.aspect" additivity="false">
<appender-ref ref="logFileAppender"/>
</logger>

<logger name="org" level="off"/>
<logger name="org.springframework.boot.web.embedded.tomcat.TomcatWebServer" level="info"/>
<logger name="springfox" level="off"/>
<logger name="com.jd" level="off"/>
<root level="INFO">
<!--<appender-ref ref="consoleAppender"/>-->
<appender-ref ref="consolelogAppender"/>
</root>
</springProfile>

</configuration>