Skip to content

Log4j2 shutdown before it prints the last messages during graceful shutdown of spring boot application. #11360

@Myslyvchuk

Description

@Myslyvchuk

Hi, I'm using spring boot with version 1.5.7.RELEASE. Also I'm using Log4j2 logger with excluding SimpleLogger from hateoas, data-jpa and adding dependencies for it.

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>

    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

The main problem that U have code this is running in thread and has logger.

private void run() {
      while (!notStopped) {
        } catch (InterruptedException e) {
          logger.info("===============================================SHUTDOWN===============================================");
  }
     }
        }
@PreDestroy
  public void stop() throws InterruptedException {
      notStopped = true;
      SOME CODE
  }

So, the problem is that when I'm stopping the server with Crtl+C I'm expecting that logger will output

===============================================SHUTDOWN=============================================== 

but instead of that I'm receiving

Thread-3 WARN Unable to register Log4j shutdown hook because JVM is shutting down. Using SimpleLogger

When I'm deleting all dependencies for Log4j2 and deleting exclusions for logging from starters, it prints me the expected result SHUTDOWN.
As I understood there is a problem with shutdown hook of Log4j2. I found a related issues with Log4j2, and all of them described that it was problem in Log4j2 which would be fixed in 2.6 version. But now it is 2.7 version of Log4j2. How to fix this?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions