karaf中利用Bundle引入外部log4j配置文件
Posted gccbuaa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了karaf中利用Bundle引入外部log4j配置文件相关的知识,希望对你有一定的参考价值。
环境准备:
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配置文件的主要内容,如果未能解决你的问题,请参考以下文章