Skip to content

The log is invalid in the SpringApplicationRunListeners.starting #33696

@dousp

Description

@dousp

behavior

  • code show as below
public class CostTimeSpringApplicationRunListener implements SpringApplicationRunListener {
    private static final Logger log = LoggerFactory.getLogger(CostTimeSpringApplicationRunListener.class);

    public CostTimeSpringApplicationRunListener(SpringApplication sa, String[] args) {
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        System.out.println("1==");
        log.info("11==");
        log.info("cost time -> starting {}", LocalDateTime.now());
        log.info("22==");
        System.out.println("2==");
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        log.info("cost time -> environmentPrepared {}", LocalDateTime.now());
    }

    ...
}
  • console:
17:53:48.407 [main] INFO com.dou.log.demo2.LogDemo2Application - before run 
1==
2==
2023-01-05T17:56:14.635+08:00  INFO 11080 --- [           main] d.c.CostTimeSpringApplicationRunListener : cost time -> environmentPrepared 2023-01-05T17:56:14.635750900

after debugging

  • the log in starting is created by LogbackLoggingSystem.beforeInitialize() ( loggerContext.getTurboFilterList().add(FILTER) )
  • the log in environmentPrepared is re-processed by LogbackLoggingSystem.initialize() (loggerContext.getTurboFilterList().remove(FILTER) )
  • FILTER:
private static final TurboFilter FILTER = new TurboFilter() {
	@Override
	public FilterReply decide(Marker marker, ch.qos.logback.classic.Logger logger, Level level, String format,
			Object[] params, Throwable t) {
		return FilterReply.DENY;
	}
}

So is it by design? Log output is not supported in starting, because the log is not fully initialized at this time?
but:

@SpringBootApplication
public class LogDemo2Application {
    private static final Logger log = LoggerFactory.getLogger(LogDemo2Application.class);
    public static void main(String[] args) {
        log.info("before run ");
        SpringApplication.run(LogDemo2Application.class, args);
        log.info("after run ");
    }
}

The log before the SpringApplication.run method here can be output ....
This makes springboot's logging behavior weird。。。

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions