michele
michele

Reputation: 26598

SpringBoot and log4j2 - application context loaded twice when SpringApplication.run()

I want to use Spring boot and log4j2.

I have this pom:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</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>

This is my main class:

@Component("batchLauncher")
@Import({ MyConfiguration.class })
public class MyLauncher implements CommandLineRunner {

    private static Logger log = LogManager.getLogger(MyLauncher.class);

    @Autowired
    MyController myController;

    public static void main(String[] args) {
        log.info("STARTING");
        SpringApplication.run(MyLauncher.class, args);
        log.info("FINISHED");
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("START Batch");
        MyController.start();
        log.info("END Batch");      
    }
}

I launch the jar with this option:

-Dlog4j.configurationFile=C:\log4j2.properties

When the application start, the console show me:

    DEBUG StatusLogger Reconfiguration complete for context[name=18b4aac2] at URI C:\log4j2.properties (org.apache.logging.log4j.core.LoggerContext@72057ecf) with optional ClassLoader: null
DEBUG StatusLogger Shutdown hook enabled. Registering a new one.
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@72057ecf] started OK.
2019-02-08 14:57:31.047 INFO [main] [it.batch.MyLauncher] [main] [it.batch.MyLauncher.main] - STARTING THE APPLICATION


DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@6c80d78a
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Loaded configuration from 
...
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@72057ecf] started OK with configuration XmlConfiguration[location=jar:file:/C:/Users/G0426/.m2/repository/org/springframework/boot/spring-boot/2.1.2.RELEASE/spring-boot-2.1.2.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml].


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

2019-02-08 14:57:31.753  INFO 13496 --- [           main] i.f.c.c.o.b.Info

As you can see from logs, log4j2 load my log4j properties file until the line

SpringApplication.run(MyLauncher.class, args);

The previous line is writed in log file, after that running SpringApplication.run(...), a second instance/context is loaded and log4j2 start logging using the default configuration located at:

file:/C:/Users/G0426/.m2/repository/org/springframework/boot/spring-boot/2.1.2.RELEASE/spring-boot-2.1.2.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml

What I am doing wrong?

Thanks.

Upvotes: 0

Views: 1128

Answers (1)

Kedar Joshi
Kedar Joshi

Reputation: 1511

SpringApplication.run(MyLauncher.class, args); is THE starting point of a Spring application. Spring has no control over anything logged before its execution.

Hence the behavior is appropriate considering Spring based logging configuration kicks in only after its context is loaded. Also, Spring Boot uses logging.config property to load the logging configuration.

You can try setting -Dlogging.config=C:\log4j.properties.

See Spring Boot Documentation for further details.

Upvotes: 1

Related Questions