[ 
https://issues.apache.org/jira/browse/LOG4J2-2305?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16480696#comment-16480696
 ] 

Björn Kautler commented on LOG4J2-2305:
---------------------------------------

The problem / difference is, that log4j now depends on slf4j 1.8.
 Starting with 1.8 slf4j deprecated the old method to provide an implementation 
and replaced it by usage of {{java.util.ServiceLoader}}.
 Within OSGi this does not instantly work propery as each bundle is kept in a 
separate class loader and thus the service loader mechanism cannot find the 
service definition file and thus the service providing the implementation.

For using the new service loader mechanism in OSGi world, you have to utilize 
the Service Loader Mediator Specification on both sides.

But thanks to OSGi bundle fragments, you can easily enable the bundles to 
properly talk to each other.

You can generate those bundle fragments e. g. via the following two Gradle 
tasks as workaround:
{code}
plugins {
    id 'osgi'
}

task slf4jApiFragmentJar(type: Jar) {
    baseName = project.name + '-slf4jApiFragment'
    manifest = osgiManifest {
        name = 'net.kautler.test.osgi.slf4j.api.fragment'
        symbolicName = 'net.kautler.test.osgi.slf4j.api.fragment'
        version = '1.0.0'
        classesDir = temporaryDir
        classpath = files()
        instruction 'Fragment-Host', 'slf4j.api'
        instruction 'Require-Capability', '' +
                'osgi.extender;' +
                'filter:="(osgi.extender=osgi.serviceloader.processor)",' +
                'osgi.serviceloader;' +
                
'filter:="(osgi.serviceloader=org.slf4j.spi.SLF4JServiceProvider)"'
    }
}

task log4jSlf4jImplFragmentJar(type: Jar) {
    baseName = project.name + '-log4jSlf4jImplFragment'
    manifest = osgiManifest {
        name = 'net.kautler.test.osgi.log4j.slf4j.impl.fragment'
        symbolicName = 'net.kautler.test.osgi.log4j.slf4j.impl.fragment'
        version = '1.0.0'
        classesDir = temporaryDir
        classpath = files()
        instruction 'Fragment-Host', 'org.apache.logging.log4j.slf4j-impl'
        instruction 'Require-Capability', '' +
                'osgi.extender;' +
                'filter:="(osgi.extender=osgi.serviceloader.registrar)"'
        instruction 'Provide-Capability', '' +
                'osgi.serviceloader;' +
                'osgi.serviceloader=org.slf4j.spi.SLF4JServiceProvider'
    }
}
{code}
 

PS: Likewise you have to provide fragments for log4j-api and log4j-core to be 
able to be properly found and to add a log4j configuration file if not doing 
the configuration programmatically.

> Log4j 2.10+not working with SLF4J 1.8 in OSGI environment
> ---------------------------------------------------------
>
>                 Key: LOG4J2-2305
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2305
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.10.0, 2.11.0
>            Reporter: Rob Gansevles
>            Priority: Major
>
> I have a simple osgi plugin that just logs something to slf4j Logger in the 
> Activator's bundle start method.
> When I install and start log4j-core-2.11.0.jar, log4j-api-2.11.0.jar, 
> log4j-slf4j-impl-2.11.0.jar and slf4j-api-1.8.0-beta1.jar in a clean felix 
> gogo shell, and then install and start my plugin, it prints the 
> No-SLF4J-providers-were-found message.
>  
>  
> It does work if I do the same with log4j  2.9.1 and slf4j-api-1.7.25.
>  
> You can find my simple plugin on github: 
> [https://github.com/rgansevles/osgi-slf4j-sample]
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to