自定义Appender在Karaf Decanter不工作

时间:2016-03-27 16:35:21

标签: logging log4j apache-karaf apache-servicemix

我正在尝试使用Decanter创建自定义appender,如上所述here。我的目标是监听karaf捆绑包输出的所有日志并将其显示在控制台中。我在Apache Servicemix 6.1.0下使用Karaf 3.0.4

我在Karaf& amp;添加了Decanter功能库。然后按照Decanter文档中的说明安装了collector-log和appender-log。

我的自定义appender如下所示: -

Activator

我的package com.jabong.karaf.decanter.appender.elklog; import java.util.Dictionary; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.event.EventConstants; import org.osgi.service.event.EventHandler; public class Activator implements BundleActivator { private ServiceRegistration registration; @Override public void start(BundleContext bundleContext) { ELKLogAppender appender = new ELKLogAppender(); Dictionary<String, String> properties = new Hashtable<>(); properties.put(EventConstants.EVENT_TOPIC, "decanter/collect/*"); registration = bundleContext.registerService(EventHandler.class, appender, properties); } @Override public void stop(BundleContext bundleContext) { if (registration != null) { registration.unregister(); } } } 如下所示: -

<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.jabong.karaf.decanter.appender</groupId>
    <artifactId>elklog</artifactId>
    <version>0.0.1</version>
    <packaging>bundle</packaging>
    <name>Apache Karaf :: Decanter :: Appender :: ELKLog</name>
    <dependencies>
        <!-- OSGi -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>4.3.1</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.4.0</version>
                <inherited>true</inherited>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.jabong.karaf.decanter.appender.elklog.Activator</Bundle-Activator>
                        <Import-Package>
                            *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的pom.xml如下所示: -

deploy

我可以将它放在332 | Active | 80 | 0.0.1 | basecomponent 333 | Active | 80 | 1.0.1 | Apache Karaf :: Decanter :: API 334 | Active | 80 | 1.0.1 | Apache Karaf :: Decanter :: Collector :: Log 337 | Active | 80 | 1.0.1 | Apache Karaf :: Decanter :: Appender :: Log 339 | Active | 80 | 0.0.1 | Apache Karaf :: Decanter :: Appender :: ELKLog 340 | Active | 80 | 2.4.0 | Commons IO 341 | Active | 80 | 1.0.1 | Apache Karaf :: Decanter :: Collector :: File 文件夹中,在Karaf中正确安装捆绑包。

org.ops4j.pax.logging.cfg

我在卡拉夫的# Root logger log4j.rootLogger=INFO, out, osgi:VmLogAppender log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer # To avoid flooding the log when using DEBUG level on an ssh connection and doing log:tail log4j.logger.org.apache.sshd.server.channel.ChannelSession = INFO # 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=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %X | %m%n # File appender log4j.appender.out=org.apache.log4j.RollingFileAppender log4j.appender.out.layout=org.apache.log4j.PatternLayout log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %X | %m%n log4j.appender.out.file=${karaf.data}/log/servicemix.log log4j.appender.out.append=true log4j.appender.out.maxFileSize=100MB log4j.appender.out.maxBackupIndex=10 # Sift appender log4j.appender.sift=org.apache.log4j.sift.MDCSiftingAppender log4j.appender.sift.key=bundle.name log4j.appender.sift.default=servicemix log4j.appender.sift.appender=org.apache.log4j.FileAppender log4j.appender.sift.appender.layout=org.apache.log4j.PatternLayout log4j.appender.sift.appender.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X | %m%n log4j.appender.sift.appender.file=${karaf.data}/log/$\\{bundle.name\\}.log log4j.appender.sift.appender.append=true 如下所示: -

data/servicemix.log

但我在控制台上没有看到任何消息。即使我在subs(replacement, simultaneous=True)

中看到很多日志

1 个答案:

答案 0 :(得分:1)

在etc / org.ops4j.pax.logging.cfg中,更改为

log4j.rootLogger=INFO, out, osgi:* 

log4j.rootLogger=INFO, out, osgi:VmLogAppender 

解决了这个问题。