Saranya
Saranya

Reputation: 35

GWT Framework Enable console logs

Im new to the GWT framework which my project is using now. I wanted to trace the code flow and hence thought I could put System.out logs or debug logs. But nothing worked. Then I came accross this page and saw its tottaly differnet for logging. I added,

Debug.Java

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 ); }-*/;
}

and called inside my class

Frame.java

public void onModuleLoad() {

    logSC("@@@@ onModuleLoad");

     Debug.enable();


    Debug.log("&&&&&&&INSIDE BICC******DEBUG LOGGERRRRRRRRR**************************");
}

But I didnt get the debug logs. Could you please advise me what should i do to enable logs and get printed in my console window.

Regards, Saranya Chandrasekaran

Upvotes: 1

Views: 1084

Answers (4)

Vegegoku
Vegegoku

Reputation: 131

That is not the right way to do logging in GWT, GWT already enables you to use the java logging on the browser, so to enable java logging you need to follow these steps : 1- inherits the java logging module by adding<inherits name="com.google.gwt.logging.Logging"/> to your XXX.gwt.xml file, this will enable logging.

2- enable the console logger by adding:

<set-property name="gwt.logging.logLevel" value="INFO"/>

<set-property name="gwt.logging.enabled" value="TRUE"/>

<set-property name="gwt.logging.consoleHandler" value="ENABLED"/>

-this is the default.

then you can define normal java logger like this

public static final Logger LOGGER = Logger.getLogger(MyClass.getName());
//use the logger
LOGGER.log(Level.info, "my log message");

this kind of logging will work even in the production mode and result in a larger javascript file after compilation. any log statements that does not match the log level defined in the XXX.gwt.xml will be removed from the compilation final result.

another way to do logging for development mode or Super dev mode is to use GWT.log this type of logging will only work for development modes but will be removed in production compilation.

for more readings about this topic you can refer to the gwtproject web site

you can also ask in the gwt-users google group

or you can ask in gwt gitter channel for a live interactive chat

Upvotes: 1

sarir
sarir

Reputation: 111

You need to add the in gwt.xml to enable it < set-prop name="gwt.logging.logLevel" value="ALL" />

<set-property name="gwt.logging.enabled" value="TRUE" />

Upvotes: 0

Christian
Christian

Reputation: 4104

See here a solution for using the gwt log framework:

gwt logging not working

It uses java.util.log

Upvotes: 0

Colin Alworth
Colin Alworth

Reputation: 18356

The console.log call in your JSNI needs a $wnd. prefix so that it runs on the correct window (gwt defaults to sandboxing its code in an iframe).

private static native void nativeConsoleLog( String s ) /*-{
    $wnd.console.log( s ); 
}-*/;

Note that using JsInterop will not have this issue - add elemental2-core to your project, and call DomGlobal.console.log() and it will already work in the main window.

Upvotes: 2

Related Questions