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