unclejohn00
unclejohn00

Reputation: 149

Eclipse e4 - Logger Injection

As far as I have understood reading online articles, the embedded data-logger in Eclipse should be best deal especially if you're implementing an RCP app. (Confirm?)

Anyway I'm struggling using it.

The first issue I have faced is a problem with Injection:

import org.eclipse.e4.core.services.Logger;

@Inject
private Logger logger;

In an MPart it produces a null variable.

Upvotes: 3

Views: 606

Answers (1)

de-jcup
de-jcup

Reputation: 1885

I had similar problems when I tried to use a Logger inside a handler in a e4 plugin and starting the plugin inside Eclipse Version: 2023-12 (4.30.0).

The dependency injection was not able to resolve the logger at this point .

As a workaround I did inject the eclipse context and resolved the logger from there:


import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.swt.widgets.Shell;

public class SomeHandler {

    @Inject
    IEclipseContext context;

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s) {

        Logger logger = context.get(Logger.class);
        logger.info("Info log entry - visible in Error log view");

        // further stuff
    }

But there are some caveats:

  • org.eclipse.e4.core.services.log.Logger has restricted api access
  • when using the workaround, the plugin inside the log is always org.eclipse.e4.ui.workbench and not your own (means not like in activator loggers - which was e3 the way to access the logger)

Currently I did not found a better/quicker way. And it was the only way mentioned in the (now outdated) wiki description

Upvotes: 0

Related Questions