OSGi/Karaf 没有找到 GelfAppender

OSGi/Karaf doesn't find GelfAppender

我尝试使用带有 Gelf Appender 的 Apache Karaf 来连接到 Log4J,但我只是得到 ClassNotFoundException。

我遵循了 Karaf Docs 部分 "Using your own appenders" 中的说明:

我添加了一个文件夹 system/org/graylog2/gelfj/1.1.12/ 并将 gelfj-1.1.12.jar 从 Maven 存储库复制到该文件夹​​中。我将此行添加到 etc/startup.properties:

org/graylog2/gelfj/1.1.12/org.graylog2.gelfj-1.1.12.jar=7
org/ops4j/pax/logging/pax-logging-api/1.7.0/pax-logging-api-1.7.0.jar=8

(第二行已经有了,我按照说明"add the new fragment bundle to the list before the pax-logging-service bundle"。)

我也添加到文件 etc/org.ops4j.pax.logging.cfg

# Root logger
log4j.rootLogger=INFO,out,graylog2,osgi:*
...
log4j.appender.graylog2=org.graylog2.log.GelfAppender
log4j.appender.graylog2.graylogHost=foo.bar.de
log4j.appender.graylog2.facility=myall
log4j.appender.graylog2.Threshold=INFO

如果我启动 karaf,我只会得到这个异常:

/opt/apache-karaf-2.3.11/bin/karaf server
karaf: JAVA_HOME not set; results may vary
log4j:ERROR Could not instantiate class [org.graylog2.log.GelfAppender].
java.lang.ClassNotFoundException: org.graylog2.log.GelfAppender not found by org.ops4j.pax.logging.pax-logging-service [4]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
    at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:72)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    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:97)
    at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
    at org.apache.log4j.PropertyConfigurator.parseCatsAndRenderers(PropertyConfigurator.java:639)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:504)
    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.PaxLoggingServiceImplManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:362)
    at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:148)
    at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:81)
    at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1436)
    at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1392)
    at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:103)
    at java.lang.Thread.run(Thread.java:701)
log4j:ERROR Could not instantiate appender named "graylog2".

我也试过这个 instructions 结果相同。

感谢您的帮助。

取自最新的 gelfj-1.1.12.jar,我可以看出它已经是一个片段包,它将附加到 pax-logging-service 包。快速安装和刷新 pax-logging-service 捆绑包可以成功安装它。 从上面添加您的配置会导致主机未找到失败。

因此,由于 gelf 包已经是片段包,所以只需通过

安装它
install mvn:org.graylog2/gelfj/1.1.12

还有一个

refresh 2

或者只是重新启动容器。