spring boot logback 配置

为什么要使用logback ?

       ——在开发中不建议使用System.out因为大量的使用会增加资源的消耗。因为使用System.out是在当前线程执行的,写入文件也是写入完毕之后才继续执行下面的程序。而使用Log工具不但可以控制日志是否输出,怎么输出,它的处理机制也是通知写日志,继续执行后面的代码不必等日志写完。

       ——个人推荐使用SLF4J(Simple Logging Façade For Java)的logback来输出日志,其比log4j效率高。

    ——Spring Boot 提供了一套日志系统,logback是最优先的选择。

疑惑解答:logback,slf4j,log4j之间的关系

       Slf4j是The Simple Logging Facade for Java的简称,是一个简单日志门面抽象框架,它本身只提供了日志Facade API和一个简单的日志类实现,一般常配合Log4j,LogBack,java.util.logging使用。Slf4j作为应用层的Log接入时,程序可以根据实际应用场景动态调整底层的日志实现框架(Log4j/LogBack/JdkLog...);


LogBack和Log4j都是开源日记工具库,LogBack是Log4j的改良版本,比Log4j拥有更多的特性,同时也带来很大性能提升。详细数据可参照下面地址:Reasons to prefer logback over log4j

       LogBack官方建议配合Slf4j使用,这样可以灵活地替换底层日志框架。

(note: 为了优化log4j,以及更大性能的提升,Apache基金会已经着手开发了log4j 2.0, 其中也借鉴和吸收了logback的一些先进特性,目前log4j2还处于beta阶段)

Logback的结构

       LogBack被分为3个组件,logback-core, logback-classic 和 logback-access.

其中logback-core提供了LogBack的核心功能,是另外两个组件的基础。

logback-classic则实现了Slf4j的API,所以当想配合Slf4j使用时,需要将logback-classic加入classpath。

logback-access是为了集成Servlet环境而准备的,可提供HTTP-access的日志接口;

Slf4j+Logback的快速实践

spring-boot默认支持logback,所以无需引用任何以来只需要,配置application.properties即可,如果要功能丰富些,则配置下logback.xml。

一、在application.properties里配置的方式:

logging.file=./springboot.log

这是最简便的方法,默认级别是info,要改级别的话还要在appliacation.properties里增加一行 

logging.level.org.springframework.web=INFO


二、配置logback-spring.xml

在 src/main/resources 下面创建logback.xml (根据不同环境来定义不同的日志输出,那么取名为logback-spring.xml 即可,官方优先推荐使用-spring.*的配置方式)文件。

logback-spring.xml 文件:

1
2
3
4
5
6
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< configuration >
     < include  resource = "org/springframework/boot/logging/logback/base.xml"  />
     < logger  name = "org.springframework.web"  level = "INFO" />
  
  </ configuration >

在代码中调用:

1
2
3
import  org.slf4j.Logger;
import  org.slf4j.LoggerFactory;
private  Logger logger =  LoggerFactory.getLogger( this .getClass());

具体使用起来特别的简单,那么来解读一下配置文件。

配置文件解读:

logback-spring.xml文件:

1
2
3
4
5
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< configuration >
     < include  resource = "org/springframework/boot/logging/logback/base.xml"  />
     < logger  name = "org.springframework.web"  level = "INFO" />
</ configuration >

在这个文件定义了一个 捕获 org.springframework.web 的日志,日志级别是 INFO,上面引用的base.xml 文件内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml  version = "1.0"  encoding = "UTF-8" ?>
<!--
Base logback configuration provided for compatibility with Spring Boot 1.1
-->
  
< included >
     < includeresource = "org/springframework/boot/logging/logback/defaults.xml"  />
     < propertyname = "LOG_FILE" value = "${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}" />
     < includeresource = "org/springframework/boot/logging/logback/console-appender.xml"  />
     < includeresource = "org/springframework/boot/logging/logback/file-appender.xml"  />
     < rootlevel = "INFO" >
         < appender-refref = "CONSOLE"  />
         < appender-refref = "FILE"  />
     </ root >
</ included >

这个base.xml是Spring Boot的日志系统预先定义了一些系统变量:

       PID,当前进程ID{LOG_FILE},Spring Boot配置文件(application.properties|.yml)中logging.file的值,${LOG_PATH}, Spring Boot配置文件中logging.path的值 

       同时默认情况下包含另个appender——一个是控制台,一个是文件,分别定义在console-appender.xml和file-appender.xml中。同时对于应用的日志级别也可以通过application.properties进行定义:

如果在 logback.xml 和 application.properties 中定义了相同的配置(如都配置了 org.springframework.web)但是输出级别不同,则实际上 application.properties 的优先级高于 logback.xml *


多环境日志输出

根据不同环境(prod:生产环境,test:测试环境,dev:开发环境)来定义不同的日志输出,在 logback-spring.xml 中使用 springProfile 节点来定义,方法如下:

注意文件名称不是logback.xml,想使用spring扩展profile支持,要以logback-spring.xml命名】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< configuration >
     < include  resource = "org/springframework/boot/logging/logback/base.xml"  />
     < logger  name = "org.springframework.web"  level = "INFO" />
     < logger  name = "org.springboot.sample"  level = "TRACE"  />
    
     <!-- 测试环境+开发环境. 多个使用逗号隔开. -->
      < springProfile  name = "test,dev" >
         < logger  name = "org.springframework.web"  level = "INFO" />
         < logger  name = "org.springboot.sample"  level = "INFO"  />
         < logger  name = "com.kfit"  level = "info"  />
     </ springProfile >
  
    
     <!-- 生产环境. -->
     < springProfile  name = "prod" >
         < logger  name = "org.springframework.web"  level = "ERROR" />
         < logger  name = "org.springboot.sample"  level = "ERROR"  />
         < logger  name = "com.kfit"  level = "ERROR"  />
     </ springProfile >
    
</ configuration >

可以启动服务的时候指定 profile (如不指定使用默认),如指定prod 的方式为:

1
java -jar xxx.jar --spring.profiles.active=prod


总结

在Spring Boot 中记录日志只需两步: 

1、在 src/main/resources 下面创建logback.xml 文件,并按上面讲述的进行配置。 

或者使用最简单的方法在 application 配置文件中配置。 

2、在Java代码中创建实例,并在需要输出日志的地方使用。

1
2
3
4
5
6
7
8
// 在Java类中创建 logger 实例
private  Logger logger = LoggerFactory.getLogger( this .getClass());
// 在方法中使用日志输出,如
publicvoidlogTest() {
     logger.debug( "日志输出测试 Debug" );
     logger.trace( "日志输出测试 Trace" );
     logger.info( "日志输出测试 Info" );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值