print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.
Logs by calling method GWT. log. These messages can only be seen in Development Mode in the DevMode window. Logs to the javascript console, which is used by Firebug Lite (for IE), Safari and Chrome.
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");
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 );
}-*/;
In GWT version 2.6.0, method GWT.log writes message to browser console, you don't need to write native methods.
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
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);
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!");
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