2

I'm currently implementing a Logger and I'm wondering why the code won't run. Most of the codes snipets are like these:

Logger log = LoggerFactory.getLogger(this.getClass());

My imported classes:

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;
import org.osgi.service.log.Logger;
import org.osgi.service.log.LoggerFactory;

But I can't seem to use getLogger.

Why is that?

Thanks in advance! No getLogger() classes are available

mengmeng
  • 1,266
  • 2
  • 22
  • 46

2 Answers2

3

Here is the source code of org.osgi.service.log.LoggerFactory.

As you can see, it's an interface with no static methods therefore this code:

Logger log = LoggerFactory.getLogger(this.getClass());

is simply not valid.

To fix this use slf4j as front end (this means replace org.osgi.service.log.LoggerFactory import with org.slf4j.LoggerFactory etc.).

UPDATE

If you want to stick with org.osgi.service.log.LoggerFactory then follow this:

Obtain the LoggerFactory instance:

public class Activator implements BundleActivator
{
    private volatile LoggerFactory loggerFactory;

    public void start(BundleContext context) throws Exception 
    {   
        ServiceReference ref = context.getServiceReference(LoggerFactory.class.getName());
        if (ref != null)
        {
            loggerFactory = (LoggerFactory) context.getService(ref);
        }
    }

    //..

Elsewhere in the bundle you can then use the LoggerFactory to get a Logger for any class:

Logger logger = loggerFactory.getLogger(Foo.class);

UPDATE2

A better alternative would be to get a reference who's service type is LoggerFactory like this:

@Reference(service = LoggerFactory.class)
private Logger logger;
  • But I need to use this LoggerFactory/Logger to change from LogService (since LogService has been depreciated in 1.4) Please help. Thanks! – mengmeng Nov 21 '18 at 09:56
  • Oh. I need to use plugins from eclipse only >_ – mengmeng Nov 21 '18 at 10:04
  • May I know what Foo.class here? Thanks! – mengmeng Nov 22 '18 at 05:40
  • @MelSyGallosa Is the class you want to log. –  Nov 22 '18 at 07:28
  • Thank you! I got another question. I implemented this but when I used logger.info(message) and used the LogListener. It doesn't seem to go through the LogListener.logged method. (I can't get the logentry) why is that? Thanks – mengmeng Nov 22 '18 at 07:32
  • Please do not ask new questions in the comments. Post a new question in the OSGi topic. – Neil Bartlett Nov 22 '18 at 14:47
  • The code in this answer for obtaining a Logger is very problematic. It is far better to use: `@Reference(service=LoggerFactory.class) private Logger logger`. Please see the presentation linked in @bjhargrave's answer. – Neil Bartlett Nov 22 '18 at 14:49
  • @NeilBartlett If you follow the link I gave, you would find also the service injection as a solution. The two solutions are, actually, equivalent. –  Nov 22 '18 at 14:53
  • @EugenCovaci They are not equivalent: for example, in the code given in the answer you never release the service. Maybe show the better solution in the actual answer rather than hidden behind a link (which could become invalid in a few years time)? – Neil Bartlett Nov 22 '18 at 15:36
  • 1
    @NeilBartlett Done. –  Nov 22 '18 at 15:41
  • @EugenCovaci JRE needs to be 1.5. I could only use 1.4 due to my current environment – mengmeng Nov 26 '18 at 06:46
1

LoggerFactory is an OSGi service. You need to get it from the OSGi service registry. See my EclipseCon Europe 2018 presentation for more information.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27