OSGi中使用log4j

最近在看关于OSGi方面的内容,自己做了些简单的例子。虽然在log方面OSGi有提供一些实现,但我们javaer们早已习惯了log4j,于是很想试试看log4j在OSGi上能不能用。

在网上搜了下,相关的资料不多也不少,但都不是很详细,所以记下来,方便大家参考一下。

例子用到了:
[list]
[*]apache的felix(版本4.0.2)
[*]springDM(版本1.2.1)中的log4j的jar包。
[/list]
首先,实现一个bundle类:
public class Activator implements BundleActivator
{
private static final Logger log = Logger.getLogger(Activator.class);

public void start(BundleContext context)
{
log.info("Starting our bundle.");
}

public void stop(BundleContext context)
{
log.info("Stopping our bundle.");
}
}

代码十分简单,仅仅是为了看看我们的log是否都工作,接着准备相应的manifest.mf文件:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_29-b11-402-11M3527 (Apple Inc.)
Bundle-Name: LogTest
Bundle-Description: A bundle that test logging
Bundle-Vendor: OuYang
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example1.Activator
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework,org.apache.log4j
Bundle-SymbolicName: exampleOne

因为用到了log4j,所以Import-Package中加上了org.apache.log4j,接着把上面的Activator.class文件和manifest.mf文件打包成jar文件(命名为example1.jar)。

要在OSGi中使用log4j,需要在一个单独的bundle中提供log4j.properties文件,并且这个bundle要定义成fragment的,先看manifest.mf文件:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_29-b11-402-11M3527 (Apple Inc.)
Bundle-ManifestVersion: 2
Bundle-SymbolicName: Log4JProperties
Fragment-Host: org.springframework.osgi.log4j.osgi

上面的Fragment-Host: org.springframework.osgi.log4j.osgi表示这个bundle是fragment的,它的主bundle是org.springframework.osgi.log4j.osgi,也就是springDM中的log4j这个jar包了。

接着准备一个log4j.properties文件:
log4j.rootLogger=INFO, stdout, R
log4j.logger.tutorial=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d]-%-5p %t | %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/Users/ouyang/logs/osgi-new-test.log
log4j.appender.R.MaxFileSize=5000KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d]-%-5p %t | %m%n

接着把manifest文件和log4j.properties文件打包成jar文件(命名为log-bundle.jar)。

最终生成的两个bundle的结构如下:
example1.jar
--tutorial.example1
  --Activator.class
--MATE-INF
--MANIFEST.MF

log-bundle.jar
--MATE-INF
--MANIFEST.MF
--log4j.properties

最后启动felix,安装并启动相关的bundle,就可以看到log 信息被成功的输出了。
g! install file:///Users/ouyang/Desktop/123/log4j.osgi-1.2.15-SNAPSHOT.jar
Bundle ID: 5
g! install file:///Users/ouyang/Desktop/123/log-bundle.jar
Bundle ID: 6
g! install file:///Users/ouyang/Desktop/123/example1.jar
Bundle ID: 7
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (4.0.2)
1|Active | 1|Apache Felix Bundle Repository (1.6.6)
2|Active | 1|Apache Felix Gogo Command (0.12.0)
3|Active | 1|Apache Felix Gogo Runtime (0.10.0)
4|Active | 1|Apache Felix Gogo Shell (0.10.0)
5|Installed | 1|log4j.osgi (1.2.15.SNAPSHOT)
6|Installed | 1|Log4JProperties (0.0.0)
7|Installed | 1|Service listener example (1.0.0)
g! start 7
[2012-01-04 17:09:43,974]-INFO Gogo shell | Starting our bundle.
g! stop 7
[2012-01-04 17:09:49,151]-INFO Gogo shell | Stopping our bundle.
g!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值