How to print to the console in GWT

前端 未结 10 1114
广开言路
广开言路 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:52

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

    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.

    0 讨论(0)
  • 2020-12-23 15:02

    In GWT version 2.6.0, method GWT.log writes message to browser console, you don't need to write native methods.

    0 讨论(0)
  • 2020-12-23 15:04

    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");
    
    0 讨论(0)
提交回复
热议问题