user2623906
user2623906

Reputation:

How to write log4j logs into eclipse Error Log View in RCP plugin

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.

Upvotes: 1

Views: 927

Answers (1)

stempler
stempler

Reputation: 750

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).

Upvotes: 1

Related Questions