废话不多说了,log4j相信大家都用过的,现在公司有很多项目的研究都是基于OSGI的,所以我们的日志处理必须换到OSGI环境下去做了。于是相应的 问题也随之而来。其中最主要的问题就是一个classloader的问题。知道OSGI架构原理的都知道OSGI里面的各个Bundle是有独立的 ClassLoader来进行加载的。所以当我们把log4j的配置文件直接放在某个Bundle的classpath下面时是不能被整个OSGI环境识 别的。下面说我们的解决办法。核心思想参考了网上一篇文章(点击这里可以查看)。
1.建一个config-template.ini文件,当然位置和名字都可以由你自己安排,我这里是建在了我的OSGI环境(Eclipse或者 Equinox)的根目录下的configuration文件夹下了,这样这个配置文件就能在运行时被根加载器加载,而对所有的bundle可见,那么不仅仅是我们自定义的bundle可以用log4j,而且其他的第三方的bundle(如activemq,spring)都可以不用改动任何东西,就能使他们的log4j产生效果了,这也就是我们做这个事情的根本目的。这个文件夹下原来就有个config.ini(在部署的时候这个config.ini必须放在OSGI环境的confuguration文件夹下),是用于设置OSGI环境的启动的相关 属性的,我们可以复制它,然后稍作修改,内容如下:
代码
- osgi.splashPath = file:E:/eclipse-workspace/com.oval.dcts.ba.ui
- osgi.bundles=org.eclipse.equinox.common@2:start,
- org.eclipse.update.configurator@3:start, org.eclipse.core.runtime@start
- eclipse.product=org.eclipse.sdk.ide
- osgi.instance.area.default=@user.home/workspace
-
- eclipse.buildId=M20060921-0945
-
- log4j.configuration=file:/D:/Program Files/eclipse/configuration/log4j.properties
然后是准备我的log4j的properties文件,从上面可以看到,我需要把它放到D:/Program Files/eclipse/configuration,当然这个位置也是可以你自己定的(在linux我可以很好的设置相对位置来灵活处理,但是在windows下被反斜杠搞得郁闷,暂时还没有找到好办法,只能先设个绝对地址,到时候可能需要跟使用者约定一下。),而且这个位置在目前我们找的资料来看,用绝对地址相对简单一些,当然同时存在这一些部署上的不方便,我们暂时还没有找到合适的解决方案,不过以后会加以改进,先让我们的目的达到了再说。
log4j.properties 里的写法如下:
xml 代码
- ## ------------------------------------------------------------------------
- ## 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.
- ## ------------------------------------------------------------------------
-
- #
- # The logging properties used for eclipse testing, We want to see debug output on the console.
- #
-
- log4j.rootLogger=INFO, stdout, out
-
- # CONSOLE appender not used by default
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- #log4j.appender.stdout.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
- #log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
- log4j.appender.stdout.layout.ConversionPattern=%d %-5p %-5C:%L %x -> %m%n
-
- # File appender
- log4j.appender.out=org.apache.log4j.RollingFileAppender
- log4j.appender.out.file=./log/log4j.log
- log4j.appender.out.maxFileSize=1024KB
- log4j.appender.out.maxBackupIndex=5
- log4j.appender.out.append=true
- log4j.appender.out.layout=org.apache.log4j.PatternLayout
- log4j.appender.out.layout.ConversionPattern=%d %-5p %-5C:%L %x -> %m%n
-
-
- log4j.logger.org.springframework=INFO
- 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
分享到:
- 2007-07-13 18:26
- 浏览 10974
- 评论(7)
- 论坛回复 / 浏览 (7 / 12964)
- 查看更多
相关推荐
NULL 博文链接:https://ouyanggod.iteye.com/blog/1335233
log4j-2.17升级版本包,包括log4j-1.2-api-2.17.1.jar、log4j-api-2.17.1.jar、log4j-core-2.17.1.jar、log4j-slf4j-impl-2.17.1.jar、log4j-web-2.17.1.jar
OSGI环境搭建以及在搭建环境的基础上面附带一个小例子,让你熟悉osgi开发流程
pax-logging-log4j2 (log4J2) pax-logging-logback (logback) pax-logging-log4j1在 2.0.0 版本之前被命名为pax-logging-service 。 不同的日志 API 基本接口org.osgi.service.log.LogService从OSGi的纲要规范的第...
该压缩包中包含以下内容: 1、jcl-over-slf4j-1.7.21.jar 2、jcl-over-slf4j-1.7.21-sources.jar 3、jul-to-slf4j-1.7.21.jar 4、jul-to-slf4j-1.7.21-sources.jar 5、log4j-over-slf4j-1.7.21.jar 6、log4j-over-...
osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍
我转载的博客“OSGi环境下建立Web工程的Hello World ”中列出的jar,结合博客“OSGi环境下建立Web工程的Hello World ”开发应用。
osgi多个bundle之间读取同一配置文件,需要单独添加一个用来读取配置文件的bundle
Log4j 和 Slf4j 适配器将日志写入 scribe 服务器。 项目主页: : OSGi 环境:ddth-log2scribe 模块打包为 OSGi 包。 安装 最新发布版本: 0.1.0 。 请参阅 。 Maven 依赖: < groupId>...
基于OSGi框架之开发环境搭建基于OSGi框架之开发环境搭建
osgi数据库连接一章demo,c3p0配置oracle连接池和ibatis在osgi框架中的使用
OSGI环境搭建-使用JETTY-9.X创建WEB应用-注册Servlet-Jetty性能调优
NULL 博文链接:https://pay758.iteye.com/blog/1156470
5、log4j-over-slf4j-1.7.21.jar 6、log4j-over-slf4j-1.7.21-sources.jar 7、osgi-over-slf4j-1.7.21.jar 8、osgi-over-slf4j-1.7.21-sources.jar 9、slf4j-android-1.7.21.jar 10、slf4j-android-1.7.21-...
OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范OSGi R4规范
1.7.21-sources.jar 7、osgi-over-slf4j-1.7.21.jar 8、osgi-over-slf4j-1.7.21-sources.jar 9、slf4j-android-1.7.21.jar 10、slf4j-android-1.7.21-sources.jar 11、slf4j-api-1.7.21.jar 12、slf4j-api-1.7.21-...
这是来自cnblogs博主 静默虚空 的文章 ,为方便离线查看,转存为了pdf
这个sevlvlet是和上面传的jdbcbundle是配套的,这是一个关于OSGI的简单实例。
java-goinstant-auth.zip,Java应用程序的GoInstant身份验证。Java应用程序的GoInstant身份验证