0

这个问题似乎与Using Logback 有关,但 Log4j 开始显示 WARN no Appenders,但那里的答案似乎并没有为我解决这个问题。

当我创建一个除了 slf4j 之外没有依赖项的 maven 项目时,我收到一个警告:

log4j:WARN No appenders could be found for logger (com.example.App).
log4j:WARN Please initialize the log4j system properly.

脚步:

在一个空目录中,执行

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart

Define value for property 'groupId': : com.example
Define value for property 'artifactId': : slf4jtest
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.example: : 

将 SLF4J 添加到 pom.xml:

<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>com.example</groupId>
  <artifactId>slf4jtest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>slf4jtest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.2</version>
      </dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.2</version>
</dependency>
  </dependencies>
</project>

在 App.java 中调用记录器

package com.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        Logger logger = LoggerFactory.getLogger(App.class);
        logger.debug("hello");
        System.out.println( "Hello World!" );
    }
}

执行:

mvn package

执行:

mvn exec:java -Dexec.mainClass="com.example.App"

输出:

log4j:WARN No appenders could be found for logger (com.example.App).
log4j:WARN Please initialize the log4j system properly.
Hello World!

log4j 未列为依赖项:

$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building slf4jtest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ slf4jtest ---
[INFO] com.example:slf4jtest:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] \- org.slf4j:slf4j-api:jar:1.7.2:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.872s
[INFO] Finished at: Tue Feb 19 11:23:33 EST 2013
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------
4

2 回答 2

1

所以我没有意识到 Mac OS X 在 /Library/Java/Extensions 下预装了 log4j 和 slf4j... 我通过运行以下命令发现了这一点:

java -verbose:class -cp ~/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:~/.m2/repository/org/slf4j/slf4j-api/1.7.2/slf4j-api-1.7.2.jar:./target/classes com/example/App

...并发现(在许多其他类中)这些类被优先加载和使用,而不是我通过 maven 包含的类。

/Library/Java/Extensions/log4j-1.2.15.jar
/Library/Java/Extensions/slf4j-log4j12-1.5.8.jar
/Library/Java/Extensions/slf4j-api-1.5.8.jar

不确定这个问题的“正确”解决方案是什么,但从该目录中删除所有这些文件似乎可以解决问题。

于 2013-02-20T06:42:03.350 回答
0

不能复制。我得到了预期的 SLF4J 绑定失败:

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.
Hello World!

我的依赖树看起来和你的一样:

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ slf4jtest ---
[INFO] com.example:slf4jtest:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] \- org.slf4j:slf4j-api:jar:1.7.2:compile
[INFO] ------------------------------------------------------------------------

系统信息:

$ mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
Maven home: d:\dev\tools\maven
Java version: 1.7.0_03, vendor: Oracle Corporation
Java home: d:\dev\tools\java\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
于 2013-02-19T02:43:33.320 回答