2011-11-14 48 views
3

我想將插件的異常發送到錯誤日誌,但我不希望它們顯示在Eclipse控制檯中。這就是我正在做的。Eclipse PDE日誌記錄

首先,我實現了ILogListener。

public class MyILogListener implements ILogListener { 

    @Override 
    public void logging(IStatus status, String plugin) { 
     System.out.println("logging: " + plugin); 

    } 

} 

我有這個示例操作來做測試。

public void run(IAction action) { 

    ILogListener iL = new MyILogListener(); 
    Platform.addLogListener(iL); 

    Bundle bundle = Platform.getBundle(test.Activator.PLUGIN_ID); 
    ILog log = Platform.getLog(bundle); 
    Object o = null; 
    try { 
     o.equals("sk"); 
    } catch (Exception e) { 
     log.log(new Status(Status.ERROR, test.Activator.PLUGIN_ID, "test", e)); 


    } 

} 

該異常寫入Eclipse應用程序的錯誤日誌中,並顯示在控制檯中。

java.lang.NullPointerException 
at test.actions.SampleAction.run(SampleAction.java:52) 
at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251) 
at org.eclipse.ui.internal.WWinPluginAction.runWithEvent(WWinPluginAction.java:229) 
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584) 
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501) 
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3588) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3209) 
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696) 
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660) 
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494) 
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674) 
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667) 
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123) 
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:616) 
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622) 
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577) 
at org.eclipse.equinox.launcher.Main.run(Main.java:1410) 
at org.eclipse.equinox.launcher.Main.main(Main.java:1386) 

記錄:org.eclipse.core.runtime

你可以看到MyILogListener正在打印 「org.eclipse.core.runtime」,我認爲這將打印我的插件ID,這是「測試」。另外,在我的實現中,我沒有寫任何東西到日誌中,但是異常出現在錯誤日誌中。我只想在錯誤日誌中寫入異常,而不是在控制檯中顯示它。

我做錯了什麼?我在Eclipse 3.7.0和3.7.1上嘗試這個。

回答

2

根據Eclipse 3.7 documentation

所有的Eclipse日誌記錄API現在通過org.eclipse.equinox.log.Logger聚集名爲org.eclipse.equinox.logger。所有ILogListener實例都會收到通過這個記錄器記錄的條目的通知。

這意味着,通過登錄到一個包的日誌聽衆的一堆會被通報,其中之一是Eclipse的StatusManager其中記錄了狀態錯誤日誌。

如果你只想登錄到Eclipse錯誤日誌(和錯誤日誌視圖),那麼你應該直接調用StatusManager.handle()方法之一。