`

OSGI环境下的log4j日志配置

阅读更多

废话不多说了,log4j相信大家都用过的,现在公司有很多项目的研究都是基于OSGI的,所以我们的日志处理必须换到OSGI环境下去做了。于是相应的 问题也随之而来。其中最主要的问题就是一个classloader的问题。知道OSGI架构原理的都知道OSGI里面的各个Bundle是有独立的 ClassLoader来进行加载的。所以当我们把log4j的配置文件直接放在某个Bundleclasspath下面时是不能被整个OSGI环境识 别的。下面说我们的解决办法。核心思想参考了网上一篇文章(点击这里可以查看)。
    1.
建一个config-template.ini文件,当然位置和名字都可以由你自己安排,我这里是建在了我的OSGI环境(Eclipse或者 Equinox)的根目录下的configuration文件夹下了,这样这个配置文件就能在运行时被根加载器加载,而对所有的bundle可见,那么不仅仅是我们自定义的bundle可以用log4j,而且其他的第三方的bundle(如activemq,spring)都可以不用改动任何东西,就能使他们的log4j产生效果了,这也就是我们做这个事情的根本目的。这个文件夹下原来就有个config.ini(在部署的时候这个config.ini必须放在OSGI环境的confuguration文件夹下),是用于设置OSGI环境的启动的相关 属性的,我们可以复制它,然后稍作修改,内容如下:


 代码
  1. osgi.splashPath = file:E:/eclipse-workspace/com.oval.dcts.ba.ui  
  2. osgi.bundles=org.eclipse.equinox.common@2:start,
  3. org.eclipse.update.configurator@3:start, org.eclipse.core.runtime@start   
  4. eclipse.product=org.eclipse.sdk.ide  
  5. osgi.instance.area.default=@user.home/workspace  
  6.   
  7. eclipse.buildId=M20060921-0945  
  8.   
  9. log4j.configuration=file:/D:/Program Files/eclipse/configuration/log4j.properties 

然后是准备我的log4j的properties文件,从上面可以看到,我需要把它放到D:/Program Files/eclipse/configuration,当然这个位置也是可以你自己定的(在linux我可以很好的设置相对位置来灵活处理,但是在windows下被反斜杠搞得郁闷,暂时还没有找到好办法,只能先设个绝对地址,到时候可能需要跟使用者约定一下。),而且这个位置在目前我们找的资料来看,用绝对地址相对简单一些,当然同时存在这一些部署上的不方便,我们暂时还没有找到合适的解决方案,不过以后会加以改进,先让我们的目的达到了再说。

log4j.properties 里的写法如下:


xml 代码
  1. ## ------------------------------------------------------------------------  
  2. ## Licensed to the Apache Software Foundation (ASF) under one or more  
  3. ## contributor license agreements.  See the NOTICE file distributed with  
  4. ## this work for additional information regarding copyright ownership.  
  5. ## The ASF licenses this file to You under the Apache License, Version 2.0  
  6. ## (the "License"); you may not use this file except in compliance with  
  7. ## the License.  You may obtain a copy of the License at  
  8. ##  
  9. ## http://www.apache.org/licenses/LICENSE-2.0  
  10. ##  
  11. ## Unless required by applicable law or agreed to in writing, software  
  12. ## distributed under the License is distributed on an "AS IS" BASIS,  
  13. ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14. ## See the License for the specific language governing permissions and  
  15. ## limitations under the License.  
  16. ## ------------------------------------------------------------------------  
  17.   
  18. #  
  19. # The logging properties used for eclipse testing, We want to see debug output on the console.  
  20. #  
  21.   
  22. log4j.rootLogger=INFO, stdout, out  
  23.   
  24. # CONSOLE appender not used by default  
  25. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  26. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  27. #log4j.appender.stdout.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n  
  28. #log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n  
  29. log4j.appender.stdout.layout.ConversionPattern=%d %-5p %-5C:%L %x -> %m%n  
  30.   
  31. # File appender  
  32. log4j.appender.out=org.apache.log4j.RollingFileAppender  
  33. log4j.appender.out.file=./log/log4j.log  
  34. log4j.appender.out.maxFileSize=1024KB  
  35. log4j.appender.out.maxBackupIndex=5  
  36. log4j.appender.out.append=true  
  37. log4j.appender.out.layout=org.apache.log4j.PatternLayout  
  38. log4j.appender.out.layout.ConversionPattern=%d %-5p %-5C:%L %x -> %m%n  
  39.   
  40.   
  41. log4j.logger.org.springframework=INFO  
  42. log4j.logger.org.springframework.jdbc.core=INFO  

其中比较关键的为log4j.appender.out.file=./log/log4j.log。这里告诉环境把日志文件的保存位置,这样写的话便会将log文件保存在你的环境的根目录下的log文件夹下,文件为log4j.log。

接下来,我们需要在OSGI的启动环境里加上commons-logging和log4j这两个OSGI的bundle咯。在这个地方我们遇到些问题,测试了很久日志都打不出来,后来通过一步步debug才发现问题出在了这两个bundle上,所以大家最好下个最新版的。在启动选项的目标平台里把这两个bundle勾选上。接下来就是到“配置”栏去设置环境加载时使用的配置文件了,在配置文件栏选择“使用现有config.ini文件作为模板”(默认情况,eclipse会在他的运行环境中自己生成一个默认的ini文件,里面没有我们之前的对log4j做的配置)。选择好后,就开始把我们的OSGI环境跑起来吧。

可以看到日志成功的打到了日志文件里了。

由于第一帖不知道怎么贴图片,我把文章中用到的图片都发到附件里了,大家可以结合图来看,文笔比较差,有没讲解清楚的地方希望大家原谅~

enjoying~

  • 描述: 勾选上commons-logging的 Bundle
  • 大小: 4.8 KB
  • 描述: 勾选上log4j的bundle
  • 大小: 3.4 KB
  • 描述: 使用现有config.ini文件作为模板
  • 大小: 113.2 KB
分享到:
评论
7 楼 yuyou6 2007-11-28  
log4j.configuration=file\:plugins/log4j.properties
为相对路径的配制方法
6 楼 amyer 2007-11-22  
我使用了1楼的方法,运行提示如下:
log4j:WARN No appenders could be found for logger (com.thtf.ezone.m2m.alarm.generator.AlarmGeneratorComponent).
log4j:WARN Please initialize the log4j system properly.

这是说没有初始化log4j的配置,请问这一步的代码在哪里去实现呢?还是应该由equinox自己已经实现了呢?请1楼给指点一下吧。我使用的是eclipse3.3,其中的bundle是:org.apache.log4j_1.2.13.v200706111418.jar 和
org.apache.commons.logging_1.0.4.v200706111724.jar
5 楼 yapex 2007-09-21  
to zhufanamo
这样的方式的主要缺陷就是如果你有多个bundle,那么你必须为每一个bundle做同样的配置,未免有些累赘。

to zengxxcn
这样的解决方案看似正统(符合osgi的标准),但是有两个问题,第一,已经有大量的第三方包使用了log4j或者是commons-logging,我们没有办法马上让这些第三方包统一使用osgi的log service的方式输出日志。
第二,这样的解决方案对osgi环境直接产生了依赖,开发的bundle不能在脱离了osgi的环境中使用。这个是我们非常不愿意看到的。

呵呵,其实即使是zhoufu24现在所说的解决方案,我们也不是特别的满意,原因在于osgi是一个动态环境,非常有别于我们传统的设计和开发思想。是不是每个bundle都应该能自己决定对日志输出的默认级别呢?现在的解决方案虽然能够工作,但是,每当新的bundle被动态加入到环境中,就需要手动修改log4j.properties文件了。

理想的日志解决方案应该能够满足:
1. 能够兼容已有日志输出的方式
2. 对osgi环境没有强制的依赖
3. 每个bundle能够自行决定默认的日志输出级别
4. 通过osgi的admin service能够动态改变每个bundle日志输出的级别
4 楼 alin_ass 2007-09-18  
偶还是支持2楼的~虽然osgi log service用起来稍微麻烦点
3 楼 zhoufu24 2007-07-19  
嗯,一楼的方法好像还是个比较不错的方法,我会在以后的项目中尝试下这种方式。之前一直觉得OSGI的这个日志问题蛮烦得,我说的这种方法也是慢慢研究出来的。最大的限度直接利用OSGI本身的特性,而不需要做任何程序修改。也应该不错的。
2 楼 zengxxcn 2007-07-16  
看看我的log实现:

所有的业务bundle都使用OSGi Log Service去记录log;
另外有一个log writer bundle,它使用LogListener监听Log Service中发生的log entry,使用log4j存储下来。
1 楼 zhufanamo 2007-07-14  
还有一种很更简单的的方式

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 {
    }

}

相关推荐

Global site tag (gtag.js) - Google Analytics