SLF4J and Log4j 2 binding Maven dependency
Asked Answered
I

2

6

Hopefully a simple question but My google foo is failing me - I've got a maven project where we're using SLF4J with Log4J 1.2 bindings.

We now want to move to Log4j 2 particularly for the performance improvement - however, I can't for the life of me find the maven dependency for the log4j 2.0 binding. I've found some notes at http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/ but no mention of any dependency info.

I'm also slightly confused by the fact there's apparently two ways to put slf4j on top of log4j2 (binding or adaptor)

What's the correct way to bind slf4j with log4j2 and how do I define the maven dependencies?

Editing to add some code following the 1st answer below, I'm getting exception:

Exception in thread "main" java.lang.NoSuchMethodError: org/apache/logging/log4j/spi/AbstractLoggerWrapper.(Lorg/apache/logging/log4j/spi/AbstractLogger;Ljava/lang/String;)V at org.slf4j.impl.SLF4JLogger.(SLF4JLogger.java:48)

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>logging.test</groupId>
<artifactId>logtest2</artifactId>
<version>0.0.1</version>
<name>logtest2</name>
<description>logtest2</description>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j.adapters</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0-beta3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-beta9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-beta9</version>
    </dependency>
</dependencies>

My log4j.xml:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="A1" class="org.apache.log4j.FileAppender">
        <param name="File" value="c:/logtest2.0/log.txt" />
        <param name="Append" value="false" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %p %X{sessionId} %c MSG: %m%n" />
        </layout>
    </appender>
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %p %X{sessionId} %c MSG: %m%n" />
        </layout>
    </appender>
    <category name="org.apache.log4j.xml">
        <priority value="debug" />
        <appender-ref ref="A1" />
    </category>
    <root>
        <priority value="debug" />
        <appender-ref ref="STDOUT" />
    </Root> </log4j:configuration>

and my test java class:

package loggertest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggerTest {

    public static final Logger LOGGER = LoggerFactory.getLogger(LoggerTest.class);

    public static void main(final String[] p_args) throws InterruptedException {
        LOGGER.debug("Logger test");
    }
}
Implication answered 25/9, 2013 at 18:56 Comment(0)
C
2
"org.apache.logging.log4j:log4j-slf4j-impl:2.0-beta9" - 
            LOG4J implementation of SLF4J API
"org.apache.logging.log4j:log4j-core:2.0-beta9" - Core LOG4J implementation

This, plus a valid log4j2.xml on the classpath should get you started.

Contrayerva answered 25/9, 2013 at 19:6 Comment(3)
Thanks, seems I also needed the log4j-api as well, but even so, slf4j is throwing Exception in thread "main" java.lang.NoSuchMethodError: org/apache/logging/log4j/spi/AbstractLoggerWrapper.<init>(Lorg/apache/logging/log4j/spi/AbstractLogger;Ljava/lang/String;)VImplication
Sorry, I've accidentaly mislead you, you don't need to include log4j api as a dependency, as log4j core and log4j slf4j implementations both depend on it, so maven should get them for you (I've edited the answer to reflect this). However, you seem to be using org.apache.logging.log4j.adapters:log4j-slf4j-impl:2.0-beta3. Try org.apache.logging.log4j:log4j-slf4j-impl:2.0-beta9 insteadContrayerva
That got it. I did also track down the apache documentation for it, I misread the link that tells you how to configure maven projects and thought it was a link to something entirely different! Between you and the apache docs, I have it working now. Config seems nicer, but not really seeing the "10 times faster" yet, on a test with a large number of threads all logging like mad but I will persevere with more detailed tests. Thanks again.Implication
N
0

If you like to divert jars that use commons logging, log4j1 and others as well redirected to slf4j and finally use log4j2 implementation for logging.. here is the set that need to be used...

Explanation: First two are excluding the log4j1 and commons-logging not there in classpath. log4j-over-slf4j and jcl-over-slf4j provide proxies that redirect to slf4j api. Remaining are log4j2 implementation dependencies.

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.17.1</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.17.1</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.17.1</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-layout-template-json</artifactId>
  <version>2.17.1</version>
</dependency>
Norvin answered 30/12, 2021 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.