54. spring boot日志升级篇—logback【从零开始学Spring Boot】

 

【视频 & 交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

       44. Spring Boot日志记录SLF4J章节中有关相关的介绍,这里我们在深入的了解下logback框架。

 

为什么要使用logback ?

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

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

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

 

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

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

      

LogBackLog4j都是开源日记工具库,LogBackLog4j的改良版本,比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则实现了Slf4jAPI,所以当想配合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 文件:

<?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>

 

在代码中调用:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 private Logger logger =  LoggerFactory.getLogger(this.getClass());

 

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

配置文件解读:

logback-spring.xml文件:

<?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 文件内容为:

<?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.xmlSpring Boot的日志系统预先定义了一些系统变量:

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

       同时默认情况下包含另个appender——一个是控制台,一个是文件,分别定义在console-appender.xmlfile-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命名

<?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 的方式为:

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

 

总结

Spring Boot 中记录日志只需两步: 
1
、在 src/main/resources 下面创建logback.xml 文件,并按上面讲述的进行配置。 
或者使用最简单的方法在 application 配置文件中配置。 
2
、在Java代码中创建实例,并在需要输出日志的地方使用。

// Java类中创建 logger 实例
private Logger logger = LoggerFactory.getLogger(this.getClass());
// 在方法中使用日志输出,如
publicvoidlogTest() {
    logger.debug("日志输出测试 Debug");
    logger.trace("日志输出测试 Trace");
    logger.info("日志输出测试 Info");
}

 

  Spring Boot 系列博客】

视频&交流平台

à Spring Boot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

更多查看博客: http://412887952-qq-com.iteye.com/

 

猜你喜欢

转载自412887952-qq-com.iteye.com/blog/2307244