Hitesh
Hitesh

Reputation: 271

How to use external properties file to use log4j2 in Springboot?

I don't want to use log4j2.properties file in my spring boot. i want to use external properties file but its shows me an error.

Exception in thread "main" java.lang.AbstractMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:510)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:450)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:257)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:560)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:577)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:212)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:551)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:537)
at com.flight.testcode.App.<clinit>(App.java:11)

Pom File dependency

<dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
   <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.6.1</version>
    </dependency>

So how can i use external properties file.

It also does not support log4j2 version 2.11.1 ?

Upvotes: 2

Views: 3675

Answers (3)

Nilesh Kumar
Nilesh Kumar

Reputation: 199

Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration.

If you are using gradle, and trying to run a test method you will have to add following to the build.gradle. This enables the test method to pick -Dlog4j.configurationFile= to be picked during the test run :

test { systemProperty "log4j.configurationFile", System.getProperty("log4j.configurationFile") }

The hierarchy of searching of the file is explained in documentation here :

Upvotes: 0

Hitesh
Hitesh

Reputation: 271

Finally i found solution of log42 external file loading.

import org.apache.logging.log4j.core.config.Configurator;

static {
   Configurator.initialize(null, "<filename>.properties");
}

If we does not use Configurator.initialize() method ,then log4j2 use its default configuration.

Upvotes: 2

BILO
BILO

Reputation: 87

Add the following dependency

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>

Then specify your logging configuration at startup:

-Dlogging.config=/path/to/log4j2.yaml (or .json or .xml)

Upvotes: 6

Related Questions