Search code examples
loggingspring-bootapp-startup

Can I configure startup and shutdown logs for Spring Boot applications?


For the ability to verify the startup and shutdown of our Spring Boot applications we want to configure a startup.log and shutdown.log capturing events that bootstrap and shutdown the application.

For startup everything up to:

Root WebApplicationContext: initialization completed in {x} ms

And for shutdown everything from:

Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53bd8fca: startup date [Wed Aug 19 09:47:10 PDT 2015]; root of context hierarchy

to the end.

Is this something that is container specific? (Tomcat vs Jetty vs Undertow)


Solution

  • You can create an event listener that watches for ApplicationReadyEvent and ContextStoppedEvent and log whatever you want.

    @Service
    public class Foo {
    
        @EventListener
        public void onStartup(ApplicationReadyEvent event) { ... }
    
        @EventListener
        public void onShutdown(ContextStoppedEvent event) { .... }
    
    }