Markus
Markus

Reputation: 1482

Eclipse4 RCP: Configure log4j with fragment plugin

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?

Upvotes: 0

Views: 1062

Answers (1)

Alex K.
Alex K.

Reputation: 3304

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

Upvotes: 2

Related Questions