Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Over-riding the log4j2 version in a Springboot starter

We are trying to build a Springboot starter that will create log4j2 configuration programmatically, so developers don't have to bother creating log4j2.xml files. The problem is that the log4j2 programmatic API changes from version to version. We have tested our code with log4j2 version 2.5 and it works correctly in a stand-alone environment

Now we are trying to include our API in a Springboot starter so all springboot applications can include this starter and don't have to worry about log4j configuration.

The problem we are facing is that Springboot bundles its own version of log4j and we cannot control which version of Springboot the users are going to use.

Is there a way we can force the springboot starter to load the version 2.5 of log4j2 else our test Springboot app is complaining about some method not found in log4j

like image 567
Indranil Banerjee Avatar asked Nov 02 '16 21:11

Indranil Banerjee


2 Answers

It is convenient to add log4j2 dependency to section in parent project, as below
<dependencyManagement>
    <dependencies>
        ...
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>2.15.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

It will stipulate all log4j 2 versions in the project and modules. Instead, it's not necessary to add some log4j2 dependency independently.

Check this question post

If the project's log4j dependencies are only from spring-boot-starter-log4j2, it has a definitive setting way, refer to spring blog

<properties>
    <log4j2.version>2.17.0</log4j2.version> 
</properties> 
like image 96
tombear Avatar answered Oct 13 '22 20:10

tombear


You can override the dependency versions by declaring them in your maven pom files or gradle files.

In maven, to include log4j2 dependencies you will be excluding the default logback logger and then adding the following dependencies

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

If you check the content of this starter pom, you can see the following dependencies in it

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    </dependency>

In order to override any of these dependencies in which the version is managed, you can redeclare these dependencies in your pom file and provide a "version" tag for it. Simply include this

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.15</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.15</version>
</dependency>

In your pom file. Dependency Management tools like Maven and Gradle supports this type of overriding.

like image 41
ameenhere Avatar answered Oct 13 '22 20:10

ameenhere