惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
A
About on SuperTechFans
Vercel News
Vercel News
博客园 - 【当耐特】
爱范儿
爱范儿
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
D
Docker
博客园 - 叶小钗
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
I
InfoQ
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - Allen Young

如何使用Eclipse中对Web Browser的支持 从流氓说开去 小试org.eclipse.jface.dialogs.TitleAreaDialog 如何在SWT中使鼠标的状态变成忙碌 如何在自己编写的Plugin中使用第三方jar 如何在程序中设置Hibernate Configuration的Property 在线IM工具,我们能够信任你吗? Comet: Low Latency Data for the Browser 顾城 - 我是一个任性的孩子 Links for 2006/8/14 在Liferay Portal中使用DWR Links for 2006/8/9 What's new in Spring 2.0? 也谈“轮子理论” 后续 也谈“轮子理论” 道德的起源 What's New in Eclipse 3.2 Java Development Tools Ten things programmers might want to know about marketers Callisto的闹剧
如何在Eclipse PDE的Error Log View中显示自己的Log
Allen Young · 2006-10-05 · via 博客园 - Allen Young

Platform: Eclipse 3.2

开发任何软件都不得不处理Exception和Log,Eclipse Plug-in也是如此。不过幸运的是,Eclipse PDE提供了记录及显示Exception和Log的机制:Error Log View。作为Eclipse SDK的一部分,PDE的普及率很高,所以除非你是要做RCP,不然的话用Error Log View处理Exception和Log应该是你的最佳选择。当然,这也带来了对PDE的依赖性。

使用Error Log View实际上非常简单,每个Plug-in的Activator类都有一个getLog()方法,返回一个ILog对象,这个对象就可以把Exception和Log记录到Error Log View中。ILog对象最主要的方法就是log了,顾名思义,它接收一个IStatus类型的对象,并把其代表的状态记录下来。Eclipse和许多常用的插件(如JDT)实现了很多的IStatus,最common的就是Status类,我们可以简单地使用它,或创建自己的IStatus实现。Status的构造函数有5个参数,具体如下:

  • int severity:日志的级别,可以是OK、ERROR、INFO、WARNING或CANCEL。这些常量都定义在Status类中。
  • String pluginId:当前Plug-in的ID。
  • int code:Plug-in指定的状态码,一般如果无需指定,则使用Status.OK。
  • String message:日志信息。
  • Throwable exception:记录的Exception,如果没有Exception,则传入null。
这样的话,我们就可以编写一个LogUtil类来负责日志工作,代码如下:

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Status;

public class LogUtil {private static LogUtil instance = null;private ILog logger = null;private LogUtil() {
        logger = Activator.getDefault().getLog();
    }
public static LogUtil getInstance() {
        
if (instance == null) {
            instance = 
new LogUtil();
        }
return instance;
    }
public void log(int severity, String message, Throwable exception) {
        logger.log(
new Status(severity, Activator.getDefault().getPluginID(),
                Status.OK, message, exception));
    }
public void logCancel(String message, Throwable exception) {
        logger.log(
new Status(Status.CANCEL, Activator.getDefault()
                .getPluginID(), Status.OK, message, exception));
    }
public void logError(String message, Throwable exception) {
        logger.log(
new Status(Status.ERROR, Activator.getDefault()
                .getPluginID(), Status.OK, message, exception));
    }
public void logInfo(String message, Throwable exception) {
        logger.log(
new Status(Status.INFO,
                Activator.getDefault().getPluginID(), Status.OK, message,
                exception));
    }
public void logOk(String message, Throwable exception) {
        logger.log(
new Status(Status.OK, Activator.getDefault().getPluginID(),
                Status.OK, message, exception));
    }
public void logWarning(String message, Throwable exception) {
        logger.log(
new Status(Status.WARNING, Activator.getDefault()
                .getPluginID(), Status.OK, message, exception));
    }
}


除此之外,我们还可以通过ILog的addLogListener方法和removeLogListener方法为日志动作添加和删除事件监听器。这些Listener可以帮助我们在日志记录完成后做一些额外的事情。例如,如果记录的是ERROR级别的Log,那么我们可能要弹出一个Alert对话框告诉用户出现了错误,但如果是INFO级别,就没这个必要了。