Java slf4j & log4j2 maven 设置查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25386651/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:17:40  来源:igfitidea点击:

slf4j & log4j2 maven setup query

javamavenloggingslf4jlog4j2

提问by

I am using log4j2 and slf4j in my project & using maven for the build. I am using the following pom file (releveant dependencies shown only) but I am getting the error copied below with this pom file - any idea what I need to add/remove to get this to work. I have already visited the url in the error as well as the log4j2 dependencies page so please do not just point to URLs in your response.

我在我的项目中使用 log4j2 和 slf4j 并使用 maven 进行构建。我正在使用以下 pom 文件(仅显示相关的依赖项),但我在下面使用此 pom 文件复制了错误 - 知道我需要添加/删除什么才能使其正常工作。我已经访问了错误中的 url 以及 log4j2 依赖项页面,所以请不要只在响应中指向 URL。

Message:

信息:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

pom file

pom文件

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <slf4j.version>1.7.7</slf4j.version>
</properties>

   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>


    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.2.0</version>
    </dependency>

     <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0.1</version>
     </dependency>

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

Update:I added the following dependency to my pom file and I see the jar in my mavenrepository - though I still see the same message when I run mvn clean/install

更新:我将以下依赖项添加到我的 pom 文件中,并且在我的 mavenrepository 中看到了 jar - 尽管我在运行 mvn clean/install 时仍然看到相同的消息

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0.1</version>
    </dependency>

采纳答案by ali haider

You seem to be missing the following from your pom file.

您的 pom 文件中似乎缺少以下内容。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.0.1</version>
</dependency>

回答by Serge Ballesta

Your log4j2 configuration in correct (POM side), but you never say to slf4j where it should write (backend part).

您的 log4j2 配置正确(POM 端),但您从未对 slf4j 说它应该写在哪里(后端部分)。

Your should add that to your pom file

你应该把它添加到你的 pom 文件中

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.0.1</version>
</dependency>

It is the Log4j 2 SLF4J Binding. According to Log4j 2 SLF4J Binding documentationThe Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation

它是 Log4j 2 SLF4J 绑定。根据Log4j 2 SLF4J Binding 文档Log4j 2 SLF4J Binding 允许编码到 SLF4J API 的应用程序使用 Log4j 2 作为实现

If it still does not work, you may have an Eclipse problem because Eclipse m2e is known to be weird regarding slf4j. According to this detailed post from SO SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. errora workaround could be to use an external maven to do the build.

如果它仍然不起作用,则您可能遇到了 Eclipse 问题,因为众所周知 Eclipse m2e 在 slf4j 方面很奇怪。根据 SO SLF4J 的这篇详细帖子:无法加载类“org.slf4j.impl.StaticLoggerBinder”。错误解决方法可能是使用外部 Maven 进行构建。

回答by Remko Popma

Apart from the log4j-slf4j-impl dependency, you also need the slf4j-ext dependency.

除了 log4j-slf4j-impl 依赖项之外,您还需要 slf4j-ext 依赖项。

See http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/dependencies.html

http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/dependencies.html

回答by joker zhang

I think your first pom.xml is correct, (dependency is correct), maybe config file location is wrong

我认为你的第一个 pom.xml 是正确的,(依赖是正确的),也许配置文件位置是错误的

pom.xml

pom.xml

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>