Spring Boot中slf4j日志依赖关系

发布时间:2021-06-04 16:17:02 作者:Leah
来源:亿速云 阅读:405

Spring Boot中slf4j日志依赖关系?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

底层依赖关系

Spring Boot中slf4j日志依赖关系

关系如何转化

Spring Boot中slf4j日志依赖关系

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

Spring Boot中slf4j日志依赖关系

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

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日志依赖关系的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 浅谈spring boot 集成 log4j 解决与logback冲突的问题
  2. Spring Boot如何使用logback、logstash、ELK记录日志文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

slf4j spring boot

上一篇:html设置背景径向渐变的方法

下一篇:kotlin Standard中的内联函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》