`

OSGI环境下配置log4j日志

    博客分类:
  • java
阅读更多

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环境跑起来吧。

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

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

分享到:
评论
1 楼 gaoyanfu 2011-08-21  
附件在哪呢

相关推荐

Global site tag (gtag.js) - Google Analytics