java - 使用 Logback 进行 Apache Felix OSGi 日志记录

标签 java logging osgi apache-felix blueprint-osgi

我当前的应用程序使用 Logback用于记录。我使用 Apache Felix 部署了一个 OSGi 框架,该框架允许在运行时动态注册包。 Felix 设置如下:

    ...

    //System.out.println("Building OSGi Framework");
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Map<String, String> config = new HashMap<>();

    // specify the class loader
    config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);

    // specify the osgi cache directory
    config.put(Constants.FRAMEWORK_STORAGE, appConfig.getOsgiCacheDir());

    // make sure the cache is cleaned
    config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    // expose api
    config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "list.of.packages.to.export");

    // more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
    config.put("ds.showtrace", "false");
    config.put("ds.showerrors", "true");
    config.put("felix.fileinstall.dir", appConfig.getActiveOsgiExtDir());
    config.put("felix.fileinstall.tmpdir", appConfig.getTempOsgiExtDir());
    config.put("felix.fileinstall.bundles.startTransient","true");
    config.put("felix.fileinstall.poll","5000");
    config.put("felix.fileinstall.bundles.new.start", "true");

    LOG.debug("OSGi config: " + config.toString());

    framework = frameworkFactory.newFramework(config);

    try {
        framework.init();
        FrameworkEvent event;

        do {
            framework.start();
    ...

唯一的问题是 Felix 似乎没有日志记录。当一个包由于某种原因无法加载时,我不明白为什么!我知道我可以在包中使用以下内容来获取父记录器:
ServiceReference ref = bundleContext.getServiceReference(LogService.class.getName());
if (ref != null) {
    LogService log = (LogService) bundleContext.getService(ref);
    ...
   }

但是,我不明白如何让 felix 首先使用 logback 作为记录器。

最佳答案

我猜你用的是 Apache Felix Log在你的 OSGi 容器中有一个 LogService。 Apache Felix Log 实现了 OSGi Enterprise Specification 的“101 Log Service Specification”一章。

LogService 实现的基础将记录的条目存储一段时间,但不会将它们写入任何地方。您可以通过 LogReaderService 或 LogListener 查询记录的条目。

另一方面,您可能以将 slf4j-api 和 slf4j-logback 添加到 OSGi 容器的方式使用 Logback。通过这样做,通过 slf4j-api 类记录的所有内容都将由 logback 记录。

如果我的猜测是对的,osgi-loglistener-slf4j捆绑可以帮助你。这个包除了为环境中的每个 LogReaderService 注册一个 LogListener 并将它捕获的每个日志条目转发到 slf4j-api 之外什么都不做。该捆绑包可在 maven-central 上获得

如果您不想使用 slf4j API,您可以编写一个类似的包,将日志从 LogReaderService 转发到 Logback。如果您检查我链接的包的来源,您会发现它仅用几行代码就实现了。

关于java - 使用 Logback 进行 Apache Felix OSGi 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33165779/

相关文章:

java - 限制 java ssl 调试日志记录

osgi - 如何在 apache felix gogo shell 中自定义 OSGi 提示符

java - 按属性搜索 OSGI 服务

java - 这句话是什么意思

java - 具有可变数量 @XMLElements 的 JAXB

java - Gson如何将已知大小的列表转换为多个字段

html - 使用 Java API 将 .html 文件转换为 .mhtml

java - 正确初始化log4j系统

logging - 如何修复JSP编译器警告: one JAR was scanned for TLDs yet contained no TLDs?

maven - 在同一内容包中嵌入同一 OSGI 依赖项的多个版本