[英]How do I attach the sources for a library bundle in OSGi/Equinox/Eclipse?
[英]How do LogService and LogReaderService in Equinox work along?
我正在開發一個OSGi Equinox軟件包,我想添加一些日志記錄,主要是重定向到OSGi控制台,僅用於調試目的。
在放棄使用log4j之后,由於Equinox(LogService和ExtendedLogService)中有幾個日志服務,我發現這篇文章描述了如何使用LogService:
所以我提出了一個看起來像這樣的Activator:
package org.example.servlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.eclipse.equinox.log.ExtendedLogService;
public class Activator implements BundleActivator {
private static BundleContext context;
private ServiceTracker logServiceTracker;
private LogService logService;
static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
// create a tracker and track the log service
logServiceTracker = new ServiceTracker(context, LogService.class.getName(), null);
logServiceTracker.open();
// grab the service
logService = (LogService) logServiceTracker.getService();
if(logService != null)
logService.log(LogService.LOG_INFO, "Yee ha, I'm logging!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
好吧,我從未在OSGi控制台中看到記錄的消息...尋找更多信息,我找到了這個帖子:
一些答案表明我應該實現一個實際偵聽日志事件的LogServiceReader對象(這只是我的猜測),將記錄的消息重定向到任何(文件,控制台等...)
現在我的問題,不僅僅是如何實現這個接口,我如何在我的LogServiceReader實現和Activator中使用的LogService之間進行綁定...
謝謝! 亞歷克斯
直接回答這個問題:
LogService是一種負責存儲日志消息的服務。 LogReaderService是一種服務,負責讀取這些日志消息並將它們分派給日志偵聽器。 這些之間的綁定是自動完成的。 您將自己做的是一方面將消息記錄到LogService,並可能將LogListeners綁定到LogReaderService,這將在某處記錄日志,例如另一方面的控制台。
要解決日志不出現的問題,您需要做一些額外的事情。
首先,你是否在osgi容器中安裝了一個提供LogService和LogReaderService實現的bundle?
你可以通過向你的Activator添加這樣的東西來檢查org.osgi.service.log.LogService的存在:
if(logService != null){
System.out.println("There is a LogService available");
logService.log(LogService.LOG_INFO, "Yee ha, I'm logging!");
}
else {
System.out.println("There is no LogService available");
}
或者只需在equinox控制台中鍵入“bundles”,然后查找提供org.osgi.service.log.LogService和org.osgi.service.log.LogReaderService的軟件包。
如果沒有可用的LogService,請安裝一個。 例如:
install http://oscar-osgi.sf.net/repo/log/log.jar
org.apache.log4j equinox依賴項也提供了這樣的服務。
再次啟動和停止自己的捆綁包。 它現在應該打印“有一個LogService可用”。
現在您的消息被記錄到LogService並由LogReaderService處理,但是,由於可能沒有向此服務注冊的LogListener(取決於啟動的其他bundle),
您可能需要在捆綁Activator中自己添加LogListener。
有關執行此操作的示例bundle Activator,請查看http://blog.kornr.net/index.php/2008/12/09/understanding-the-osgi-logging-service 。
好吧,以防其他人對這個問題感興趣。 我在Google Code中找到了一個非常好的wiki,解釋了OSGi日志系統如何工作,這是一個“必讀”:
由於我只想使用日志在控制台中打印,因此提出的解決方案對我有用。 唯一的問題是找到按預期工作的LogService的實現。 文章中指出的實際工作,但實現版本1.1,而目前的LogService規范是1.3版本 ,據我所知。 所以我做的是,從以前的實現中下載了源代碼,並根據我的口味和最新版本(1.3)規范調整了該實現。 你可以在這里找到資源。
顯然,您也可以使用您的或其他任何LogService接口的實現,由您決定。
此外,如果您計划使用后端現有的日志記錄服務,例如commons-logging或sl4j等...我推薦這些鏈接,它們提供了有關從何處開始的很好的信息:
這對我來說太過分了,因為我想要的只是一種在OSGi控制台中打印消息的方法,但不必使用log4j,片段等。
希望有人發現這很有用
問候,亞歷克斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.