1. commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择。log4j和logback则是具体的日志实现方案。
  2. 比较常用的搭配是commons-logging+log4j,slf4j+logback
  3. 为什么要用SLF4J+Logback 替换commons-logging+log4j?

    1. SLF4J是编译时绑定到具体的日志框架,性能优于采用运行时搜寻的方式的commons-logging
    2. 不需要使用logger.isDebugEnabled()来解决日志因为字符拼接产生的性能问题
      1. logger.info("my name is {}", "medusar");
        logger.info("my name is " + "medusar");
      2. 
        

        在效率上,第一行比第二行更高,因为如果当前日志级别是ERROR,第一行不会进行字符串拼接,而第二行,无论日志级别是什么,都会先进行字符串拼接。

      3. 所以为了解决这个问题,commons-logging等框架提供了下面的方式:
        if (log.isDebugEnabled()){
        log.debug("dddd"+"eee");
        }
  4. 基于commons-logging的日志使用
    1. import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      public class XXXService {
      private static final Log log = LogFactory.getLog(XXXService.class);
      public void doSomething(){
      log.info("begin dosomething....");
      }
      }
  5. 基于slf4j的日志使用

    1. import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      public class XXXService {
      private static final Logger logger = LoggerFactory.getLogger(XXXService.class);
      public void doSomething() {
      logger.info("begin dosomething...");
      }
      }
  6. SpringBoot底层也是使用slf4j+logback的方式进行日志记录;
  7. SpringBoot也把其他的日志都替换成了slf4j;
  8. Spring Boot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉。
    1.  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <exclusions>
      <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      </exclusion>
      </exclusions>
      </dependency>
    2. 以后开发的时候,日志记录方法的调用,不应该直接调用日志的实现类,而是调用日志抽象层里面的方法;给系统里面导入slf4j的jar和logback的实现jar
    3. 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"); }
      }
  9. 指定文件中日志输出的格式
    1. # 在控制台输出的日志格式 logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{} - %msg%n
      # 指定文件中日志输出的格式 logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{} === - %msg%n
    2. <!-- 日志输出格式:            
      1. %d表示日期时间,
      2. %thread表示线程名,
      3. %-5level:级别从左显示5个字符宽度
      4. %logger{50} 表示logger名字最长50个字符,否则按照句点分割。
      5. %msg:日志消息,
      6. %n是换行符-->
  10. 切换日志框架(无意义,slf4j+logback已经是最佳实现)

    1. <dependencies>
      <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>
      <!--添加slf4j依赖-->
      <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      </dependency>
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      </dependency>
      </dependencies>
  11. 调整日志级别
    1. Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
    2. Logback does not have a FATAL level. It is mapped to ERROR.
    3. The default log configuration echoes messages to the console as they are written. By default, ERROR-level, WARN-level, and INFO-level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.
    4. $ java -jar myapp.jar --debug
    5. Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties).
    6. All the supported logging systems can have the logger levels set in the Spring Environment (for example, in application.properties) by using logging.level.<logger-name>=<level> where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root.

      The following example shows potential logging settings in application.properties:

    7. logging.level.root=WARN
      logging.level.org.springframework.web=DEBUG
      logging.level.org.hibernate=ERROR
  12. 日志文件
    1. By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging.file or logging.path property (for example, in your application.properties).
    2. Log files rotate when they reach 10 MB and, as with console output, ERROR-level, WARN-level, and INFO-level messages are logged by default. Size limits can be changed using the logging.file.max-size property. Previously rotated files are archived indefinitely unless the logging.file.max-history property has been set.
    3. Logging properties are independent of the actual logging infrastructure. As a result, specific configuration keys (such as logback.configurationFile for Logback) are not managed by spring Boot.
  13. Log Groups

    1. It’s often useful to be able to group related loggers together so that they can all be configured at the same time. For example, you might commonly change the logging levels for all Tomcat related loggers, but you can’t easily remember top level packages.

      To help with this, Spring Boot allows you to define logging groups in your Spring Environment. For example, here’s how you could define a “tomcat” group by adding it to your application.properties:

      logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat

      Once defined, you can change the level for all the loggers in the group with a single line:

      logging.level.tomcat=TRACE

      Spring Boot includes the following pre-defined logging groups that can be used out-of-the-box:

      Name Loggers

      web

      org.springframework.core.codec, org.springframework.http, org.springframework.web

      sql

      org.springframework.jdbc.core, org.hibernate.SQL

  14. Custom Log Configuration

    1. The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config.
    2. Since logging is initialized before the ApplicationContext is created, it is not possible to control logging from @PropertySources in Spring @Configuration files. The only way to change the logging system or disable it entirely is via System properties.
    3. Depending on your logging system, the following files are loaded:
    4. Logging System Customization

      Logback

      logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy

      Log4j2

      log4j2-spring.xml or log4j2.xml

      JDK (Java Util Logging)

      logging.properties

    5. When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.
    6. To help with the customization, some other properties are transferred from the Spring Environment to System properties, as described in the following table:

      1. Spring Environment System Property Comments

        logging.exception-conversion-word

        LOG_EXCEPTION_CONVERSION_WORD

        The conversion word used when logging exceptions.

        logging.file

        LOG_FILE

        If defined, it is used in the default log configuration.

        logging.file.max-size

        LOG_FILE_MAX_SIZE

        Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)

        logging.file.max-history

        LOG_FILE_MAX_HISTORY

        Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)

        logging.path

        LOG_PATH

        If defined, it is used in the default log configuration.

        logging.pattern.console

        CONSOLE_LOG_PATTERN

        The log pattern to use on the console (stdout). (Only supported with the default Logback setup.)

        logging.pattern.dateformat

        LOG_DATEFORMAT_PATTERN

        Appender pattern for log date format. (Only supported with the default Logback setup.)

        logging.pattern.file

        FILE_LOG_PATTERN

        The log pattern to use in a file (if LOG_FILE is enabled). (Only supported with the default Logback setup.)

        logging.pattern.level

        LOG_LEVEL_PATTERN

        The format to use when rendering the log level (default %5p). (Only supported with the default Logback setup.)

        PID

        PID

        The current process ID (discovered if possible and when not already defined as an OS environment variable).

  15. xx

Spring boot 官网学习笔记 - logging的更多相关文章

  1. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  2. Spring boot 官网学习笔记 - Spring DevTools 介绍

    想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...

  3. Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)

    Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...

  4. Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利

    If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...

  5. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  6. Spring boot 官网学习笔记 - Spring Boot CLI 入门案例

    安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...

  7. Spring boot 官网学习笔记 - Configuration Class(@import)

    推荐使用 Java-based configuration ,也可以使用xml we generally recommend that your primary source be a single ...

  8. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网

    官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...

  9. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

随机推荐

  1. 玩转SpringBoot 2 之项目启动篇

    SpringBoot 启动方式有那些? SpringBoot 有4种方式进行启动,具体方式如下: IDEA方式启动 Eclipse 方式启动 Maven 启动方式 通过SpringBoot 程序 ja ...

  2. pickle 都写文件

    import pickle mylist=[[1,2,3,4,5,6,7],["abc","xyz","hello"],[1,2,3,4,5 ...

  3. Heron and His Triangle 2017 沈阳区域赛

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  4. FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283

    Problem L Tic-Tac-Toe Accept: 94    Submit: 184Time Limit: 1000 mSec    Memory Limit : 262144 KB  Pr ...

  5. Spring 两大核心 IOC 和 AOP

    如果你的简历上写着Spring (请详述一下spring的两大核心)这个问题一定会被问到. 一.什么叫IOC 1. IOC 全称(Inversion of Control)-- 控制反转. IOC 只 ...

  6. zookeeper集群部署问题排查记录

    今天在三台虚拟机搭建zookeeper集群,一直连不通,然后进行了几个小时的斗争,做个记录. 具体部署方式网上有很多, 不在赘述.产生连接不同的问题主要有以下几个方面: 1.仔细检查配置文件. 是否有 ...

  7. 人体行为识别(骨架提取),搭建openpose环境,VS2019(python3.7)+openpose

    这几天开始接触人体行为识别,经过多方对比后,选择了现在最热的人体骨架提取开源库,openpose. 下面就不多说了,直接开始openpose在win10下的配置: 需求如下:1. VS2019    ...

  8. Flink入门宝典(详细截图版)

    本文基于java构建Flink1.9版本入门程序,需要Maven 3.0.4 和 Java 8 以上版本.需要安装Netcat进行简单调试. 这里简述安装过程,并使用IDEA进行开发一个简单流处理程序 ...

  9. 新手学习FFmpeg - 调用API调整视频局部速率

    通过修改setpts代码实现调整视频部分的播放速率. 完整代码可参考: https://andy-zhangtao.github.io/ffmpeg-examples/ 在前面提到了PTS/DTS/T ...

  10. Android studio初次安装启动时弹出unable to access android sdk add-on list提示的解决方法

    一.问题描述 初次安装Android Studio,启动后,报错如下: unable to access android sdk add-on lis 如图: 二.原因分析 AS启动后,会在默认路径下 ...