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
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With