Use bundle in Karaf to introduce the external log4j configuration file

Source: Internet
Author: User
Environment preparation:


1. Create a Config and logs folder under karaf_home

2. Copy mylog4j. properties to the config directory.


View log4j-1.2.17.jar/manifest. MF


Manifest-Version: 1.0Export-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.eventBuilt-By: cyTool: Bnd-0.0.357Bundle-Name: Apache Log4jCreated-By: Apache Maven Bundle PluginBundle-Vendor: Apache Software FoundationBuild-Jdk: 1.6.0_23Bundle-Version: 1.2.17Bnd-LastModified: 1336302107501Bundle-ManifestVersion: 2Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txtBundle-Description: Apache Log4j 1.2Bundle-DocURL: http://logging.apache.org/log4j/1.2Bundle-SymbolicName: log4jImport-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.helpersName: org.apache.log4jDynamicImport-Package: *Implementation-Vendor: "Apache Software Foundation"Implementation-Title: log4jImplementation-Version: 1.2.17


We can see that the log4j-1.2.17.jar is a bundle.

Hot log4j deployment

Start Karaf and copy the log4j-1.2.17.jar to the karaf_home/deploy directory

Enter the LIST command to check that the bundle has been successfully installed.


Mylog4j. Properties


### direct log messages to stdout ###log4j.appender.consoleout=org.apache.log4j.ConsoleAppenderlog4j.appender.consoleout.layout=org.apache.log4j.PatternLayoutlog4j.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.DailyRollingFileAppenderlog4j.appender.myfile.File=logs/myfile.loglog4j.appender.myfile.DatePattern='.'yyyy-MM-ddlog4j.appender.myfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.myfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c:%L - %m%nlog4j.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 log factory class */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. 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 );}}



Deploy log-test


You can see in the Karaf command line, as shown in:



You can see the log file named myfile. Log in karaf_home/logs.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.