Search code examples
javamavenosgislf4jsling

OSGi slf4j logging in Apache Sling/felix


I am trying to use slf4j logger in my OSGi bundle for Apache Sling.

When adding the dependency and import tag to the POM.xml, the bundle remains in resolved state.

Am I something missing?

Activator.class

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

    public class Activator implements BundleActivator {
    ...
    private final Logger log = LoggerFactory.getLogger(SampleServiceImpl.class);
    ...

POM.xml

<Import-Package>org.slf4j</Import-Package>
...
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
            <scope>provided</scope>
        </dependency>

Apache Sling Web Console / Bundle View

EDIT If I comment out the Logger instantiation and remove the imports from the Activator.class the service stays in resolved state. Once I remove the lines regarding slf4 in the POM, everything works.


Solution

  • I suppose your <Import-Package>org.slf4j</Import-Package> statement prevents other required packages from being imported. The value of that element should rather be org.slfj4.*,* so that all "automatic" imports are generated. See the maven-bundle-plugin docs for more info.

    But if your pom is similar to that of a Sling bundle like the slingbucks sample you shouldn't need such explicit import statements anyway.