1

I have created an eclipse RCP Plugin for custom Logging API using log4j. my logging API prints log on console only. I want to print them in Eclipse Error Log View. Please suggest how to do this in eclipse RCP plugin.

I tried ILogListner and StatusManager but couldn't succeed. Please give some suggestions or sample code regarding this use case.

here I write custom appender and add this appender to my Logger.

public class VirtualConsole extends ConsoleAppender{

      @Override
        public void append(LoggingEvent event) {
            int level = IStatus.INFO;
            if (event.getLevel().equals(Level.ERROR))
                level = IStatus.ERROR;
            IStatus status = new Status(level, event.categoryName,event.getMessage().toString());
            StatusManager.getManager().handle(status, StatusManager.LOG|StatusManager.SHOW);

            //and the normal logging
            super.append(event);
      }
}

LoggerService.java

private static Logger LOGGER = Logger.getLogger(LoggerService.class.getName());
VirtualConsole v = new VirtualConsole();
LOGGER.addAppender(v);

My log4j.properties file

#Define root logger options
log4j.rootLogger=debug, console

#Define console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n

LoggerService is an OSGI Service which is a custom logger service based on log4j.

I have a client code which consumes this LoggerService and uses log method. whenever client invoke any LoggerService method (i.e loggerService.info("msg") ) this message is logged into the console but I want to log in eclipse Error view.

I already referred eclipse plugin development: error logging in log4j to error view

Note:- both client and service is an OSGI bundle.

Update:- i have on Logger Service OSGI bundle which return lo4j instance to another Client OSGI bundle and then client use this service for logging.

  • Did you debug if your appender is actually called when invoking the service method? – stempler Apr 04 '17 at 07:59
  • Yes it is calling, and log & status is showing in console but not in Eclipse Error log. –  Apr 04 '17 at 08:06
  • In ErrorLogObserver (as stated in the answer) I use the Log of the Activator (extending AbstractUIPlugin), maybe that makes a difference to using the StatusManager. – stempler Apr 04 '17 at 08:15
  • i tried with Activator.getDefaut().getLog(). but not successed. please cloud you suggest small use case with log4j & appenders or some way to log the messages in eclipse error log. i create logger instance in one service and use logger api in another bundle using osgi service. –  Apr 04 '17 at 08:33

1 Answers1

1

We created a library called slf4j-plus some time ago for integrating logging into the Eclipse RCP application hale studio. It offers a couple of features extending slf4j, among them also an appender that logs to the error view. You can find more information in the documentation.

So if it is an option for you to use slf4j and logback for logging you could use the library (if not, at least you use it as an example that you can follow; the actual logging to the error log happens in the ErrorLogObserver). There is a logging adapter for log4j (and Apache commons logging, etc.) to route log4j logging to slf4j, this is how you could integrate it with your current logging.

Here is a p2 repository / update site with the current version of the bundles in case you want to test it (does not include slf4j and logback bundles though).

stempler
  • 750
  • 6
  • 15