spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.16k stars 40.68k forks source link

Some logs are dismissed during context creation #7772

Open romainmoreau opened 7 years ago

romainmoreau commented 7 years ago

The default logging system is used (Logback).

For instance, the warning logged here is dismissed. This behavior appears because at the time the warning is logged, the Logback context contains a filter that denies everything.

This issue is similar to #7758 but it's with Logback.

Here's a quick and dirty demo:

package demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoApplication.class);

    @Value("${demo.value:empty}")
    private String demoValue;

    @Bean
    public CommandLineRunner commandLineRunner() {
        return new CommandLineRunner() {
            @Override
            public void run(String... args) throws Exception {
                LOGGER.warn("value = {}", demoValue);
            }
        };
    }

    public static void main(String[] args) throws Exception {
        // Valid JSON:
        // System.setProperty("SPRING_APPLICATION_JSON", "{\"demo\":{\"value\":\"demo\"}}");
        // Invalid JSON:
        System.setProperty("SPRING_APPLICATION_JSON", "{");
        SpringApplication.run(DemoApplication.class, args);
    }
}
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>
snicoll commented 7 years ago

I am not sure what we can do about it. It's a chicken and egg problem. When that code runs, the Environment has not been prepared yet. We want the environment to contribute to the initialization of the logging system (pattern, loggers, etc).

The filter is there on purpose to avoid an early init of the logging system.

philwebb commented 5 years ago

Incidentally #14239 changed the logic so that invalid JSON fails the app.