Activating an OSGI LogListener before other servic

2019-07-19 03:11发布

问题:

I am writing a OSGI-based desktop application, using Apache Felix, Declarative Services and Maven. It's going to use JavaFX, so I'm using Drombler FX too.

In the bundles I develop, I'd like to use an appropriate logging mechanism, and I'm currently trying to make Apache Felix Log work. I have no problem getting a LogService but this is not sufficient to make output appear on the console.

The word on the net is that Everit's osgi-loglistener-slf4j will do the job and – sure enough – after osgi-loglistener-slf4j has been activated log output appears on the console.

However, osgi-loglistener-slf4j is not activated until after all my own bundles, so none of the log information that I am interested in is output. I've tried creating a @Reference to a LogLevel in my first bundle to try and force activation of osgi-loglistener-slf4j, but without success.

How do I get osgi-loglistener-slf4j activated before everything else? I have read about start levels, but I haven't been able to find any information about how to apply them in my context (i.e. desktop not server, so no PAX or Karaf; Maven; and I use NetBeans, so no Eclipse).

回答1:

Drombler FX uses SLF4J: http://www.drombler.org/drombler-fx/0.7/docs/tutorial/logging.html#logging

Just use the following and it should work:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;  

...

private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);

In the POM add the following dependency:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>