使用log4j json-event-layout与在servicemix下运行的Karaf 3.0.5

时间:2016-02-27 17:33:40

标签: log4j apache-karaf karaf apache-servicemix

我正在尝试使用JSONEventLayoutV1作为karaf日志记录的模式布局,以json格式记录所有事件。

我遵循的步骤。

  1. 修改了pom.xml jsonevent-layout文件,如下所示: -

    <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.7</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name>
            <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
            <Import-Package>*;resolution:=optional</Import-Package>
            <Export-Package>*</Export-Package>
            <Fragment-Host>org.ops4j.pax.logging.pax-logging-service;bundle-version="[1.6,1.7)"</Fragment-Host>
            <Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
            <Implementation-Version>${project.version}</Implementation-Version>
        </instructions>
    </configuration>
    

    </plugin>

  2. 使用mvn clean install -Pbundle

  3. 构建json格式
  4. 将创建的包复制到系统目录

    mkdir -p ${karaf.home}/system/net/logstash/log4j/jsonevent-layout/1.8-SNAPSHOT/    
    cp target/jsonevent-layout-1.8-SNAPSHOT.jar ${karaf.home}/system/net/logstash/log4j/jsonevent-layout/1.8-SNAPSHOT/
    
  5. 在Pax Logging之前将此行添加到etc/startup.properties net/logstash/log4j/jsonevent-layout/1.8-SNAPSHOT/jsonevent-layout-1.8-SNAPSHOT.jar=3 mvn\:org.ops4j.pax.url/pax-url-aether/2.4.1 = 5 mvn\:org.ops4j.pax.url/pax-url-wrap/2.4.1/jar/uber = 5 mvn\:org.ops4j.pax.logging/pax-logging-api/1.8.4 = 8 mvn\:org.ops4j.pax.logging/pax-logging-service/1.8.4 = 8
  6. 更新org.ops4j.pax.logging.cfg如下: - log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=net.logstash.log4j.JSONEventLayoutV1 log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n
  7. 但每当我开始使用Karaf时,我都会遇到以下异常: -

    jabong@jabong1143:~/Downloads/software/dev/apache-servicemix-6.1.0$ sudo bin/servicemix
    Please wait while Apache ServiceMix is starting...
     21% [===============>                                                        ]log4j:ERROR Could not instantiate class [net.logstash.log4j.JSONEventLayoutV1].
    java.lang.ClassNotFoundException: net.logstash.log4j.JSONEventLayoutV1 not found by org.ops4j.pax.logging.pax-logging-service [5]
        at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
        at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
        at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.apache.log4j.helpers.Loader.loadClass(Loader.java:198)
        at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:326)
        at org.apache.log4j.helpers.OptionConverter.instantiateByKey(OptionConverter.java:123)
        at org.apache.log4j.PaxLoggingConfigurator.parseAppender(PaxLoggingConfigurator.java:129)
        at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
        at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
        at org.apache.log4j.PaxLoggingConfigurator.doConfigure(PaxLoggingConfigurator.java:72)
        at org.ops4j.pax.logging.service.internal.PaxLoggingServiceImpl.updated(PaxLoggingServiceImpl.java:214)
        at org.ops4j.pax.logging.service.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:362)
        at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)
        at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)
        at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)
        at org.apache.felix.cm.impl.ConfigurationManager$UpdateConfiguration.run(ConfigurationManager.java:1747)
        at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:103)
        at java.lang.Thread.run(Thread.java:745)
    log4j:ERROR No layout set for the appender named [out].
    100% [========================================================================]
    

    有人可以让我知道我做错了什么吗?

    Karaf User Forum中也讨论了这个问题。但仍然没有运气。

1 个答案:

答案 0 :(得分:1)

对于普通的log4j,fragement方法是正确的。不确定它是否也适用于pax-logging。一种有效的方法是提供pax logging appender服务并将上面的appender包装在其中。请参阅this code from karaf decanter作为示例。

根据您想要实现的目标,简单地使用apache karaf decanter可能更容易。它可以捕获日志消息jmx和系统信息,并将它们推送到弹性搜索中。