无法使 logback 与 karaf 3.0.4 一起使用 [英] Unable to make logback work with karaf 3.0.4

查看:50
本文介绍了无法使 logback 与 karaf 3.0.4 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 karaf 3.0.4 使用 logback.在代码中,我尝试以编程方式为 logback 记录器创建一个 FileAppender.

I am attempting to make karaf 3.0.4 use logback. In the code i attempt to programatically create a FileAppender for a logback logger.

ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("bingo");

这里的 LoggerFactor 是 SLF4J LoggerFactory.我还没有发布添加文件附加程序的代码.

Here the LoggerFactor is SLF4J LoggerFactory. I have not posted the code which adds the file appender.

这会在 karaf 中引发以下异常:

This throws the following exception in karaf:

Caused by: java.lang.ClassCastException: org.ops4j.pax.logging.slf4j.Slf4jLogger cannot be cast to ch.qos.logback.classic.Logger
at bingo.concurrent.TaskContextImpl.<init>(TaskContextImpl.java:34)
at bingo.concurrent.TaskExecutorImpl.execute(TaskExecutorImpl.java:83)

我已经浏览了已经报告的类似问题:如何使 Pax 日志记录使用 loggback 配置

I have gone through the already reported similar questions: How to make Pax logging to use the loggback config

我已经完成了以下操作:

I have already done the following:

  • 将 pax-logging-logback 作为 startup.properties 的一部分,以便它始终安装在 karaf 启动时.我使用的版本是 1.8.3.
  • 更改了配置文件org.ops4j.pax.logging.cfgorg.ops4j.pax.logging.logback.config.file=${karaf.home}/etc/logback.xml

这没有帮助.karaf.log 中没有捕获日志.我也尝试过手动安装 pax-logging-logback 但这也无济于事,我得到了相同的结果.

This did not help. There are no logs that gets captured in the karaf.log. I also tried manually installing pax-logging-logback but that did not help either and i has the same result.

我错过了什么吗?

问候,马达夫

推荐答案

我已经设法解决了这个问题.不幸的是,karaf 与 pax 日志服务捆绑在一起,它为 Slf4j 记录器包装了自己的实现,并始终委托给该实现.如果希望将 logback 用作默认日志记录实现,不仅用于 karaf 日志,而且用于在运行在 karaf 上的应用程序代码中定义自定义 logback appender,那么我们需要执行以下操作:

I have managed to resolve the issue. Unfortunately karaf comes bundled with pax logging service which wraps its own implementation for Slf4j logger and always delegates to that implementation. If one wishes to use logback as the default logging implementation not only for karaf logs but for defining custom logback appenders in the application code that runs on karaf then we need to do the following:

有两种方法可以使它工作 - 使用 osgi 引导委托或明确地将包添加到 etc/config.properties 中的 org.osgi.framework.system.packages.extra.我们选择了后者,因为 karaf 只是一个本地运行时,我们希望在其中快速测试东西,我们不想更改启动委托,因为这需要更改我们在单个包中导入/导出包的方式,这会影响不是 karaf 的高效运行时.

There are 2 ways to make it work - use osgi boot delegation or explicitly add packages to the org.osgi.framework.system.packages.extra in etc/config.properties. We went with the latter as karaf was simply a local runtime where we wished to quickly test stuff and we did not want to change the boot delegation as that would have required changing the way we import/export packages in the individual bundles which would have affected the productive runtime which was not karaf.

  • 需要删除以下捆绑包:pax-logging-service, lib/bin/karaf-client.jar
  • 向 lib 文件夹添加以下包(选择您的 karaf 运行时支持的版本)slf4j-api, logback-core, logback-classic, jul-to-slfj, ​​jcl-over-slf4j, osgi-over-slf4j
  • 在 etc/startup.properties 中 - 删除对 pax-logging-service 和 pax-logging-api 的引用
  • 将 etc/config.properties 中所需的包添加到 org.osgi.framework.system.packages.extra,确保没有 karaf 抱怨的多余空格
  • 创建一个 logback.xml 并将其添加到 $karaf.home 或 $karaf.home/etc
  • 注释掉 etc/org.ops4j.pax.logging.cfg 中的所有内容
  • 在 etc/system.properties 中添加 logback.configurationFile="logback.xml 文件的路径"

就是这样!!我们无法使 org.apache.karaf.log.core 和 org.apache.karaf.log.command 工作,并且由于我们没有使用任何 karaf 日志记录控制台命令,因此我们保持原样.

That is all!! We were not able to make org.apache.karaf.log.core and org.apache.karaf.log.command work and since we did not use any karaf logging console commands we left it as is.

干杯!!马达夫

这篇关于无法使 logback 与 karaf 3.0.4 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