我的当前应用程序使用Logback进行日志记录。我使用Apache部署了一个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作为记录器。
发布于 2015-10-16 09:29:43
我猜您使用Apache Felix日志在您的OSGi容器中有一个LogService。Apache实现了OSGi企业规范的"101日志服务规范“一章。
LogService实现的基础是将记录的条目存储一段时间,但不会将它们写入任何地方。您可以通过LogReaderService或LogListener查询记录的条目。
另一方面,您可能以将slf4j-api和slf4j-logback添加到OSGi容器的方式使用Logback。这样,通过slf4j-api类记录的所有内容都将由logback记录。
如果我的猜测是正确的,那么osgi-loglistener-slf4j包可以帮助您。这个包只为环境中的每个LogListener注册一个LogReaderService,并将它捕获的每个日志条目转发给slf4j-api。该包可在maven-中环上使用。
如果您不想使用slf4j API,您可以编写一个类似的包,用于从LogReaderService到Logback转发日志。如果您检查我链接的包的源代码,您将看到它是用几行代码实现的。
https://stackoverflow.com/questions/33165779
复制相似问题