Java SLF4J 和 Log4j 2 绑定 Maven 依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19012994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SLF4J and Log4j 2 binding Maven dependency
提问by Steve Atkinson
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.
希望是一个简单的问题,但我的 google foo 没有让我失望 - 我有一个 maven 项目,我们在其中使用 SLF4J 和 Log4J 1.2 绑定。
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.
我们现在想要迁移到 Log4j 2,特别是为了性能改进 - 但是,我一生都无法找到 log4j 2.0 绑定的 maven 依赖项。我在http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/找到了一些注释,但没有提到任何依赖信息。
I'm also slightly confused by the fact there's apparently two ways to put slf4j on top of log4j2 (binding or adaptor)
我也有点困惑,因为显然有两种方法可以将 slf4j 放在 log4j2(绑定或适配器)之上
What's the correct way to bind slf4j with log4j2 and how do I define the maven dependencies?
将 slf4j 与 log4j2 绑定的正确方法是什么,如何定义 maven 依赖项?
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)
线程“main”中的异常 java.lang.NoSuchMethodError: org/apache/logging/log4j/spi/AbstractLoggerWrapper.(Lorg/apache/logging/log4j/spi/AbstractLogger;Ljava/lang/String;)V 在 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:
我的 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:
和我的测试java类:
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");
}
}
采纳答案by Tegi
"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.
这加上类路径上的有效 log4j2.xml 应该可以帮助您入门。