环境准备:

1.在karaf_home下新建 config及logs目录

2.将mylog4j.properties拷贝到config文件夹下

查看log4j-1.2.17.jar/MANIFEST.MF

Manifest-Version: 1.0
Export-Package: org.apache.log4j.net;uses:="org.apache.log4j,org.apach
e.log4j.spi,javax.naming,org.apache.log4j.helpers,javax.jms,org.apach
e.log4j.xml,javax.mail,javax.mail.internet,org.w3c.dom,javax.jmdns";v
ersion="1.2.17",org.apache.log4j.jmx;uses:="org.apache.log4j,javax.ma
nagement,org.apache.log4j.helpers,org.apache.log4j.spi";version="1.2.
17",org.apache.log4j.jdbc;uses:="org.apache.log4j,org.apache.log4j.sp
i";version="1.2.17",org.apache.log4j.config;uses:="org.apache.log4j.h
elpers,org.apache.log4j,org.apache.log4j.spi";version="1.2.17",org.ap
ache.log4j.helpers;uses:="org.apache.log4j,org.apache.log4j.spi,org.a
pache.log4j.pattern";version="1.2.17",org.apache.log4j;uses:="org.apa
che.log4j.spi,org.apache.log4j.helpers,org.apache.log4j.pattern,org.a
pache.log4j.or,org.apache.log4j.config";version="1.2.17",org.apache.l
og4j.or.jms;uses:="org.apache.log4j.helpers,javax.jms,org.apache.log4
j.or";version="1.2.17",org.apache.log4j.nt;uses:="org.apache.log4j.he
lpers,org.apache.log4j,org.apache.log4j.spi";version="1.2.17",org.apa
che.log4j.or.sax;uses:="org.apache.log4j.or,org.xml.sax";version="1.2
.17",org.apache.log4j.pattern;uses:="org.apache.log4j.helpers,org.apa
che.log4j.spi,org.apache.log4j,org.apache.log4j.or";version="1.2.17",
org.apache.log4j.spi;uses:="org.apache.log4j,org.apache.log4j.helpers
,org.apache.log4j.or";version="1.2.17",org.apache.log4j.rewrite;uses:
="org.apache.log4j,org.apache.log4j.spi,org.apache.log4j.helpers,org.
apache.log4j.xml,org.w3c.dom";version="1.2.17",org.apache.log4j.or;us
es:="org.apache.log4j.helpers,org.apache.log4j.spi,org.apache.log4j";
version="1.2.17",org.apache.log4j.xml;uses:="javax.xml.parsers,org.w3
c.dom,org.xml.sax,org.apache.log4j.config,org.apache.log4j.helpers,or
g.apache.log4j,org.apache.log4j.spi,org.apache.log4j.or";version="1.2
.17",org.apache.log4j.varia;uses:="org.apache.log4j.spi,org.apache.lo
g4j,org.apache.log4j.helpers";version="1.2.17"
Ignore-Package: com.sun.jdmk.comm,javax.swing.text,javax.swing.border,
javax.swing.tree,javax.swing,com.ibm.uvm.tools,javax.swing.table,java
x.swing.event
Built-By: cy
Tool: Bnd-0.0.357
Bundle-Name: Apache Log4j
Created-By: Apache Maven Bundle Plugin
Bundle-Vendor: Apache Software Foundation
Build-Jdk: 1.6.0_23
Bundle-Version: 1.2.17
Bnd-LastModified: 1336302107501
Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Log4j 1.2
Bundle-DocURL: http://logging.apache.org/log4j/1.2
Bundle-SymbolicName: log4j
Import-Package: javax.jmdns;resolution:=optional,javax.jms;resolution:
=optional,javax.mail;resolution:=optional,javax.mail.internet;resolut
ion:=optional,javax.management,javax.naming,javax.xml.parsers,org.w3c
.dom,org.xml.sax,org.xml.sax.helpers Name: org.apache.log4j
DynamicImport-Package: *
Implementation-Vendor: "Apache Software Foundation"
Implementation-Title: log4j
Implementation-Version: 1.2.17

我们能够看到 log4j-1.2.17.jar 就是一个Bundle

热部署 log4j


启动karaf,将 log4j-1.2.17.jar 拷贝到karaf_home/deploy文件夹下

输入 list 命令。能够查看Bundle已经成功安装

mylog4j.properties

### direct log messages to stdout ###
log4j.appender.consoleout=org.apache.log4j.ConsoleAppender
log4j.appender.consoleout.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c:%L - %m%n ### direct messages to file myfile.log ###
log4j.appender.myfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myfile.File=logs/myfile.log
log4j.appender.myfile.DatePattern='.'yyyy-MM-dd
log4j.appender.myfile.layout=org.apache.log4j.PatternLayout
log4j.appender.myfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c:%L - %m%n log4j.rootLogger==info,consoleout,myfile

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.demo.hello.provider</groupId>
<artifactId>log-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging> <name>log-test</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>
org.apache.log4j,
org.osgi.framework.*
</Import-Package>
<Bundle-Activator>com.demo.hello.provider.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

Activitor

