使用 SL4j 从 Eclipse OSGI 插件进行日志记录



我正在使用来自我的 eclipse (luna) osgi bundles 的 sl4j。一切都很正常,直到我升级到日食火星。我在某个地方弄坏了东西,我不知道我做错了什么。

经过几个小时的尝试,我决定写一个简单的osgi捆绑包来测试我认为我理解的mecanism。

我正在通过新的 osgi 向导并创建这个简单的捆绑包:

清单是:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Service
Bundle-SymbolicName: service
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: service.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.osgi.framework;version="1.3.0",
 org.slf4j
Require-Bundle: ch.qos.logback.classic,
 ch.qos.logback.core

激活器代码为:

public class Activator implements BundleActivator {
private Logger logger=LoggerFactory.getLogger(this.getClass());
public void start(BundleContext context) throws Exception {
    System.out.println("Hello World!!");
    logger.error("logging error {}", this);
}
public void stop(BundleContext context) throws Exception {
    System.out.println("Goodbye World!!");
}

}

我在激活器旁边放了一个 logback.xml 配置文件.java。

但我不断遇到众所周知的消息:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hello World!!

当我将捆绑包作为 OSGI 框架调试目标运行时。

我检查了那个网址,以前在stackexchange和其他网站上的帖子,但我看不出有什么问题。

我知道这一定是一个我看不到的简单故障。

谢谢你的帮助。

您不需要 Require-Bundle 进行 logback,因为您的 bundle 不直接依赖于 logback。在运行时,您必须确保安装了 logback 并且未安装 slf4j-api。

另请参阅这篇文章,了解如何在OSGi中配置登录。

@Christian:谢谢,您的回答引导我找到解决方案,即ch.qos.logback.slf4j 捆绑包似乎在 Eclipse Mars 默认发行版中缺失

将 Eclipse 配置为使用 Orbit http://download.eclipse.org/tools/orbit/downloads/drops/R20151221205849/P2 存储库(在"编辑目标"首选项中)后,可以添加缺少的捆绑包,我的测试现在成功了。

最新更新