本文介绍了springboot集成slf4j+log4j2的示例代码,分享给大家,具体如下:
maven依赖
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
|
<!--增加log4j2依赖↓-->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-log4j2</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-actuator</artifactid>
<exclusions>
<!-- 去除旧log依赖 -->
<exclusion>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-logging</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
<exclusions>
<!-- 去除旧log依赖 -->
<exclusion>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-logging</artifactid>
</exclusion>
</exclusions>
</dependency>
|
如果其它依赖有log日志冲突,可以加入下面配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<exclusions>
<exclusion>
<groupid>log4j</groupid>
<artifactid>*</artifactid>
</exclusion>
<exclusion>
<groupid>org.slf4j</groupid>
<artifactid>*</artifactid>
</exclusion>
<exclusion>
<groupid>org.apache.logging.log4j</groupid>
<artifactid>*</artifactid>
</exclusion>
</exclusions>
|
log4j2.xml
放在resources目录下
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!--
6 个优先级从高到低依次为:off、fatal、error、warn、info、debug、trace、 all。
如果设置优先级为warn,那么off、fatal、error、warn 4 个级别的log能正常输出
设置为off 表示不记录log4j2本身的日志,
-->
<!-- status:用来指定log4j本身的打印日志级别,monitorinterval:指定log4j自动重新配置的监测间隔时间 -->
<configuration status= "info" monitorinterval= "30" >
<!-- 自己设置属性,后面通过${}来访问 -->
<!-- <properties>
<property name= "log_home" >${web:rootdir}/logs</property>
</properties>-->
<appenders>
<!--appender 1 . 输出到console控制台,指定输出格式和过滤器等级为info -->
<console name= "console" target= "system_out" >
<!--thresholdfilter指定日志消息的输出最低层次-->
<thresholdfilter level= "all" onmatch= "accept" onmismatch= "deny" />
<patternlayout pattern= "%d{hh:mm:ss.sss} %-5level %class{36} %l %m - %msg%xex%n" />
</console>
<!--appender 2 . 输出到滚动保存的文件, 触发保存日志文件的条件是日志文件大于3kb,只保存最新的 10 个日志-->
<file name= "alllog" filename= "${log_home}/all.log" >
<thresholdfilter level= "all" onmatch= "accept" onmismatch= "deny" />
<patternlayout charset= "utf-8" pattern= "%d{yyyy.mm.dd 'at' hh:mm:ss z} %-5level %class{36} %l %m - %msg%xex%n" />
</file>
<!--appender 3 . 输出到滚动保存的文件, 触发保存日志文件的条件是日志文件大于3kb,只保存最新的 10 个日志-->
<rollingfile name= "debuglog" filename= "${log_home}/debug.log" filepattern= "${log.path}/debug-%i.log" >
<thresholdfilter level= "debug" onmatch= "accept" onmismatch= "deny" />
<patternlayout charset= "utf-8" pattern= "[%-5level][%d{yyyy-mm-dd hh:mm:ss}][%f:%l] - %m%n" />
<sizebasedtriggeringpolicy size= "3kb" />
<!-- defaultrolloverstrategy 中的参数max,可以限制 sizebasedtriggeringpolicy中size超出后,只保留max个存档-->
<defaultrolloverstrategy max= "10" />
</rollingfile>
<!--appender 4 . 输出到滚动保存的文件, 触发保存日志文件的条件是每分钟第一次的日志事件。error日志是按分钟产生日志 -->
<rollingfile name= "errorlog" filename= "${log_home}/error.log"
filepattern= "${log.path}/error-%d{yyyy-mm-dd_hh-mm}.log" >
<thresholdfilter level= "error" onmatch= "accept" onmismatch= "deny" />
<patternlayout charset= "utf-8" pattern= "[%-5level][%d{yyyy-mm-dd hh:mm:ss}][%c:%f:%l] - %m%n" />
<timebasedtriggeringpolicy/>
</rollingfile>
<rollingfile name= "rollingfile" filename= "${log_home}/rar.log"
filepattern= "${log_home}/$${date:yyyy-mm}/${file_name}-%d{mm-dd-yyyy}-%i.log.gz" >
<patternlayout charset= "utf-8" pattern= "%d{yyyy-mm-dd 'at' hh:mm:ss z} %-5level %class{36} %l %m - %msg%xex%n" />
<!--日志文件最大值 第二天压缩-->
<policies>
<timebasedtriggeringpolicy/>
<sizebasedtriggeringpolicy size= "10 mb" />
</policies>
</rollingfile>
</appenders>
<!--root 默认加载-->
<loggers>
<root level= "debug" >
<appender-ref ref= "console" />
<!--<appender-ref ref= "alllog" />-->
<!--<appender-ref ref= "debuglog" />-->
<!--<appender-ref ref= "errorlog" />-->
<!--<appender-ref ref= "rollingfile" />-->
</root>
</loggers>
</configuration>
|
logtest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import org.slf4j.logger;
import org.slf4j.loggerfactory;
public class logtest {
public final logger logger=loggerfactory.getlogger(getclass());
public static void main(string[] args) {
log.trace( "trace" );
log.debug( "debug" );
log.warn( "warn" );
log.info( "info" );
log.error( "error" );
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000015568449