Eclipse PDE and log4j.properties?

0 votes
asked Aug 26, 2010 by u123

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.

5 Answers

0 votes
answered Aug 26, 2010 by zvikico

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.

0 votes
answered Aug 26, 2010 by steved

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.

0 votes
answered Aug 27, 2010 by leo-holanda

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!
0 votes
answered Aug 16, 2013 by jdknight

I've just had the same situation when I tried to shift from using slf4j-simple to slf4j-log4j12 (while adding the log4j jar). After doing some debugging, it is just as SteveD mentions, the issue is a result of resource visibility. In my situation, I had a plugin which held my logging libraries; however, I did not want to specify my logging properties in a shared plugin.

My approach, which may work for your situation, was to have a logging configuration for each plugin that you wish to have logging capabilities. For a plugin that would required logging, I would add something like the following to the plugin's activator:

Bundle bundle = bundleContext.getBundle();
URL entry = bundle.getEntry("log4j.properties");

try(InputStream inputStream = entry.openStream())
{
    PropertyConfigurator.configure(inputStream);
}
catch(IOException e) {}

Even though it is unfortunate that code is required to be added to the activator; I was able to configure different logging capabilities per plugin, I did not have to deal with new plugins / Eclipse buddy registrations and also did not have to deal with managing log4j.properties files in any manifest or build.properties file.

Other Notes

  • When the failed-to-initialize message appears ("Please initialize the log4j system properly"), it will only appear once. This gave me some confusion at first when I didn't detect that two (2) plugins were configured incorrectly but only one (1) error message showed up.
0 votes
answered Sep 15, 2017 by christopher-hull

Why not use the platform logging facility?

I've recently added logging to my Eclipse plugin. I used the logging facility that is built into the PDE. I'll post this just in case that option was somehow overlooked. I didn't realize that I didn't need to add log4j at first, perhaps others don't realize that either.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...