【问题标题】:slf4j & log4j2 maven setup queryslf4j & log4j2 maven 设置查询
【发布时间】:2014-10-12 17:52:08
【问题描述】:

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

消息:

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 文件

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

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

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

【问题讨论】:

    标签: java maven logging slf4j log4j2


    【解决方案1】:

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

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

    【讨论】:

    • groupId 应该是“org.apache.logging.log4j”和版本“2.0.1”,如下所述:logging.apache.org/log4j/2.x/maven-artifacts.html
    • 我的错误 - 是的,版本应该是 2.0.1 而不是 beta 版本,而不是适配器组 ID
    • @user3813256 那如果答案没有解决问题,你为什么将答案标记为正确?
    【解决方案2】:

    你的 log4j2 配置正确(POM 端),但你从来没有告诉 slf4j 它应该写在哪里(后端部分)。

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

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

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

    如果仍然无法正常工作,您可能遇到了 Eclipse 问题,因为 Eclipse m2e 已知在 slf4j 方面很奇怪。根据 SO SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. error 的这篇详细帖子,解决方法可能是使用外部 maven 进行构建。

    【讨论】:

    • 当我通过 Eclipse 运行 maven clean/install 时,我仍然看到相同的消息。我的 maven 存储库中似乎有所有依赖项 - slf4j=api-1.7.7, log4j-slf4j-impl-2.0.1, log4j-api-2.0.1, log4j-core-2.0.1 但我收到了消息当我运行 Maven 并且没有生成我的日志文件输出时。当我不使用 maven 但我的 lib 目录中有所有 jars 并使用 eclipse 作为构建路径时,我得到了具有相同 log4j2 xml 文件的文件输出
    • @user3813256:我希望这就足够了,因为我不使用 Eclipse,但我已经通过引用另一篇文章更新了我的答案。
    • 我会尽快接受 - 我认为你是对的,但我需要在我的电脑上证明 - 谢谢你的帮助!
    【解决方案3】:

    除了 log4j-slf4j-impl 依赖,你还需要 slf4j-ext 依赖。

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

    【讨论】:

    • 最后一列显示 Optional = YES。
    【解决方案4】:

    我认为你的第一个 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>
    

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 2018-07-30
      • 2021-03-19
      • 2015-09-13
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多