zoukankan      html  css  js  c++  java
  • Spring Boot 日志

    一、日志

    1、SpringBoot采用的日志

      日志分为日志抽象层与日志实现层。类似于JDBC与数据库的形式,不管连接的是什么数据库,只需将JDBC中对应的数据库信息修改即可。这里的日志抽象层是类似JDBC的效果,只需修改相应的日志实现层的信息即可。
      常用日志抽象层分类:
        JCL
        SLF4j
        jboss-logging
      常用日志实现层分类:
        Log4j
        JUL
        Log4j2
        Logback
      SpringBoot底层采用的是Spring框架,Spring默认采用JCL。但SpringBoot选用SLF4j与logBcak来实现日志的输出。

    2、使用SLF4j

    (1)官网地址:https://www.slf4j.org/
    (2)使用手册地址: https://www.slf4j.org/manual.html
    (3)手册地址中有一个HelloWorld示例:

    【需要导入的jar包】
    slf4j的jar包,以及其实现层的jar包。
    
    【HelloWorld.java】
    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");
      }
    }

    (4)常用模式:

    3、使用 SLF4J统一不同框架的日志

    (1)地址: https://www.slf4j.org/legacy.html
    (2)步骤:
      step1:将系统中其他原有的日志框架先排除出去。
      step2:使用一个中间包去替换原有的jar包。
      step3:导入slf4j 以及 相关实现层的 jar 包。
    (3)比如现在想要将Spring框架的日志(commons-logging)转为(SLF4J+logback)。
      首先使用 jcl-over-slf4j.jar 去替换commons-logging.jar包。然后需要导入 SLF4J与logback相关的jar包。

    4、SpringBoot的日志

    (1)SpringBoot底层使用slf4j + logback 记录日志。
    (2)SpringBoot会将不同的日志框架转为slf4j(使用中间替换包,先将原jar包从依赖关系中剔除,再添加新的jar包依赖)。
      在IDEA中找到<dependencies>标签,右击选中Diagrams,接着选中 Show Dependencies,可以看到当前所有依赖信息组成的图。

    找到日志相关信息,可以看到SpringBoot底层会将不同的日志框架转为slf4j 。

    5、SpringBoot如何使用日志

    (1)获得日志记录器,使用 LoggerFactory.getLogger()。
    (2)日志的级别:
      由低到高:trace < debug < info < warn < error
      根据不同的级别输出日志信息。对于某个级别,输出比此级别高以及此级别的日志。
      其中 SpringBoot 默认使用 info 级别的信息。即 输出 info , warn, error 等日志信息。

    【在test目录下的一个测试类】
    package com.example.helloworld;
    
    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 HelloworldApplicationTests {
    
        Logger logger = LoggerFactory.getLogger(getClass());
        @Test
        public void contextLoads() {
            logger.trace("=== trace ===");
            logger.debug("=== debug ===");
            logger.info("=== info ===");
            logger.warn("=== warn ===");
            logger.error("=== error ===");
        }
    
    }

    (3)设置日志打印级别:
      可以在配置文件(application.properties)中使用 logging.lever 去设置级别。

    (4)选择日志打印的位置:
      在配置文件中,如果存在logging.file 且 文件名为 my.log, 则会输出日志到当前项目路径下的 my.log文件中。
      在配置文件中,如果存在logging.path 且文件名为 /spring/log, 则会输出到指定目录的spring.log文件中。
      在配置文件中,若上述两个都未指定,则默认输出到控制台。
      在配置文件中,若上述两个均存在,则以logging.file为主。

    (5)设置日志打印的格式:
      在配置文件中,使用 logging.pattern.console 可以指定打印在控制台的日志格式。
      使用 logging.pattern.file 可以指定打印在指定文件中的日志格式。

    【日志格式:】
        %d : 表示日期时间。
        %thread :表示线程名。
        %-5lever : 级别从左显示5个字符的宽度。
        %logger[50] :表示logger名字最长为50个字符,否则按照点分割。
        %msg : 表示日志消息。
        %n :表示换行符。
        
    【举例:】
         logging.pattern.console = %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5lever %logger[50] -- %msg %n
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/l-y-h/p/11279350.html
Copyright © 2011-2022 走看看