SpringBoot(六)日志处理

2023-05-16

1、日志框架

简单分析案例: 小张;开发一个大型系统;

1、System.out.println("");将关键数据打印在控制台;去掉?写在一个文件?

2、框架来记录系统的一些运行时信息;日志框架 ; zhanglogging.jar;

3、高大上的几个功能?异步模式?自动归档?xxxx? zhanglogging-good.jar?

4、将以前框架卸下来?换上新的框架,重新修改之前相关的API;zhanglogging-prefect.jar;

5、JDBC—数据库驱动;

写了一个统一的接口层;日志门面(日志的一个抽象层);logging-abstract.jar;

给项目中导入具体的日志实现就行了;我们之前的日志框架都是实现的抽象层;

市面上的日志框架;

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j…

日志门面 (日志的抽象层)日志实现
JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-loggingLog4j JUL(java.util.logging) Log4j2 Logback

左边选一个门面(抽象层)、右边来选一个实现;

日志门面: SLF4J;

日志实现:Logback;

SpringBoot:底层是Spring框架,Spring框架默认是用JCL;‘

SpringBoot选用 SLF4j 和 logback;

2、SLF4j使用

如何在系统中使用SLF4j

官方文档:https://www.slf4j.org

以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法;

给系统里面导入slf4j 的 jar 和 logback 的实现 jar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

图示;
在这里插入图片描述

每一个日志的实现框架都有自己的配置文件。使用 slf4j 以后,配置文件还是做成日志实现框架自己本身的配置文件;

遗留问题

a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx

统一日志记录,即使是别的框架和我一起统一使用 slf4j 进行输出?

在这里插入图片描述

如何让系统中所有的日志都统一到 slf4j ;

1、将系统中其他日志框架先排除出去;

2、用中间包来替换原有的日志框架;

3、我们导入 slf4j 其他的实现;

3、SpringBoot 日志关系

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
</dependency>

SpringBoot 使用它来做日志功能;

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-logging</artifactId>
</dependency>

底层依赖关系

在这里插入图片描述

总结:

​ 1)、SpringBoot 底层也是使用 slf4j+logback 的方式进行日志记录

​ 2)、SpringBoot也把其他的日志都替换成了 slf4j

​ 3)、中间替换包;

@SuppressWarnings("rawtypes")
public abstract class LogFactory {

    static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";

    static LogFactory logFactory = new SLF4JLogFactory();
}    

在这里插入图片描述

4)、如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉?

Spring框架用的是 commons-logging

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

SpringBoot 能自动适配所有的日志,而且底层使用 slf4j+logback 的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;

4、日志使用

默认配置

SpringBoot 默认帮我们配置好了日志;

//记录器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
	//System.out.println();

	//日志的级别;
	//由低到高   trace<debug<info<warn<error
	//可以调整输出的日志级别;日志就只会在这个级别以以后的高级别生效
	logger.trace("这是trace日志...");
	logger.debug("这是debug日志...");
	//SpringBoot默认给我们使用的是info级别的,没有指定级别的就用SpringBoot默认规定的级别;root级别
	logger.info("这是info日志...");
	logger.warn("这是warn日志...");
	logger.error("这是error日志...");
}

日志输出格式:

  • %d表示日期时间,

  • %thread表示线程名,

  • %-5level:级别从左显示5个字符宽度

  • %logger{50} 表示logger名字最长50个字符,否则按照句点分割。

  • %msg:日志消息,

  • %n是换行符

  • %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

SpringBoot 修改日志的默认配置

logging.level.com.atguigu=trace

# 不指定路径在当前项目下生成springboot.log日志
# 可以指定完整的路径;
#logging.file=G:/springboot.log

# 在当前磁盘的根路径下创建spring文件夹和里面的log文件夹;使用 spring.log 作为默认文件
logging.path=/spring/log

#  在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# 指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
logging.filelogging.pathExampleDescription
(none)(none)只在控制台输出
指定文件名(none)my.log输出日志到my.log文件
(none)指定目录/var/log输出到指定目录的 spring.log 文件中

指定配置

给类路径下放上每个日志框架自己的配置文件即可;SpringBoot就不使用他默认配置的了

Logging SystemCustomization
Logbacklogback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
Log4j2log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging)logging.properties

logback.xml:直接就被日志框架识别了;

logback-spring.xml:日志框架就不直接加载日志的配置项,由 SpringBoot 解析日志配置,可以使用 SpringBoot 的高级 Profile 功能

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->
  	可以指定某段配置只在某个环境下生效
</springProfile>

例如:

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <springProfile name="dev">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern>
        </springProfile>
        <springProfile name="!dev">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern>
        </springProfile>
    </layout>
</appender>

如果使用 logback.xml 作为日志配置文件,还要使用 profile 功能,会有以下错误

no applicable action for [springProfile]

5、切换日志框架

可以按照 slf4j 的日志适配图,进行相关的切换;

slf4j+log4j的方式;

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <artifactId>logback-classic</artifactId>
      <groupId>ch.qos.logback</groupId>
    </exclusion>
    <exclusion>
      <artifactId>log4j-over-slf4j</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
</dependency>

切换为 log4j2

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions>
       <exclusion>
         <artifactId>spring-boot-starter-logging</artifactId>
         <groupId>org.springframework.boot</groupId>
       </exclusion>
     </exclusions>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

如果有收获!!! 希望老铁们来个三连,点赞、收藏、转发。
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot(六)日志处理 的相关文章

