Osgi可以使用的日志有好几种方式log4j 、pax logging、logService、slf4j+logback最后研究下来slf4j+logback比较好用,下面演示该方式的配置。
参考文档
http://hbkh2000.iteye.com/blog/1028307
1.必须jar包
首先我们需要下载需要的一下jar包
SLF4J下载地址:http://www.slf4j.org/download.html
Logback下载地址:http://logback.qos.ch/download.html
下载后解压后有很多jar包,我们需要其中三个即可
logback-core-1.0.0.jar
slf4j-api-1.6.4.jar
1.将jar导入到bundle中
需要日志的bundle都需要导入,此示例中我们在impl和client中都用到了日志,所以都需要导入,client导入后如图3.日志代码编写
public class PropertiesManagement implements ManagedService {
private static final Logger logger = (Logger) LoggerFactory
.getLogger(PropertiesManagement.class.getName());
@Override
public void updated(Dictionary dict) throws ConfigurationException {
if(dict!=null){
logger.info("read configuration properties......");
PropertyConstants.NAME = (String)dict.get("name");
PropertyConstants.BLOOD = Integer.parseInt((String)dict.get("blood"));
PropertyConstants.MANA = Integer.parseInt((String)dict.get("mana"));
}else {
logger.info("read configuration properties fail!");
}
}
}
4.日志配置文件
由于多个bunddle都要用到日志,所以该日志文件是需要放在一个公共的地方,所有的bunddle都可以访问到。我们放到一个指定的目录,E:\configuration\ logbackConfiguration.xml,该文件的内容如下,
具体配置可以参加文档http://aub.iteye.com/blog/1103685
5. 启动配置
在此我们是以控制台的方式启动,所以我们需要指定在启动的时候当前workspace能够加载到第四步所建立的日志配置文件,具体配置如下,在arguments的vm arguments加入配置文件的路径
-Dlogback.configurationFile= E:\configuration\ logbackConfiguration.xml启动后控制台和文件里都会输出日志
我配置的日志输出位置为<property name="File_Pattern"value="./logs/%d{yyyyMMdd}/" />
在我的eclipse-rcp的根目录下会输出如下日志文件