tags:

views:

114

answers:

3

I have created an eclipse PDE project and have added log4j as a dependency using the slf4j-api and slf4j.log4j12 bundles. In a class I have created the logger:

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

public class MyTest {

  private static final Logger logger = LoggerFactory.getLogger(MyTest.class
      .getName());

  public void tt() {
    logger.info("log-test");

  }

}

but when I run the PDE project using an OSGI launch configuration I get the warnings:

log4j:WARN No appenders could be found for logger (loggin_test.MyTest).
log4j:WARN Please initialize the log4j system properly.

based on this info:

http://jaikiran.wordpress.com/2006/07/05/i-get-log4jwarn-no-appenders-could-be-found-for-logger-message-2/

I need to put the log4j.properties in the PDE classpath. I have tried to put it in the root of the PDE project and add:

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               log4j.properties

to the build.properties pane. But I still get the same warning. Where do I put the log4j.properties file in a PDE project?

EDIT: I have also tried adding it to the /src folder but it does not help.

A: 

AFAIK, you still need the Log4j JAR, since SLF4J is just a wrapper around it. Download the JAR from the Apache web site, add it to your build path and it should work just fine.

zvikico
He must already have the JAR, otherwise it's difficult for log4j to warn him that it can't find any appenders...
hbunny
Jep I already have added org.apache.log4j version 1.2.13 to the manifest file.
tul
A: 

You've hit your first resource visibility problem with OSGi. Remember that in OSGi bundles must explicitly define their dependencies. Your problem is that your bundle defines a log4j property file, but it's the log4j bundle that must read it. But the log4j bundle doesn't know anything about your bundle (and nor should it).

Here's 2 solutions to the problem:

  1. Use Eclipse's buddy mechanism
  2. Put the log4j property file in an OSGi fragment and host it on the log4j bundle

Option 2 is a better solution, but if you find option 1 easier then go with that.

hbunny
I have done the following: 1) Created a plugin where I add org.apache.log4j as a dependency. 2) Created a fragment with the previous plugin as a host and added the log4j.properties file to the root. 3) created a new plugin which depends on the plugin from 1). But I cannot use instantiate the logger with : Logger logger = Logger.getLogger("com.foo");How do I make log4j visible in the final plugin which depends on the "wrapped" log4j plugin?
tul
When you say you cannot use Logger.getLogger, you mean you get a compile-time or runtime problem? Is the plugin from 1) just a wrapped version of log4j? How did you create it? Did you use a tool such as Bundlor or Bnd? I don't recommend wrapping well-known libraries yourself - you can find bundles of common open source libraries at: http://www.springsource.com/repository/app/
hbunny
Compile time error. Makes sense since the log4j types will only be visible in the plugin directly depending on log4j. I have tried another approach: I create a fragment (with the .properties file) and specify the org.apache.log4j as host (where I have downloaded org.apache.log4j from the website and manually added it to the running target platform ). But since I cannot depend on a fragment in my other plugin and the log4j in the target platform is not aware that it has been "extended" with the fragment I have the same problem.
tul
Eclipse already has the log4j bundle, so you shouldn't add the JAR to your plug-in, but do a Require-Bundle on log4j in your plug-in.
hbunny
No log4j is not part of the default platform. Further the problem is not adding the log4j bundle but making sure it can find the log4j.properties file when its started.
tul
A: 

I got the same problem. Adding the folder containing log4j.xml to Project Build Path is not enough (why not?). Anyway, besides the reason for that (maybe eclipse gurus can answer that for us) I solved my problem adding the folder containing log4j.xml to the eclipse "Run configuration".

After you try to run your Standalone App for the first time and get the annoying "No appenders could be found for logger" message, follow the steps bellow:

  1. Go to Menu "Run" > "Run Configurations"
  2. Your main class should appear somewhere in the left tree. Probably under "Java Application". So select it
  3. Select "Classpath" tab, select "User Entries" and click "Advanced" button.
  4. On "Advanced Options" pop-up window, select "Add Folders", click "OK" button.
  5. On "Folder Selection" pop-up, navigate to your folder where log4j.xml is located and click "OK"btn.
  6. Click "Run" button... and... Finally!
Leo Holanda