您的位置:首页 >> 编程开发 >> Java >> Java基础 >> Eclipse >> 正文
RSS
 

[引用] 为 Eclipse 插件添加日志框架

http://www.rdxx.com 05年07月27日 22:28 CSDN 我要投稿

关键词: 引用 , 日志 , 插件 , 框架 , Eclipse , IP

LoggingPlugin 类维护了 PluginLogManagers 的一个列表。这是必需的,这样,在插件停止时,就可以关闭该插件的所有层次结构,并正确删除 appender 和记录器,如清单 5 所示。

清单 5. LoggingPlugin 类处理日志管理器
private ArrayList logManagers = new ArrayList(); public void stop(BundleContext context) throws Exception {   synchronized (this.logManagers) {      Iterator it = this.logManagers.iterator();      while (it.hasNext()) {         PluginLogManager logManager = (PluginLogManager) it.next();         logManager.internalShutdown();       }     this.logManagers.clear();    }   super.stop(context);}void addLogManager(PluginLogManager logManager) {   synchronized (this.logManagers) {      if (logManager != null)         this.logManagers.add(logManager);    }}	void removeLogManager(PluginLogManager logManager) {   synchronized (this.logManagers) {      if (logManager != null)         this.logManagers.remove(logManager);    }}

插入 PluginLogManager 类的内容有很多。有时您所从属的插件,特别是那些从属于 workbench 的插件,可能引发异常。这些异常通常都会被 Eclipse 记录到日志中。允许将从属插件(dependent plug-in)插入日志框架中,这非常有用。在触发异常时,Eclipse 要记录的所有日志都会被放入日志框架,它与其他记录器共享配置文件。这种方法非常有用,因为这样可以将所有的内容都集中在一个位置上,并可以保留一个事实的历史样本,从而有助于修正应用程序的问题。

这可以通过实现 org.eclipse.core.runtime.ILogListener 并将其添加到从属插件的 ILog 实例中实现。基本上,您只需要将其与 Eclipse 的日志相关联。然后,这种实现就可以将所有的请求都重定向到一个使用您选择的名字(通常是一个插件标识符)创建的记录器中。然后您可以通过相同的配置文件对输出结果进行配置;只需指定记录器的名字、设置过滤条件、添加 appender 即可。该类如清单 6 所示:

清单 6. PluginLogListener 类
class PluginLogListener implements ILogListener {   private ILog log;   private Logger logger;   PluginLogListener(ILog log,Logger logger) {      this.log = log;      this.logger = logger;      log.addLogListener(this);   }   void dispose() {      if (this.log != null) {         this.log.removeLogListener(this);         this.log = null;         this.logger = null;      }    }   public void logging(IStatus status, String plugin) {      if (null == this.logger || null == status)          return;	      int severity = status.getSeverity();      Level level = Level.DEBUG;        if (severity == Status.ERROR)         level = Level.ERROR;      else      if (severity == Status.WARNING)         level = Level.WARN;      else      if (severity == Status.INFO)         level = Level.INFO;      else      if (severity == Status.CANCEL)         level = Level.FATAL;      plugin = formatText(plugin);      String statusPlugin = formatText(status.getPlugin());      String statusMessage = formatText(status.getMessage());      StringBuffer message = new StringBuffer();      if (plugin != null) {         message.append(plugin);         message.append(" - ");      }          if (statusPlugin != null &&            (plugin == null || !statusPlugin.equals(plugin))) {         message.append(statusPlugin);         message.append(" - ");      }	      message.append(status.getCode());      if (statusMessage != null) {         message.append(" - ");         message.append(statusMessage);      } 		      this.logger.log(level,message.toString(),status.getException());	   }      static private String formatText(String text) {      if (text != null) {         text = text.trim();         if (text.length() == 0) return null;      }       return text;   }}
共6页  1 2 3 4 5 6

 
 
标签: 引用 , 日志 , 插件 , 框架 , Eclipse , IP 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站