0

This question seems to be related to Using Logback but Log4j started displaying WARN no Appenders, but the answer there doesn't seem resolve this for me.

When I create a maven project with NO dependencies other than slf4j, I get a warning:

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

Steps:

In an empty directory, execute

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

Add SLF4J to 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>

Invoke the logger in 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!" );
    }
}

Execute:

mvn package

Execute:

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

Output:

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

log4j is not listed as a dependency:

$ 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] ------------------------------------------------------------------------
Community
  • 1
  • 1
studds
  • 1,395
  • 10
  • 15

2 Answers2

1

So I didn't realise that Mac OS X comes prepackaged with log4j and slf4j under /Library/Java/Extensions... I discovered this by running this command:

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

... and found that (amongst many others) these classes were being loaded and used in preference to the ones I was including via 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

Not sure what the "correct" solution to this problem is, but deleting all of those files from that directory seemed to do the trick.

studds
  • 1,395
  • 10
  • 15
  • Very interesting. The ["Java Development Guide for Mac"](http://developer.apple.com/library/mac/#documentation/Java/Conceptual/Java14Development/01-JavaOverview/JavaOverview.html#//apple_ref/doc/uid/TP40001883-SW1) seems to imply that this directory is for users to add libraries to. Are you sure you or someone else didn't put those there at some point, maybe even by accident? It seems totally crazy for OS X to come with those there by default. – Ryan Stewart Feb 20 '13 at 23:45
  • @RyanStewart, it does seem crazy... I have no recollection of adding in these libraries. I'm the only user of the computer. Perhaps they got installed along with some other application, although that would seem to be very poor form. – studds Feb 22 '13 at 02:57
  • Google'd and found http://www.geniuswiki.com/page#%2FGeniusWiki%2Bdocument%2FInstall%2Band%2Bupgrade which says it's included with OSX, but also http://issues.gradle.org/browse/GRADLE-1451 says that they came with AUSKey, which I've installed in the past - so not 100% sure. – studds Feb 22 '13 at 03:02
  • Thanks so much - I had a bunch of AUSKey stuff there that was preventing maven output. Moving those files to a backup location fixed my log4j:WARN issue. – David Hogan Jun 12 '14 at 20:22
0

Can't duplicate. I get the expected SLF4J binding failure:

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!

My dependency tree looks the same as yours:

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

System 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"
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199