簡體   English   中英

Apache Felix OSGi使用Logback進行日志記錄

[英]Apache Felix OSGi logging with Logback

我當前的應用程序使用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企業規范的“ 101 Log Service Specification ”一章。

LogService實現的基本功能會存儲記錄的條目一段時間,但不會將其寫入任何地方。 您可以通過LogReaderServiceLogListener查詢記錄的條目。

另一方面,您可以通過將slf4j-api和slf4j-logback添加到OSGi容器的方式來使用Logback。 這樣,通過slf4j-api類記錄的所有內容都將通過logback記錄。

如果我的猜測是正確的,則osgi-loglistener-slf4j捆綁包可以為您提供幫助。 該捆綁軟件不執行任何其他操作,但為環境中的每個LogReaderService注冊一個LogListener並將其捕獲的每個日志條目轉發到slf4j-api 該捆綁包在maven-central上可用

如果您不想使用slf4j API,則可以編寫一個類似的捆綁包,將日志從LogReaderService轉發到Logback 如果檢查我鏈接的包的來源,您將看到它僅用幾行代碼即可實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM