Spring-Boot Log


title: Spring Boot日志
catalog: true
date: 2019-09-23 13:35:41
subtitle: SpringBoot日志
header-img: “/img/article_header/article_header.png”
tags: Spring Boot
catagories:

  • SpringBoot

A. Logging framework

Market logging framework;

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

Facade log (log abstraction layer) Log achieve
JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-logging Log4j JUL(java.util.logging) Log4j2 Logback

Is selected from a left side of the facade (abstraction layer), to choose the right one implementation;

Log facade: SLF4J;

Log realization: Logback;

SpringBoot: bottom layer is the Spring framework, Spring Framework default JCL

SpringBoot choices are SLF4J and logback

Use two .SLF4J

1. How to use the system SLF4j

Logging method should not be called directly implement the class log twenty calls inside the method log abstraction layer;

SLF4j introduced to the system to achieve jar jar and logback

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");
  }
}

Icon:

Here Insert Picture Description

Implementation framework Each log has its own configuration file. After using SLF4j, configuration files or to make logging implementation framework itself profile.

2. The issue of legacy

The presence of a plurality of logs in the same frame system:

Item A (SLF4j + logback): Spring (commons-logging), Hibernate (jboss-logging), MyBatis like

We can be unified logging, even other frame, you can use SLF4j output.

Here Insert Picture Description

How to get all the logs into the system are unified in SLF4j:

  1. The other system log files to exclude
  2. Replace the original frame for logging tundish
  3. We import SLF4j other implementations

Three .SpringBoot log relationship

The most basic log-related dependence:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.1.8.RELEASE</version>
    <scope>compile</scope>
</dependency>

SpringBoot use it for logging:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    <version>2.1.8.RELEASE</version>
    <scope>compile</scope>
</dependency>

The underlying dependencies:

Here Insert Picture Description

to sum up:

  1. SpringBoot underlayer is used SLF4j + logback manner log
  2. SpringBoot be replaced with the other log SLF4j
  3. Alternatively intermediate package replaces the original logging framework
  4. If we want to introduce other framework, this framework must take to remove the default log out.

SpringBoot automatically adapted to all logs, and use the bottom of the way when SLF4j + logback log, the introduction of other frame, the frame need only log the dependent frame to exclude.

IV. Usage logs

1. Default Configuration

SpringBoot help us configure the default log

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

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

}

Log file output formats:

Log output formats:

  • % D indicates the date and time,

  • % Thread represents the thread name,

  • % -5level: Level 5 character width from the left to display

  • % Logger {50} represents the logger name up to 50 characters, otherwise divided according to the period.

  • % Msg: log messages,

  • % N is the newline

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

logging.file logging.path Example Description
(none) (none) Only output in the console
Specify the file name (none) my.log Output log file to my.log
(none) Specified directory / Var / log Spring.log output to a file in the specified directory

2. Directed Placement

Each log frame its own configuration files, springboot not use the default configuration on the classpath decentralization

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

logback.xml: logging framework directly identified

logback-spring.xml: logging framework will not be able to load directly log configuration items by SpringBoot parsing log configuration, you can use SpringBoot advanced features Profile

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

Such as:

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <!--
        日志输出格式:
   %d表示日期时间,
   %thread表示线程名,
   %-5level:级别从左显示5个字符宽度
   %logger{50} 表示logger名字最长50个字符,否则按照句点分割。 
   %msg:日志消息,
   %n是换行符
        -->
    <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>
Published 26 original articles · won praise 27 · views 6858

Guess you like

Origin blog.csdn.net/qq_40705355/article/details/103634192