gpt4 book ai didi

Spring Boot中slf4j日志依赖关系示例详解

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring Boot中slf4j日志依赖关系示例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言 。

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

把其他common-logging、log4j、java.util.logging转换为slf4j 。

下面这篇文章主要给大家介绍了关于spring boot slf4j日志依赖关系的相关内容,下面话不多说了,来一起看看详细的介绍吧 。

底层依赖关系 。

Spring Boot中slf4j日志依赖关系示例详解

关系如何转化 。

Spring Boot中slf4j日志依赖关系示例详解

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

Spring Boot中slf4j日志依赖关系示例详解

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

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

?
1
2
3
4
5
6
7
8
9
10
11
<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 。

?
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.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日志 。

日志基础配置 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 指定日志输入级别
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

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.

原文链接:https://juejin.im/post/5be467ae5188256dbb517885 。

最后此篇关于Spring Boot中slf4j日志依赖关系示例详解的文章就讲到这里了,如果你想了解更多关于Spring Boot中slf4j日志依赖关系示例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com