Over-riding the log4j2 version in a Springboot starter
Asked Answered
C

2

7

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

Calvillo answered 2/11, 2016 at 21:58 Comment(0)
A
6
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> 
Albinus answered 13/12, 2021 at 3:39 Comment(5)
Alternatively one might import log4j-bom, which fixes the versions of all artifacts from the Log4j 2.x project, but does not override the versions of other artifacts.Chum
for gradle is ext['log4j2.version'] = '2.16.0'Wisner
Bingo, Thanks above solution overrides log4j version from starterOkie
@袁文涛 - where does this go in the build.gradle?Laudian
@RodneyP.Barbati. look up here docs.gradle.org/current/userguide/writing_build_scripts.htmlWisner
S
2

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.

Sixpack answered 2/11, 2016 at 23:7 Comment(2)
Thanks for the response. What we noticed is that if we set the log4j2 dependencies in the Springboot app that is using our starter, then things work fine. This is however not a desirable solution. We want to set the dependencies in the starter, so the end application is able to use the correct log4j2 version. However, when we set the log4j2 version dependencies in our starter, then the end application uses the log4j2 version that came with the Springboot version rather than what we set.Calvillo
Your spring boot app does not use the spring boot starter, and uses "your" starter? Could you post some details on your project structure,poms etc.Sixpack

© 2022 - 2024 — McMap. All rights reserved.