SLF4J and Log4j 2 binding Maven dependency

♀尐吖头ヾ 提交于 2019-12-23 19:14:23

问题


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");
    }
}

回答1:


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



来源:https://stackoverflow.com/questions/19012994/slf4j-and-log4j-2-binding-maven-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!