随机推荐

  • Redis在Linux中详细安装、Nosql概述

    一 NoSql入门和概述 1 入门概述 1 互联网时代背景下 xff0c 为什么用nosql xff1f 1 单机Mysql的美好时代 一个网站的访问量一般都不大 xff0c 用单个数据库完全可以轻松应对 在那个时候 xff0c 更多的都是
  • PyTorch:torch.max、min、argmax、argmin

    目录 1 torch max 2 torch argmax 3 torch min 4 torch argmin 1 torch max 函数定义 xff1a torch max input dim max 61 None max indi
  • HTML 的嵌套规则

    标签是文本标签 xff0c 一般嵌套用文本 1 a标签最好不要嵌套块级元素 xff0c 可以嵌套内联元素 xff0c 但是不能嵌套a标签和input之类的标签 能嵌套的标签像 等等 2 ul和ol的子元素不能是别的元素只能是li xff0c
  • Redis数据类型【详解有演示】

    Redis在Linux中详细安装 Nosql概述 xff1a https blog csdn net weixin 45606067 article details 107904512 三 Redis数据类型 1 Redis的五大数据类型
  • Redis持久化RDB和AOF、事务管理

    详解配置文件redis conf 常用的配置 xff1a https blog csdn net weixin 45606067 article details 107917743 五 Redis的持久化 1 RDB xff08 Redis
  • Redis主从复制、哨兵模式、Java客户端使用redis

    Redis持久化RDB和AOF 事务管理 xff1a https blog csdn net weixin 45606067 article details 107904567 七 Redis的发布订阅 1 是什么 进程间的一种消息通信模式
  • 详解配置文件redis.conf 常用的配置

    Redis数据类型 详解有演示 xff1a https blog csdn net weixin 45606067 article details 107904540 四 解析配置文件redis conf 1 他在哪 默认设置地址 为什么我
  • Redis 配置文件redis.conf详细解释

    Redis 配置文件示例 注意单位 当需要配置内存大小时 可能需要指定像1k 5GB 4M等常见格式 1k 61 gt 1000 bytes 1kb 61 gt 1024 bytes 1m 61 gt 1000000 bytes 1mb 6
  • MyBatisPlus(一)概述介绍

    一 概述 需要的基础 xff1a 把MyBatis Spring SpringMVC学习了 为什么要学习它呢 xff1f MyBatisPlus 可以节省我们大量工作时间 xff0c 所有的CRUD 代码它都可以自动化完成 比如 xff1a
  • 为什么要搭建自己的博客

    为什么要搭建自己的博客 xff1f 很多小伙伴起初和我有一样的疑惑 搭建博客的原因如下 现在市面上的博客很多 xff0c 比如如CSDN xff0c 博客园 xff0c 简书等平台 xff0c 可以直接在上面发表 xff0c 用户交互做的好
  • 为什么要写博客

    为什么要写博客 xff1f 我相信很多小伙伴和我有一样的疑惑 最近我也常和身边的朋友说写写博客做总结 xff0c 因为有的时候面试官遇到技术还不错的面试者会问你有没有博客 xff0c 如果有的话会在面试的时候额外加分 据我观察几乎每一个程序
  • hexo搭建博客【详细步骤】适合初学者

    为什么要搭建自己的博客 xff1a https blog csdn net weixin 45606067 article details 107966915 下面说一下如何从零开始上手搭建博客 Hexo搭建博客步骤 xff1a 搭建博客需
  • SpringBoot(一)概述、HelloWord案例

    1 SpringBoot 简介 回顾什么是Spring Spring是一个开源框架 xff0c 2003 年兴起的一个轻量级的Java 开发框架 Spring是为了解决企业级应用开发的复杂性而创建的 xff0c 简化开发 Spring是如何
  • PyTorch:view() 与 reshape() 区别详解

    总之 xff0c 两者都是用来重塑tensor的shape的 view只适合对满足连续性条件 xff08 contiguous xff09 的tensor进行操作 xff0c 而reshape同时还可以对不满足连续性条件的tensor进行操
  • SpringBoot(二)运行原理探究

    我们之前写的HelloSpringBoot xff0c 到底是怎么运行的呢 xff0c Maven项目 xff0c 我们一般从 pom xml文件探究起 SpringBoot2 3 3版本的官网文档说明 xff1a https docs s
  • idea查看源码时总是出现 .class而不是 .java源码

    步骤 1 File gt Settings gt Maven gt importing xff08 勾选上 Sources 和 Documentation xff09 2 右键项目的pom xml gt Maven gt Reimport
  • SpringBoot(三)yaml配置注入

    配置文件 SpringBoot使用一个全局的配置文件 xff0c 配置文件名称是固定的 application properties 语法结构 xff1a key 61 value application yml 语法结构 xff1a ke
  • SpringBoot(四)JSR303数据校验及多环境切换

    1 JSR303数据校验 先看看如何使用 Springboot 中可以用 64 validated 来校验数据 xff0c 如果数据异常则会统一抛出异常 xff0c 方便异常中心统一处理 我们这里来写个注解让我们的 name 只能支持 Em
  • SpringBoot(五)自动配置原理

    1 配置文件到底能写什么 xff1f 怎么写 xff1f SpringBoot 官网有大量的配置说明 xff0c 我们是无法全部记住的 xff1b SpringBoot 2 3 3 官网文档 2 分析自动配置原理 这里我们以 HttpEnc
  • SpringBoot(六)日志处理

    1 日志框架 简单分析案例 xff1a 小张 xff1b 开发一个大型系统 xff1b 1 System out println 34 34 xff1b 将关键数据打印在控制台 xff1b 去掉 xff1f 写在一个文件 xff1f 2 框