Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J and Log4j 2 binding Maven dependency

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");
    }
}
like image 373
Steve Atkinson Avatar asked Nov 01 '22 14:11

Steve Atkinson


1 Answers

"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.

like image 167
Tegi Avatar answered Nov 08 '22 03:11

Tegi