温馨提示×

温馨提示×

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

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

Spring替换掉默认common-logging.jar的方法

发布时间:2020-07-28 15:21:41 来源:亿速云 阅读:217 作者:小猪 栏目:编程语言

这篇文章主要讲解了Spring替换掉默认common-logging.jar的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

为什么使用日志打印而不是使用System.out.println()?

System.out是一个io流 如果使用它打印大批量数据 会占用大量的资源

spring默认使用common-logging打印日志信息 如果我们想替换掉它 使用其他的日志工具 分为如下几步

1.排除项目对common-logging的依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <exclusions>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

因为我所用的项目中common-logging在此依赖之下 所以需要将其排除

2.引入取代common-logging的日志打印工具的依赖

<!--其他日志工具的中间转换包-->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.2.3</version>
</dependency>

SLF4J对应不同框架如图所示

Spring替换掉默认common-logging.jar的方法

我这里引入的是转logback的依赖

3.配置logback.xml 设置输出的日志

先测试一下

Spring替换掉默认common-logging.jar的方法

结果如图 打印的日志太长了 设置打印的日志的格式和等级就需要logback.xml了

内容如图:(logback.xml在rescouce目录下)

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<configuration debug="true"> <!-- 指定日志输出的位置 -->
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder> <!-- 日志输出的格式 --> <!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体 内容、换行 -->
      <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
    </encoder>
  </appender>
  <!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR --> <!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 -->
  <root level="INFO"> <!-- 指定打印日志的 appender,这里通过“STDOUT”引用了前面配置的 appender -->
    <appender-ref ref="STDOUT"/>
  </root> <!-- 根据特殊需求指定局部日志级别 -->
  <logger name="com.atguigu.crowd.mapper" level="DEBUG"/>
</configuration>

设置后结果如图

Spring替换掉默认common-logging.jar的方法

看完上述内容,是不是对Spring替换掉默认common-logging.jar的方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

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

AI