如何在Eclipse PDE的Error Log View中显示自己的Log
2012-07-20 16:08
323 查看
开发任何软件都不得不处理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级别,就没这个必要了。
备注:
本来eclipse自带有默认日志的,但是由于RCP中有时候不能依赖PDE,所以还是自己单独实现一套跟log4j相关的,简单的日志处理视图。
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级别,就没这个必要了。
备注:
本来eclipse自带有默认日志的,但是由于RCP中有时候不能依赖PDE,所以还是自己单独实现一套跟log4j相关的,简单的日志处理视图。
相关文章推荐
- 如何在Eclipse PDE的Error Log View中显示自己的Log
- 如何在Eclipse PDE的Error Log View中显示自己的Log
- Eclipse打不开,显示打开log查看error解决办法
- struts的ui标签,theme(主题)(如何去掉fielderror actionerror actionmessage前的小点,让其按自己要求显示的5种方法)
- 如何在 Eclipse 的 Error Log 中显示插件自己的日志
- 如何处理eclipse插件开发中的view的显示与隐藏
- 在Android中如何获取视频的第一帧图片并显示在一个ImageView中
- swift 如何控制view的显示与隐藏
- 在PCH中定制自己的LOG打印日志,分别在DEBUG 与 RELEASE的状态下处理,及如何把PCH引入到项目中
- 【百度地图API】如何利用自己的数据制作社交地图?只显示可视区域内的标注
- 使用WindowManager.addView()方法增加的悬浮窗口,如何固定屏幕显示方向?
- 如何在Eclipse中显示行号
- 如何在formview里显示属性选项卡propertysheet
- Android:ImageView如何显示网络图片
- 解决eclipse中logcat不显示log的问题
- (转)用eclipse创建一个j2ee的web工程后,左面projects窗口中的项目如何没有显示webRoot文件夹,除了src的文件夹,其他都不显示
- Eclipse中如何显示代码行
- eclipse 出现An error has occurred. See the log file f:\eclipse\configuration\的解决办法
- 如何使UIScrollView的滚动条indicator一直显示
- Webview--如何让加载进来的页面自适应手机屏幕分辨率居中显示