GWT logging setup
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 t开发者_如何学Goo 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.
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);
}
}
精彩评论