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

camsedfj  于 2022-11-06  发布在  Spring
关注(0)|答案(1)|浏览(145)

我使用SpringInitializr创建了一个演示程序。
jdk8 maven 3.6.3视窗11

导入

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

结果

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,都有同样的结果
"非常感谢你的帮助"

gwbalxhn

gwbalxhn1#

这是因为您在application.properties文件(或其他记录器配置文件)中将logging level设置为WARNERROR
例如,在application.properties

logging.level.root=WARN

默认情况下,它是INFO-这就是为什么您在Spring应用程序启动之前看到打印的日志。
然后,Spring应用程序启动并将级别更改为以下INFO之一:错误或警告-根据您提供的配置。

相关问题