OpenCore:OSGi上部署Apache Common Log

 

Apache Common Log提供的动态Logger发现机制实在损害Apache基金会的声誉,这是一个看似巧妙实际上画蛇添足的设计。这种机制无法保证在有多 ClassLoader的JEE或OSGi环境下正常工作,这几天收到了Spring-OSGi google群组发出的“Commons logging madness”信件超过20封,今天又在java-blog上收到“common log woes"的文章,估计这个问题把很多人搞疯了:)

最后,这帮家伙终于讨论出了解决方案,就是用SLF4J来替换Apache Common Log的实现。我们今天用这个方案替换掉了OpenCore原来的common log插件,静态帮定到Log4J,工作良好。

具体插件实现如下:

lib目录加入:
jcl104-over-slf4j-1.1.0.jar
log4j-1.2.13.jar 
slf4j-api-1.1.0.jar 
slf4j-log4j12-1.1.0.jar

插件的自描述文件(MANIFEST.MF)配置:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.apache.commons.log
Bundle-Version: 2.0.0
Bundle-ClassPath: .,
 lib/slf4j-api-1.1.0.jar,
 lib/log4j-1.2.13.jar,
 lib/jcl104-over-slf4j-1.1.0.jar,
 lib/slf4j-log4j12-1.1.0.jar
Bundle-Vendor: %pluginProvider
Bundle-Localization: plugin
Export-Package: org.apache.commons.logging ;version="1.1.0"
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Import-Package: org.osgi.framework
Bundle-Activator: org.apache.commons.log.osgi.Activator

然后实现一个简单的Activator,根据自己需求配置Log4j:

package org.apache.commons.log.osgi;

import java.io.File;

import org.apache.log4j.PropertyConfigurator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        File file = new File("./etc/log4j.properties");
        PropertyConfigurator.configure(file.toURI().toURL());
    }

    public void stop(BundleContext context) throws Exception {
    }

}

你可能感兴趣的:(apache,log4j,log,osgi,slf4j)