美文网首页
SpringBoot中的slf4j日志依赖关系

SpringBoot中的slf4j日志依赖关系

作者: MrJonny | 来源:发表于2018-11-09 00:47 被阅读0次

SpringBoot底层使用的是slf4j+logback来进行日志记录

  • 把其他common-logginglog4jjava.util.logging转换为slf4j

底层依赖关系

image

关系如何转化

image

底层通过偷梁换柱的方法,用jcl、jul、log4j中间转换包进行转化

image

如果要引入其他框架,必须将其中默认日志依赖剔除

SpringBoot从maven依赖中剔除springframework:spring-core中的common-logging

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.20.RELEASE</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

+++

SpringBoot默认日志级别为INFO级别

  • 日志优先级从小到大顺序为:
    • trace<debug<info<warn<error
package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    Logger log = LoggerFactory.getLogger(getClass());

    @Test
    public void contextLoads() {
        log.trace("trace日志");
        log.debug("debug日志");
        log.info("info日志");
        log.warn("warn日志");
        log.error("error日志");
    }

}
  • 启动运行,控制台打印只打印了info及以上级别
2018-11-09 00:13:36.899  INFO 8156 --- [main] com.example.demo.DemoApplicationTests    : info日志
2018-11-09 00:13:36.900  WARN 8156 --- [main] com.example.demo.DemoApplicationTests    : warn日志
2018-11-09 00:13:36.900 ERROR 8156 --- [main] com.example.demo.DemoApplicationTests    : error日志

日志基础配置

# 指定日志输入级别
logging.level.com.example.demo=trace 

# 指定日志输出位置和日志文件名
logging.file=./log/log.txt

# 指定日志输出路径,若file和path同时配置,则file生效
# 此配置默认生成文件为spring.log
#logging.path=./log

# 控制台日志输出格式
# -5表示从左显示5个字符宽度
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %boldYellow(%thread) | %boldGreen(%logger) | %msg%n

# 文件中输出的格式
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} = [%thread] = %-5level = %logger{50} - %msg%n

相关文章

  • 三、Spring Boot与日志

    一、日志框架分类和选择 二、SLF4j的使用 三、SpringBoot日志关系 四、SpringBoot日志使用 ...

  • 启动项目时提示java.lang.NoClassDefFound

    原因是因为依赖项中有多个日志工具,而slf4j不知道要使用哪个日志工具。Pom中把Springboot默认的log...

  • 三、日志(2)

    3、SpringBoot日志关系 SpringBoot使用它来做日志功能; 底层依赖关系 总结: ​ 1)、S...

  • SpringBoot中的slf4j日志依赖关系

    SpringBoot底层使用的是slf4j+logback来进行日志记录 把其他common-logging、lo...

  • springboot内置logback

    logback和slf4j的关系 刚开始接触日志,公司一般都采用springboot中去整合slf4j和logba...

  • spring boot学习与实践练习2

    SpringBoot和日志 SpringBoot选用的日志框架是日志抽象层(门面)选用SLF4J,日志实现选用是l...

  • SpringBoot日志配置

    SpringBoot日志类库默认采用Slf4j,日志系统采用Logback 1.新建项目,在pom.xml中引入 ...

  • springboot日志系统

    日志系统种类 springboot通过slf4j门面支持多种日志系统:主要包含 JavaLoggingSystem...

  • 代码规范(阿里)——日志规范

    【强制】应用中不可直接使用日志系统(Log4j、Logback)中的 API,而应依赖使用日志框架 SLF4J 中...

  • 日志规约

    【强制】应用中不可直接使用日志系统(Log4j、Logback)中的 API,而应依赖使用日志框架 SLF4J中的...

网友评论

      本文标题:SpringBoot中的slf4j日志依赖关系

      本文链接:https://www.haomeiwen.com/subject/whxnxqtx.html