简体   繁体   English

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

[英]Apache Felix OSGi logging with Logback

My current application uses Logback for logging. 我当前的应用程序使用Logback进行日志记录。 I have used Apache Felix to deploy an OSGi framework that allows bundles to be dynamically registered at runtime. 我已经使用Apache Felix部署了OSGi框架,该框架允许在运行时动态注册包。 The Felix setup is as follows: 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();
    ...

The only problem is that there seems to be no logging with Felix. 唯一的问题是,Felix似乎没有日志记录。 When a bundle fails to load for some reason I can't see why! 当由于某种原因无法加载捆绑软件时,我看不到原因! I know that I can use the following in the bundle to obtain the parent logger: 我知道我可以在捆绑软件中使用以下内容来获取父记录器:

ServiceReference ref = bundleContext.getServiceReference(LogService.class.getName());
if (ref != null) {
    LogService log = (LogService) bundleContext.getService(ref);
    ...
   }

However, I don't understand how I can get felix to use logback as the logger in the first place. 但是,我不明白如何才能让felix首先使用logback作为记录器。

I guess you use Apache Felix Log to have a LogService in you OSGi container. 我猜您使用Apache Felix Log在OSGi容器中具有LogService。 Apache Felix Log implements the chapter " 101 Log Service Specification " of OSGi Enterprise Specification. Apache Felix Log实现了OSGi企业规范的“ 101 Log Service Specification ”一章。

The basic of LogService implementation stores the logged entries for a while but does not write them to anywhere. LogService实现的基本功能会存储记录的条目一段时间,但不会将其写入任何地方。 You can query the recorded entries via LogReaderService or LogListener . 您可以通过LogReaderServiceLogListener查询记录的条目。

On the other hand, you might use Logback in the way that you added slf4j-api and slf4j-logback to your OSGi container. 另一方面,您可以通过将slf4j-api和slf4j-logback添加到OSGi容器的方式来使用Logback。 By doing that, everything that is logged via slf4j-api classes will be logged by logback. 这样,通过slf4j-api类记录的所有内容都将通过logback记录。

If my guesses were right, the osgi-loglistener-slf4j bundle could help you. 如果我的猜测是正确的,则osgi-loglistener-slf4j捆绑包可以为您提供帮助。 This bundle does nothing else but registers a LogListener for every LogReaderService in the environment and forwards every log entries it catches to slf4j-api . 该捆绑软件不执行任何其他操作,但为环境中的每个LogReaderService注册一个LogListener并将其捕获的每个日志条目转发到slf4j-api The bundle is available on maven-central 该捆绑包在maven-central上可用

If you do not want to use slf4j API, you can write a similar bundle that does the log forwarding from LogReaderService to Logback . 如果您不想使用slf4j API,则可以编写一个类似的捆绑包,将日志从LogReaderService转发到Logback If you check the source of the bundle I linked, you will see that it is implemented with only a few lines of code. 如果检查我链接的包的来源,您将看到它仅用几行代码即可实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM