为 Eclipse 插件添加日志框架:两种增强 Eclipse 日志功能的方法
2009-12-14 00:00:00 来源:WEB开发网这可以通过实现 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;
}
}
更多精彩
赞助商链接