GWT记录器:不同处理程序的不同级别

时间:2013-06-22 02:29:07

标签: java gwt logging gwt-log4j

我正在尝试配置我的第一个GWT Logger,到目前为止已将日志消息打印到我的JS控制台(Firebug for FF):

<!-- Configure logging. -->
<inherits name="com.google.gwt.logging.Logging"/>
<set-property name="gwt.logging.logLevel" value="FINEST"/>
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.consoleHandler" value="ENABLED"/>
<set-property name="gwt.logging.developmentModeHandler" value="DISABLED" />
<set-property name="gwt.logging.popupHandler" value="DISABLED" />
<set-property name="gwt.logging.systemHandler" value="DISABLED" />
<set-property name="gwt.logging.firebugHandler" value="DISABLED" />
<set-property name="gwt.logging.simpleRemoteHandler" value="DISABLED" />

最终,我希望非SEVERE条消息转到consoleHandlerSEVERE消息转到simpleRemoteHandler

我怎样才能做到这一点?如何将某些日志级别转到1个处理程序,将SEVERE个消息转到另一个处理程序?提前谢谢!

1 个答案:

答案 0 :(得分:0)

根据gwt logging documentation的正常方式是:

GWT logging comes with a set of Handlers already defined and (by default) attached to the Root Logger. You can disable these handlers in the .gwt.xml file as discussed above, extend them, attach them to other loggers, and so forth.

因此,基于此想法,向记录器添加自定义处理程序,并仅向服务器发送SEVERE条记录:

  // This code is almost taken from the SimpleRemoteLogHandler.java because
  // it is a final class and we cannot extend it.
  logger.addHandler(new RemoteLogHandlerBase() {

    private AsyncCallback<String> callback = new AsyncCallback<String>() {
      public void onFailure(Throwable caught) {
        wireLogger.log(Level.SEVERE, "Remote logging failed: ", caught);
      }
      public void onSuccess(String result) {
        if (result != null) {
          wireLogger.severe("Remote logging failed: " + result);
        } else {
          wireLogger.finest("Remote logging message acknowledged");
        }
      }
    };

    private RemoteLoggingServiceAsync service = (RemoteLoggingServiceAsync) GWT.create(RemoteLoggingService.class);

    @Override
    public void publish(LogRecord record) {
      if (record.getLevel() == Level.SEVERE) {
        if (isLoggable(record)) {
          service.logOnServer(record, callback);
        }
      }
    }

  });