为什么在 SpringApplication.run() 之后写日志输出代码,它不会执行,而其他代码可以执行?

Why write the log output code after SpringApplication.run() , it will not be executed, while other code can be executed?

我用SpringInitializr创建了一个demo程序。

jdk8 行家 3.6.3 windows11

进口

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.10</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>

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

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

主应用程序

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class Application {

    public static void main(String[] args) {
        log.info("log output before SpringApplication.run");
        SpringApplication.run(Application.class, args);
        System.out.println("after SpringApplication.run first System.out.println");
        log.info("log output after SpringApplication.run");
        System.out.println("System.out.println end");
    }
}

结果 enter image description here

03:23:26.850 [main] INFO com.example.demo.Application - log output before             SpringApplication.run

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.5.10)

after SpringApplication.run first System.out.println
System.out.println end

为什么控制台不输出log.info(),却能输出System.out.println()? 我试过换个logback版本,或者用log4j,结果一样

非常感谢你帮助我

这是因为您在 application.properties 文件(或其他 logger-configuration 文件)中将 logging level 设置为 WARNERROR

例如,在application.properties

logging.level.root=WARN

默认情况下它是 INFO - 这就是您在 Spring 应用程序启动之前看到打印日志的原因。

然后 Spring 应用程序启动并将级别更改为以下信息之一:错误或警告 - 根据您提供的配置。