1.導入jar
springboot默認是用logback的日志框架的,所以需要排除logback,不然會出現jar依賴沖突的報錯。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions><!-- 去掉springboot默認配置 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <!-- 引入log4j2依賴 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> |
2.配置文件
1.如果自定義了文件名,需要在application.yml中配置
2.默認名log4j2-spring.xml,就省下了在application.yml中配置
在applicaiton.yaml中添加配置
1
2
3
4
5
6
7
8
9
10
11
|
logging: #日志文件 config: classpath:log4g2.xml level: com.alibaba.nacos.client.config.impl: WARN cn.jay.repository: trace file: #${file.name} 后期可以改成${spring.application.name} path: /log/${file.name} file: name: dome |
在config中配置log4g2.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!-- status= "OFF" ,可以去掉,它的含義為是否記錄log4j2本身的event信息,默認是OFF --> <configuration status= "off" monitorInterval= "60" > <!-- <properties>--> <!-- <property name= "projectName" >--> <!-- riiot--> <!-- </property>--> <!-- </properties>--> <appenders> <!-- 開發環境用 --> <Console name= "debug_console" target= "SYSTEM_OUT" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "DEBUG" /> <ThresholdFilter level= "INFO" onMatch= "DENY" onMismatch= "NEUTRAL" /> </Filters> </Console> <Console name= "console" target= "SYSTEM_OUT" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "info" /> </Filters> </Console> <!-- 輸出日志到文件 每天一個文件(ERROR-FATAL級別) --> <RollingRandomAccessFile name= "AppErrorDailyRollingFile" fileName= "${sys:LOG_PATH}/AppError.log" append= "true" bufferedIO= "false" bufferSize= "256" filePattern= "${sys:LOG_PATH}/AppError.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "ERROR" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 輸出日志到文件 每天一個文件(WARN級別) --> <RollingRandomAccessFile name= "AppWarnDailyRollingFile" fileName= "${sys:LOG_PATH}/AppWarn.log" append= "true" bufferedIO= "true" bufferSize= "10240" immediateFlush= "false" filePattern= "${sys:LOG_PATH}/AppWarn.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "WARN" /> <ThresholdFilter level= "ERROR" onMatch= "DENY" onMismatch= "NEUTRAL" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 輸出日志到文件 每天一個文件(INFO級別) --> <RollingRandomAccessFile name= "AppInfoDailyRollingFile" fileName= "${sys:LOG_PATH}/AppAccess.log" append= "true" bufferedIO= "true" bufferSize= "409600" immediateFlush= "false" filePattern= "${sys:LOG_PATH}/AppAccess.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "INFO" /> <ThresholdFilter level= "WARN" onMatch= "DENY" onMismatch= "NEUTRAL" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 輸出日志到文件 每天一個文件(INFO級別) --> <RollingRandomAccessFile name= "DBAccessDailyRollingFile" fileName= "${sys:LOG_PATH}/DBAccess.log" append= "true" bufferedIO= "true" bufferSize= "409600" immediateFlush= "false" filePattern= "${sys:LOG_PATH}/DBAccess.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "INFO" /> <ThresholdFilter level= "WARN" onMatch= "DENY" onMismatch= "NEUTRAL" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 輸出日志到文件 每天一個文件(ERROR級別) --> <RollingRandomAccessFile name= "DBErrorDailyRollingFile" fileName= "${sys:LOG_PATH}/DBError.log" append= "true" bufferedIO= "false" bufferSize= "256" filePattern= "${sys:LOG_PATH}/DBError.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "WARN" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 輸出日志到文件 每天一個文件(ERROR級別) --> <RollingRandomAccessFile name= "SysErrorDailyRollingFile" fileName= "${sys:LOG_PATH}/SysError.log" append= "true" bufferedIO= "false" filePattern= "${sys:LOG_PATH}/SysError.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "WARN" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- 啟動日志 --> <RollingRandomAccessFile name= "BootLog" fileName= "${sys:LOG_PATH}/Boot.log" append= "false" bufferedIO= "false" filePattern= "${sys:LOG_PATH}/Boot.log.%d{yyyy-MM-dd}.log" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" /> <Filters> <ThresholdFilter level= "INFO" /> </Filters> <Policies> <TimeBasedTriggeringPolicy modulate= "true" interval= "1" /> </Policies> </RollingRandomAccessFile> <!-- <Kafka name= "SyncKafka" topic= "logs" syncSend= "false" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m" /> <Property name= "bootstrap.servers" > 192.168 . 1.49 : 9092 </Property> <Property name= "acks" > 0 </Property> <Property name= "compression.type" >gzip</Property> <Property name= "max.block.ms" > 10000 </Property> <Filters> <ThresholdFilter level= "INFO" /> </Filters> </Kafka> <!– 異步發送kafka –> <Async name= "Kafka" bufferSize= "512" blocking= "false" > <AppenderRef ref= "SyncKafka" /> </Async> <Kafka name= "KafkaErrLog" topic= "errLogTopic" syncSend= "false" > <PatternLayout pattern= "[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m" /> <Property name= "bootstrap.servers" > 192.168 . 1.49 : 9092 </Property> <Property name= "acks" > 0 </Property> <Property name= "compression.type" >gzip</Property> <Filters> <ThresholdFilter level= "ERROR" /> </Filters> </Kafka> <!– 異步發送kafka –> <Async name= "AsyncKafkaErrLog" bufferSize= "512" blocking= "false" > <AppenderRef ref= "KafkaErrLog" /> </Async> --> </appenders> <loggers> <!-- 只把包名是com.cmsr的日志輸出到文件 --> <logger name= "com.cmsr" additivity= "false" level= "debug" > <appender-ref ref= "AppErrorDailyRollingFile" /> <appender-ref ref= "AppWarnDailyRollingFile" /> <appender-ref ref= "AppInfoDailyRollingFile" /> <appender-ref ref= "console" /> <appender-ref ref= "debug_console" /> </logger> <!-- 只把包名是com.cmsr.sicp.common.mybatis(DB正常執行,異常分別寫到不同的log文件)的日志輸出到文件 --> <logger name= "com.cmsr.sicp.common.mybatis" additivity= "false" level= "debug" > <appender-ref ref= "DBAccessDailyRollingFile" /> <appender-ref ref= "DBErrorDailyRollingFile" /> <appender-ref ref= "console" /> </logger> <!-- 過濾springframework輸出,提高啟動速度 (生產環境中需要整體刪除)--> <logger name= "org.springframework" additivity= "false" level= "warn" > <appender-ref ref= "console" /> </logger> <!-- 啟動日志單獨輸出 --> <logger name= "com.cmsr.Launcher" additivity= "false" level= "info" > <appender-ref ref= "BootLog" /> <appender-ref ref= "console" /> </logger> <!-- 定義全局。其他包的日志只輸出到控制臺,不輸出到日志文件 --> <root level= "error" > <appender-ref ref= "SysErrorDailyRollingFile" /> <appender-ref ref= "console" /> </root> </loggers> </configuration> |
到此這篇關于springboot中整合log4g2的文章就介紹到這了,更多相關springboot整合log4g2內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_38092788/article/details/121096315