spring boot实战之配置使用logback进行日志记录 ,分享给大家
在这篇文章中我们将讨论在spring boot中使用logback,在spring boot中使用logback很简单
1、为了测试我们新建两个类
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
|
package com.xiaofangtech.sunt.controller;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import com.xiaofangtech.sunt.helper.loghelper;
@restcontroller
@requestmapping ( "log" )
public class logcontroller {
private final logger logger = loggerfactory.getlogger( this .getclass());
@requestmapping ( "writelog" )
public object writelog()
{
logger.debug( "this is a debug message" );
logger.info( "this is an info message" );
logger.warn( "this is a warn message" );
logger.error( "this is an error message" );
new loghelper().helpmethod();
return "ok" ;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.xiaofangtech.sunt.helper;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
public class loghelper {
private final logger logger = loggerfactory.getlogger( this .getclass());
public void helpmethod(){
logger.debug( "this is a debug message" );
logger.info( "this is an info message" );
logger.warn( "this is a warn message" );
logger.error( "this is an error message" );
}
}
|
2、运行,在浏览器中输入http://localhost:8080/log/writelog 将会看到以下结果
我们没有配置任何其它配置,就可以看到来自logback root logger的输出信息。虽然默认情况下logback是会打印debug级别的日志,但是我们注意到debug级别的日志没有记录下来,那是因为spring boot为logback提供了默认的配置文件,base.xml,另外spring boot 提供了两个输出端的配置文件console-appender.xml和file-appender.xml,base.xml引用了这两个配置文件。
以下是base.xml的内容,我们可以看到,root logger的日志级别被重写为info级别,这就是上面例子中debug级别的日志没有打印的原因
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!--
base logback configuration provided for compatibility with spring boot 1.1
-->
<included>
<include resource= "org/springframework/boot/logging/logback/defaults.xml" />
<property name= "log_file" value= "${log_file:-${log_path:-${log_temp:-${java.io.tmpdir:-/tmp}}/}spring.log}" />
<include resource= "org/springframework/boot/logging/logback/console-appender.xml" />
<include resource= "org/springframework/boot/logging/logback/file-appender.xml" />
<root level= "info" >
<appender-ref ref= "console" />
<appender-ref ref= "file" />
</root>
</included>
|
3、通过application.properties文件对logback进行配置
1
2
3
|
logging.file=log.log
logging.level.com.xiaofangtech.sunt.controller = debug
logging.level.com.xiaofangtech.sunt.helper = warn
|
配置记录日志到log.log,com.xiaofangtech.sunt.controller日志级别为debug,.com.xiaofangtech.sunt.helper中日志级别为warn
我们将会看到以下结果,按照配置的日志级别进行记录。
并且可以看到日志记录到了日志文件中
4、通过额外的文件配置logback
通过application.properties文件配置logback,对于大多数spring boot应用来说已经足够了,但是对于一些大型的企业应用来说似乎有一些相对复杂的日志需求。在spring boot中你可以在logback.xml或者在logback-spring.xml中对logback进行配置,相对于logback.xml,logback-spring.xml更加被偏爱。下面我们以logback-spring.xml为例。
新建logback-spring.xml,配置输出的日志都为warn级别
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?>
<configuration>
<include resource= "org/springframework/boot/logging/logback/base.xml" />
<logger name= "com.xiaofangtech.sunt.controller" level= "warn" additivity= "false" >
<appender-ref ref= "console" />
<appender-ref ref= "file" />
</logger>
<logger name= "com.xiaofangtech.sunt.helper" level= "warn" additivity= "false" >
<appender-ref ref= "console" />
<appender-ref ref= "file" />
</logger>
</configuration>
|
运行结果如下:
本文参考:https://springframework.guru/using-logback-spring-boot/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/sun_t89/article/details/52130839