0
votes

I am trying to log something before my Spring Boot application starts. Here is a snippet below. I am using Lombok and Log4J2 and I have done the spring-boot-starter-logging exclusion + added spring-boot-starter-log4j2. I was wondering how to make it work and why the present code does not work.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.log4j.Log4j2;

@SpringBootApplication
@Log4j2
public class DemoApplication {

    public static void main(String[] args) {
        log.info("Does not work");
        SpringApplication.run(DemoApplication.class, args);
        log.info("Works");
    }
}

Result in console:

2020-11-30 19:45:32.667  INFO 25132 --- [           main] c.e.d.DemoApplication                    : Starting DemoApplication using Java 11.0.1 on *** with PID 25132 (**** started by **** in ****)
2020-11-30 19:45:32.702  INFO 25132 --- [           main] c.e.d.DemoApplication                    : No active profile set, falling back to default profiles: default
2020-11-30 19:45:33.921  INFO 25132 --- [           main] c.e.d.DemoApplication                    : Started DemoApplication in 2.008 seconds (JVM running for 3.103)
2020-11-30 19:45:33.927  INFO 25132 --- [           main] c.e.d.DemoApplication                    : Works

Updated:

However, as shown below, using Slf4j with default LogBack works, why not Log4j2? (Log4j2 with Slf4j still does not)

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        log.info("Does not work");
        SpringApplication.run(DemoApplication.class, args);
        log.info("Works");
    }
}
20:04:25.945 [main] INFO com.example.demo.DemoApplication - Does not work

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)

2020-11-30 20:04:26.463  INFO 33284 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 11.0.1 on **** with PID 33284 (**** started by **** in ****)
2020-11-30 20:04:26.465  INFO 33284 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-11-30 20:04:27.234  INFO 33284 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.17 seconds (JVM running for 2.109)
2020-11-30 20:04:27.242  INFO 33284 --- [           main] com.example.demo.DemoApplication         : Works
1

1 Answers

0
votes

In the end I was able to answer my own question by stepping through the code + some research. Sharing my results:

In Log4j2, the default root filter level, when not providing a config (or before Spring instantiates the Log4j2-spring config) is ERROR, therefore isEnabled returns false and the logger does not print to the console the first time. However, once instantiated by Spring, the level becomes INFO and therefore the messages is printed to the console as the log level is now superior or equal to INFO level. QED