10

I'm using GWT 2.1 java.util.logging emulation to log client side messages. According to the doc, two Formatters are provided (TextFormatter and HTMLFormatter) which are appropriate to client side logging.

Can anyone provide an example on how to setup a formatter and attach it to a handler in GWT?

Thanks

Javier Ferrero
  • 8,741
  • 8
  • 45
  • 50

3 Answers3

17

See the GWT documentation for logging here. It really depends on where you want your logging to appear, but if you only care about logging in Dev mode then you only need the SystemLogHandler and the DevelopmentModeLogHandler. The ConsoleLogHandler and FirebugLogHandler are used for web mode logging to chrome, firebug and firebug lite. The PopupLogHandler and HasWidgetsLogHandler add the log messages to some sort of UI element. All of the above should be capable of being enabled/disabled in the .gwt.xml except the HasWidgetsLogHandler which requires an associated widget container. This should be possible by adding the following:

<inherits name="com.google.gwt.logging.Logging"/>
<set-property name="gwt.logging.logLevel" value="SEVERE"/> # To change the default logLevel
<set-property name="gwt.logging.enabled" value="FALSE"/> # To disable logging
<set-property name="gwt.logging.consoleHandler" value="DISABLED"/>  # To disable a default Handler
<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="ENABLED" />
etc...
LINEMAN78
  • 2,562
  • 16
  • 19
  • You didnt address or answer the question at all, but copy pasted from the official doco for basic setup which anyone can find. – mP. Dec 17 '12 at 06:24
  • 1
    It is important to note that changes to these properties require a restart of the SuperDev Mode. – Phil C Apr 22 '15 at 09:19
  • I disagree, @mP. This does address the question, and copy/pasting content from the official documentation is much better than providing a link-only answer. – Amos M. Carpenter Dec 11 '15 at 02:40
  • Could you take a look at [this question](http://stackoverflow.com/questions/32438000/gwt-logger-not-getting-logs-below-level-severe)? – Stefan Falk Dec 14 '15 at 21:42
9

Here is a simple example of adding a Log handler to the Root logger. The logger uses the HTMLLogFormatter and puts the message in a HTML widget.

HTML html = new HTML();
// add the html widget somewhere in your code.
Logger.getLogger("").addHandler(new Handler() {
  {
    // set the formatter, in this case HtmlLogFormatter
    setFormatter(new HtmlLogFormatter(true));
    setLevel(Level.ALL);
  }

  @Override
  public void publish(LogRecord record) {
    if (!isLoggable(record)) {
      Formatter formatter = getFormatter();
      String msg = formatter.format(record);

      html.setHTML(msg);
    }
  }
});

Also have a look at HasWidgetsLogHandler, which basically does what the handler in the example above does.

Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • Thanks Hilbrand. Do you know if it is possible do the same setup using properties in the gwt.xml file? – Javier Ferrero Jan 26 '11 at 17:24
  • You mean when logging is disabled in your gwt.xml file no logging code is generated when you compile gwt? – Hilbrand Bouwkamp Jan 26 '11 at 17:39
  • I mean configuring logging via properties instead of doing it programatically – Javier Ferrero Jan 26 '11 at 18:13
  • The standard loggers can be enabled/disabled via properties (Thats the list lineman78 has in his answer), but nothing more. If you want a custom formatter you need to add the handler programatically as shown in my answer. – Hilbrand Bouwkamp Jan 27 '11 at 09:38
  • As it took so long to go through all the classes with the same names, it appears this is the import statement for this: `import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.logging.client.HtmlLogFormatter;`. And you need to also implement `close()` and `flush()` for the anonymous Handler this creates (not shown). – MrLore Feb 07 '13 at 15:42
4

Here are the two classes I ended up using:

import java.util.Date;
import java.util.logging.LogRecord;

import com.google.gwt.logging.impl.FormatterImpl;

public class LogFormatter extends FormatterImpl {

private static final StringBuilder sb = new StringBuilder();

@Override
public String format(LogRecord rec) {
    synchronized (sb) {
        sb.setLength(0);
        sb.append(new Date(rec.getMillis()).toString());
        sb.append(": ");
        sb.append(rec.getMessage());
        sb.append("\n");
        return sb.toString();
    }
}

}

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ALog {
/* IMPORTANT: User blank string (root logger) here or else it WILL NOT have the    formatter being used */
private static final Logger logger = Logger.getLogger("");

static {
    for (Handler handler : logger.getHandlers()) {
        handler.setFormatter(new LogFormatter());
        handler.setLevel(Level.ALL);
    }
}

public static void log(String msg) {
    logger.log(Level.INFO, msg);
}

public static void log(String msg, Throwable e) {
    logger.log(Level.INFO, msg, e);
}
}