/**
*
* @author wumingkun
* @version 1.0.0
* @Description
*/ package com.demo.hello.provider; import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import com.demo.hello.provider.log.LoggerFactory; /**
* @author wumingkun
*
*/
public class Activator implements BundleActivator { /* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
Logger logger =LoggerFactory.getLogger(Activator.class);
logger.info("bundle start...");
} /* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception { } }

LoggerFactory:

/**
*
* @author wumingkun
* @version 1.0.0
* @Description 日志工厂类
*/ package com.demo.hello.provider.log; import java.io.File; import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator; /**
* @author wumingkun
*
*/
public class LoggerFactory {
public static final String CONFIG_DIR = "config";
public static final String LOG4J_CONFIG = "myLog4j.properties";
private static String fileName = getFileName(); public static Logger getLogger(Class clazz) {
Logger logger = Logger.getLogger(clazz);
loadProperty();
return logger;
} private static String getFileName() {
return System.getProperty("user.dir") + File.separator + CONFIG_DIR
+ File.separator + LOG4J_CONFIG;
} public static void loadProperty() {
PropertyConfigurator.configure(fileName);
}
}

部署log-test

在karaf命令行能够看到,例如以下图所看到的:

在karaf_home/logs能够看到生成名为 myfile.log的日志文件

karaf中利用Bundle引入外部log4j配置文件的更多相关文章

  1. 在spring配置文件中引入外部properties配置文件 context:property-placeholder

    在spring的配置文件中,有时我们需要注入很多属性值,这些属性全都写在spring的配置文件中的话,后期管理起来会非常麻烦.所以我们可以把某一类的属性抽取到一个外部配置文件中,使用时通用spring ...

  2. Spring Boot 引入外部yml配置文件

    当需要在springboot中引用其他的yml文件时,需要在application.yml里配置 spring:     profiles:         include: email,xmyb   ...

  3. ASP.NET MVC中在 @RenderBody() 或者 @Html.Partial()中需要使用引入外部js,css

    今天想在后台封装一下bootstraptree这个插件,引入jquery.js bootstrap.js bootstrap.css bootstrap-tree.js后,我在页面查看脚本错误就连最简 ...

  4. JAVA MyBatis配置文件用properties引入外部配置文件

    方式一:通过properties 元素的子元素来传递数据 例如: <properties> <property name="driver" value=" ...

  5. 如何在Android应用中引入外部网页

    在某些情况下,我们需要在Android应用中引入外部网页,这里记录一下如何操作(其实很简单^.^). 先介绍一下开发环境: 开发工具:Android Studio 1.5 SDK API版本:17 操 ...

  6. SpringBoot常用配置,引入外部配置文件信息,热加载

    SpringBoot的配置文件格式 yml规范 SpringBoot的配置文件支持properties和yml,甚至还支持json. 更推荐使用yml文件格式: yml文件,会根据换行和缩进帮助咱们管 ...

  7. spring 配置文件 引入外部的property文件的两种方法

    spring  的配置文件 引入外部的property文件的两种方法 <!-- 引入jdbc配置文件    方法一 --> <bean id="propertyConfig ...

  8. vue文件中引入外部js

    1.在项目的入口文件中(app.js)定义remoteScript标签 Vue.component('remote-script', { render: function (createElement ...

  9. jsp、css中引入外部资源相对路径的问题

    在jsp页面中添加base,可用相对路径: <% String path = request.getContextPath(); String basePath = request.getSch ...

随机推荐

  1. Java-创建一个线程

    第一种继承Thread类 package com.tj; public class BasicThread1 extends Thread { public void run() { System.o ...

  2. Leetcode207--->课程表(逆拓扑排序)

    题目: 课程表,有n个课程,[0, n-1]:在修一个课程前,有可能要修前导课程: 举例: 2, [[1,0]] 修课程1前需要先修课程0 There are a total of 2 courses ...

  3. MySQL常见数据库引擎及比较?

    一:MySQL存储引擎简介 MySQL有多种存储引擎,每种存储引擎有各自的优缺点,大家可以择优选择使用:MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(BerkeleyDB) ...

  4. [error:没有解析库]Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?

    将代码拷贝到服务器上运行,发生错误提示需要新安装parser library. 查看代码中发现有以下内容: soup = BeautifulSoup(open(fp), 'xml') 安装解析库即可: ...

  5. 在Asp.net MVC中添加一个全局的异常处理的过滤器及Log4Net的使用

    1:捕获异常新建一个异常处理的类MyExceptionAttribute捕获异常信息. //写到日志中.多个线程同时操作一个文件,造成文件的并发,这时用队列 public static Queue&l ...

  6. 再谈 Go 语言在前端的应用前景

    12 月 23 日,七牛云 CEO & ECUG 社区发起人许式伟先生在 ECUG Con 2018 现场为大家带来了主题为<再谈 Go 语言在前端的应用前景>的内容分享. 本文是 ...

  7. CentOS 7.4升级Linux内核

    CentOS 7.4升级Linux内核 [日期:2018-01-15] 来源:Linux社区  作者:straycats [字体:大 中 小] 由于最近intel出了Meltdown和Spectre两 ...

  8. bzoj 3456 城市规划 无向简单连通图个数 多项式求逆

    题目大意 求n个点的无向简单连通图个数 做法1 \(f[i]\)表示i个点的无向简单连通图个数 \(g[i]=2^{\frac {i*(i-1)}{2}}\)表示i个点的无向简单图个数(不要求连通) ...

  9. TroubleShoot: SharePoint 2013: ExecuteOrDelayUntilScriptLoaded 页面发布后不执行的问题

    SharePoint 2010 中的ExecuteOrDelayUntilScriptLoaded,在2013 中使用时没有效果的问题. Example: SharePoint 2013 Code: ...

  10. Change visual studio 2015 enterprise installation path(转)

    I would like to install VS2015 in a drive different than C:. The problem is that when I run the inst ...