Log4j Latest version with slf4j-log4j12
Asked Answered
V

2

5

Using slf4j-log4j12 version- 2.0.0-alpha5 dependency for logging which pulls Apache Log4j » 1.2.17. Need to upgrade the Log4j to latest which is log4j-core » 2.17.1 such that I don't have to make changes in the codebase.

Below is the snippet of POM and sample code:

  <dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>2.0.0-alpha5</version>
    </dependency>
   </dependencies>

Sample Code:

import org.apache.log4j.Logger;
public class Entry {
    final static Logger logger = Logger.getLogger(Entry.class);
    public static void main(String[] args) {
        logger.info("Through Logging");
        System.out.println("Logging tests");
    }
}

Is there any sl4j dependency for log4j2?

Vidavidal answered 11/1, 2022 at 8:55 Comment(4)
So what error do you get?Zymogenesis
As the name already indicates slf4j-log4j12 contains log4j v1.2. Redirecting slf4j API to log4j is uncommon, therefore I recommend you to instead use slf4j + logback and the redirect the log4j requests to slf4j.Testis
@Zymogenesis above code work just fine but i have to pull in log4j 2.17 without changing the codeVidavidal
Log4j2 provides an implementation of the slf4j API. See logging.apache.org/log4j/2.x/log4j-slf4j-implHerd
W
8

The slf4j-log4j12 is a bridge (binding) from SLF4J to Log4j 1.2: all messages submitted to a org.slf4j.Logger in your code, will be sent to a org.apache.log4j.Logger of the same name. Its not the right direction.

Fortunately nowadays there are bridges/bindings/adapters between all major logging systems. It is not clear from your question, whether you need SLF4J at all, so:

  • if you don't use SLF4J, you can use log4j-1.2-api, a direct bridge between Log4j 1.x and Log4j 2.x API. It is a replacement for Log4j 1.x, so you need to remove the log4j artifact and add:

    <!-- From Log4j 1.x to Log4j 2.x API -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.19.0</version>
    </dependency>
    
  • if you want to pass through SLF4J, you can use log4j-over-slf4j (a replacement for Log4j 1.x that connects Log4j 1.x to SLF4J) and log4j-slf4j18-impl (a bridge from SLF4J to the Log4j 2.x API):

    <!-- From Log4j 1.x to SLF4J -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>2.0.5</version>
    </dependency>
    <!-- From SLF4J 2.0+ to Log4j 2.x API -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j2-impl</artifactId>
        <version>2.19.0</version>
        <scope>runtime</scope>
    </dependency>
    

Of course you'll also need an implementation of the Log4j 2.x API, like Log4j 2.x Core:

<!-- From Log4j 2.x API to Log4j 2.x Core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.19.0</version>
    <scope>runtime</scope>
</dependency>
Wurth answered 11/1, 2022 at 17:44 Comment(0)
T
1

You can try to exclude the log4 1.2.17 dependency and instead use log4j2-core. The following configuration is untested, not sure if it works:

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>2.0.0-alpha5</version>
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version>
    </dependency>
</dependencies>
Testis answered 11/1, 2022 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.