I am debugging a GWT application and I need to print some stuff to the console for testing purposes. System.out.println
and GWT.log
don't work. Does anyone have any ideas?

- 10,807
- 132
- 35
- 51
-
1Try the answers posted here: http://stackoverflow.com/questions/17463928/gwt-log-logs-not-showing-in-the-gwt-dev-mode-console/18216990#18216990 – Chepech Aug 16 '13 at 19:45
11 Answers
Quoting the documentation:
Adding GWT logging is really quite simple, as simple as the following code example. However — understanding how logging works, and how to correctly configure it is important, so please do take the time to read the rest of this document.
http://code.google.com/webtoolkit/doc/latest/DevGuideLogging.html
The simplest way to enable logging is:
# In your .gwt.xml file
<inherits name="com.google.gwt.logging.Logging"/>
# In your .java file
Logger logger = java.util.logging.Logger.getLogger("NameOfYourLogger");
logger.log(Level.SEVERE, "this message should get logged");
-
Thanks for your post Strelok! This is exactly what i was looking for !!! I don't understand why people voted this question down! – Feb 12 '12 at 10:53
-
1I think the comment about GWT.log not working is why you got voted down. – checketts Feb 14 '12 at 15:24
-
12Yeh, GWT.log only works in DevMode, so if you are compiling your code with the GWT compiler you will never see any output from GWT.log(). – Andrew Mackenzie Jul 14 '12 at 13:29
-
@Andrew, that's not true at all. GWT.log works in DevMode and Web Mode. – Strelok Jul 16 '12 at 00:20
-
Documentation says "Dev Mode" console with no mention of it appearing elsewhere, and since I have been unable to get it to show any other place...I took that on face value. So.....assuming you have been able to get it to show from compiled code - not sure what I was doing wrong. Anyway, I have moved to using the java.util.logging emulation in client/shared code and so can always call same logging code from any code....and that can have a devModeHandler if you want...so all round better for me. – Andrew Mackenzie Jul 16 '12 at 09:35
-
2@Andrew Ooops. You ARE indeed correct. It's been so long since this answer, that I already forgot all about it :) GWT.log does *not* work in Web Mode. – Strelok Jul 16 '12 at 13:15
-
1This may not work in IE's console logger because of [this bug](https://code.google.com/p/google-web-toolkit/issues/detail?id=6916). They say it's fixed in GWT 2.6 though. – Brad Cupit Jun 27 '13 at 16:33
-
2What is Logger class import you used? This is really frustrating. – CrazySabbath Oct 25 '17 at 12:24
-
1In [LogExample.java](https://github.com/recastrodiaz/google-web-toolkit/blob/master/samples/logexample/src/com/google/gwt/sample/logexample/client/LogExample.java) i found that it uses `java.util.logging.Logger` – Joost Feb 15 '18 at 09:17
I needed to do this in the context of a GWT application that was deployed to an Android device/emulator via PhoneGap (and gwt-phonegap). Neither System.out.println() nor GWT logging as above (with module declaration) showed up in Android's logcat, so I resorted to a simple JSNI wrapper to console.log:
public void onModuleLoad()
{
Logger logger = Logger.getLogger("Test1.java");
logger.log(Level.INFO, "ash: starting onModuleLoad (1)"); // not in logcat
System.out.println( "ash: starting onModuleLoad (2)" ); // not in logcat
consoleLog( "ash: starting onModuleLoad (3)" ); // This shows up
...
}
native void consoleLog( String message) /*-{
console.log( "me:" + message );
}-*/;

- 7,525
- 7
- 61
- 80

- 667
- 5
- 5
-
For all IE users: In order to avoid exceptions when the development tools are not opened you better wrap the call in a try catch block or use one of the solutions stated here: http://stackoverflow.com/q/7742781/1845976 – schnatterer Jul 07 '14 at 14:53
-
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:43
To log to browsers console you can do it using native, in a very simple way. Very helpful in debugging.
If you add a native method like in below, you can send a string to it from where you want and it will log it in the browsers console.
public static native void console(String text)
/*-{
console.log(text);
}-*/;
For more information about using native in GWT: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

- 7,525
- 7
- 61
- 80

- 678
- 1
- 9
- 18
-
1Also instead of giving parameter as String, you can give it something else (example: JavaScriptObject) and console that. – erkinyldz Feb 13 '14 at 15:57
In GWT version 2.6.0, method GWT.log writes message to browser console, you don't need to write native methods.

- 711
- 1
- 10
- 15
-
1I was hoping this would work. I added a GWT.log("Easy to find") and then searched the whole project for the string "Easy to find". It was only in my source. I checked the GWT docs. It says the "GWT.log" is only for dev mode in the dev window. http://www.gwtproject.org/doc/latest/DevGuideLogging.html – ChrisCantrell Nov 23 '15 at 20:51
-
No, GWT.log is only for DEV/SUPERDEV mode, but not for production use (it will be cut out by GWT's compiler). – Wladimir Schmidt Jan 08 '17 at 08:06
Just summing up the different possibilities shown in the answer's of mreppy and Strelok in one snippet. I also added one possible workaround for IE exceptions as described here: Why does JavaScript only work after opening developer tools in IE once?
java.util.logging.Logger logger = Logger.getLogger(this.getClass().getSimpleName());
native void jsConsoleLog(String message) /*-{
try {
console.log(message);
} catch (e) {
}
}-*/;
private void log(final String message) {
// Logs to Dev mode console only
GWT.log(message);
// Logs to Dev mode and JavaScript console (requires configuration)
this.logger.log(Level.FINEST, message);
// Logs to JavaScript console only
jsConsoleLog(message);

- 1
- 1

- 7,525
- 7
- 61
- 80
Yet another variation using the native console...
Add this class:
package XXX.XXX.XXX.XXX;
public class Debug {
private static boolean isEnabled_ = false;
public static void enable() { isEnabled_ = true; }
public static void setEnabled( final boolean isEnabled )
{ isEnabled_ = isEnabled; }
public static void log( final String s )
{ if( isEnabled_ ) nativeConsoleLog( s ); }
private static native void nativeConsoleLog( String s )
/*-{ console.log( s ); }-*/;
}
Then, enable debugging with it at some point, like upon starting the app:
public class XXXXXX implements EntryPoint {
@Override
public void onModuleLoad() {
Debug.enable();
...
}
}
Then just use it like so:
Debug.log("Hello World!");

- 10,221
- 5
- 83
- 96
I had this problem as well. The GWT log works but because it's all converted to javascript, it prints to the client output, so just view your browser's console and they will be there. In Google Chrome click the triple-line Customize button in the top right, click Tools-->Developer tools and the console will pop up. Your sought-after statements will be there. Also, Ctrl+Shift+I is the shortcut that brings it up. If you want to print to the server, I believe logger handlers and such are in order?

- 47
- 1
- 9
The documentation url in the first answer already gives the different configuration option to log to different places. This framework i wrote offers you a usefull api and allows you to choose your server-side logging implementation. Have a look : https://code.google.com/p/gwt-usefull-logging/

- 568
- 3
- 9
I suggest you use GWT Developer mode It adds a little overhead cause the automatic compilation and code-allocating on the code server, but it's pretty clear when some exceptions arises in client side of your application. I mean, some times chrome console (or firebug or whatever browser debugging built-in tool) doesn't say too much in those situations, trust me, finding a NullPointerException is a pain in the neck when you try to figure out what is happening by alerting your code.
For printing to the browser console I am using something like this:
public class EventLogger {
public static void logEvent(String subsys, String grp, String type) {
logEvent(GWT.getModuleName(), subsys, grp,
Duration.currentTimeMillis(), type);
}
public static native void logEvent(String module, String subsys,
String grp, double millis, String type)
/*-{
if ($wnd.__gwtStatsEvent) {
$wnd.__gwtStatsEvent({
'moduleName':module,
'subSystem':subsys,
'evtGroup':grp,
'millis':millis,
'type':type
});
}
}-*/;
}

- 7,525
- 7
- 61
- 80

- 21
- 3
You can put alaert.Alert(""); in your gwt code compile it and run it you will get pop up on browser when you make request or at the action where you have placed that alert

- 21
- 1