0

So this is a follow up issue that arrised with this question: Using log4j in eclipse RCP doesn't work.

So far I'm able to use the log4j API inside my RCP but when running it with the following command

Category CAT = Category.getInstance(getClass().getSimpleName());
CAT.debug("Application has been started");

I get that exception:

No appenders could be found for category (MyPlugin).
Please initialize the log4j system properly.

I have created a fragment plugin containing a file called log4j.properties and the fragment host is the plugin containing the log4j API. The property file lives in the "root" of the fragment plugin

My log4j.properties file looks like that:

# Set root logger level to debug and its only appender to default.
log4j.rootLogger=debug, default
# default is set to be a ConsoleAppender.
log4j.appender.default=org.apache.log4j.ConsoleAppender
# default uses PatternLayout.
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Any ideas what I'm doing wrong?

Community
  • 1
  • 1
Markus
  • 1,452
  • 2
  • 21
  • 47
  • It might be so, that the log4j.properties are not in the classpath of your logging plugin. Not sure how to configure it with your set up, but one thing, that you could try is to call PropertyConfigurator.configure(properties/fileName); – Alex K. Nov 20 '13 at 13:33
  • I'm sure it is not part of my classpath but I have no clue why. I neither know how to check if the fragment plugin is loaded nor if the property file is "added" to my rcp application. Calling your proposed function leads to "Could not read configuration file [log4.properties] follwoed by a java.io.FileNotFoundException. My setup is not complicated. Its a standard eclipse4 rcp with a plugin for log4j and a fragment plugin for the configuration file. – Markus Nov 20 '13 at 13:43

1 Answers1

2

I am not sure, what is the purpose of using fragment for holding configuration. Try to place your log4j,properties (note that the name of the file is misspelled in your answer) directly to your plugin root folder. In the Activator.start() execute following code:

public void start(BundleContext context) throws Exception {
  super.start(context);
  URL installURL = plugin.getBundle().getEntry("/");
  String installPath = Platform.asLocalURL(installURL).getFile();
  PropertyConfigurator.configure(installPath +"/log4j.properties");
}

First of all, category documentation says: This class has been deprecated and replaced by the Logger subclass.

Secondly, Category has to be mapped to an appender:

log4j.category.MyPlugin=info,MyPlugin
log4j.appender.MyPlugin=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MyPlugin.layout=org.apache.log4j.PatternLayout
log4j.appender.MyPlugin.layout.ConversionPattern={%t} %d - [%p] %c: %m %n
log4j.appender.MyPlugin.file=C:/logs/MyPlugin.log
log4j.appender.MyPlugin.DatePattern='.' yyyy-MM-dd-HH-mm
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • Looks like my rcp finds the file now but there are still issues. I get the following errror message when executing the configure method: "Could not find root category information. Is this OK?" When I try to log some text I get the same eror message as in the first place: No appenders could be found for category (MyPlugin). Please initialize the log4j system properly. – Markus Nov 20 '13 at 14:11
  • Also I'm using now the log4 configuration proposed at this question: http://stackoverflow.com/questions/2128185/log4j-sample-configuration-file-properties-file . – Markus Nov 20 '13 at 14:15
  • If you have found Logger try using it instead. Ore create appender MYPlugin in log4j.properties – Alex K. Nov 20 '13 at 14:15
  • What do you mean by "create apender MyPlugin". Thanks anyways for your help! I don't want to use custom appenders at all. – Markus Nov 20 '13 at 14:34