How to print to the console in GWT

前端 未结 10 1113
广开言路
广开言路 2020-12-23 14:29

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. Do

相关标签:
10条回答
  • 2020-12-23 14:41

    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!");
    
    0 讨论(0)
  • 2020-12-23 14:41

    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?

    0 讨论(0)
  • 2020-12-23 14:41

    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/

    0 讨论(0)
  • 2020-12-23 14:43

    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 );
      }-*/;
    
    0 讨论(0)
  • 2020-12-23 14:49

    For printing to the browser console I am using something like this:

    EventLogger.java

    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
            });
        }
    }-*/;
    }
    
    0 讨论(0)
  • 2020-12-23 14:50

    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

    0 讨论(0)
提交回复
热议问题