OSGi使用日志服务

网络上很多例子,都不完整,这里整理一下.   

 

由于OSGi每个Bundle都有自己的独立的类加载器,所以如果将Log4j的配置放到一个Bundle中,是不能被其他Bundle共享的,而且也分散了对log的管理,显然不是太好。 比较理想的作法就是单独建立一个Log Bundle,负责处理日志问题,包括加载、关闭、配置等。由于我的项目中涉及到Jetty、Wicket、Hibernate、Spring等这些开源的项目分别使用了Jetty和Wicket使用的是slf4j,而Spring和Hibernate使用了Apache的commons-logging,所以只好使用slf4j作为通用的日志工具,使用Log4j的实现。 因为日志管理属于全局的,所以我将一些全局的东西都打包到一个Bundle中,方便管理。这个Bundle的主要目的就是加载Log4j的配置文件,并管理其生命周期。 首先要添加slf4j-api-1.4.2.jar和slf4j-log4j12-1.4.2.jar,用来让slf4j使用log4j的jar包,当然也不能忘了log4j自身的jar包。新建一个CoreActivator实现BundleActivator接口:

 

  1. package test;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import org.apache.log4j.LogManager;  
  6. import org.apache.log4j.PropertyConfigurator;  
  7. import org.osgi.framework.BundleActivator;  
  8. import org.osgi.framework.BundleContext;  
  9.   
  10. public class CoreActivator implements BundleActivator {  
  11.   
  12.     public void start(BundleContext context) throws Exception {  
  13.         Properties props = new Properties();  
  14.         props.load(CoreActivator.class.getResourceAsStream("log4j.properties"));   //log4j.properties 放到该类的同一目录,避免log4j自动识别,无法手动管理
  15.           
  16.         PropertyConfigurator.configure(props);  
  17.     }  
  18.   
  19.     public void stop(BundleContext context) throws Exception {  
  20.         LogManager.shutdown();  
  21.     }  
  22.   
  23. }  

log4j配置文件

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %l %x %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
#log4j.appender.LOGFILE=org.apache.log4j.FileAppender
#log4j.appender.LOGFILE.File=asf.log
#log4j.appender.LOGFILE.Append=true
#og4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
#log4j.logger.com.alisoft.aep.sip.analyzer=INFO,CONSOLE,LOGFILE

 

 

接下来是重点:

MANIFEST.MF:输出包org.slf4j 

 

其他插件需要用日志服务的,需要依赖日志Bundle即可。

然后:

  1. import org.slf4j.Logger;  
  2. import org.slf4j.LoggerFactory;  
  3.   
  4. public class TestClass{  
  5.     private static Logger log = LoggerFactory.getLogger("TestClass");  
  6.   
  7.     public void doThings(){  
  8.         log.info("doThings()");  
  9.     }  
  10. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值