如何在GWT中打印到控制台

我正在debugging一个GWT应用程序,我需要打印一些东西到控制台进行testing。 System.out.printlnGWT.log不起作用。 有没有人有任何想法?

引用文档:

添加GWT日志logging非常简单,就像下面的代码示例一样简单。 但是,了解日志如何工作以及如何正确configuration日志非常重要,因此请花时间阅读本文档的其余部分。

http://code.google.com/webtoolkit/doc/latest/DevGuideLogging.html

启用日志的最简单的方法是:

 # In your .gwt.xml file <inherits name="com.google.gwt.logging.Logging"/> # In your .java file Logger logger = Logger.getLogger("NameOfYourLogger"); logger.log(Level.SEVERE, "this message should get logged"); 

我需要在通过PhoneGap(和gwt-phonegap)部署到Android设备/仿真器的GWT应用程序的上下文中执行此操作。 在Android的logcat中没有显示System.out.println()和GWT日志logging(带模块声明),所以我使用了一个简单的JSNI包装器到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 ); }-*/; 

在GWT 2.6.0版本中,方法GWT.log将消息写入浏览器控制台,不需要编写本机方法。

要login浏览器控制台,您可以使用本机进行操作,操作非常简单。 debugging非常有帮助。

如果你添加一个像下面这样的本地方法,你可以从你想要的地方向它发送一个string,并将其logging在浏览器控制台中。

 public static native void console(String text) /*-{ console.log(text); }-*/; 

有关在GWT中使用本机的更多信息: http : //www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

只是在一个片段中总结了mreppy和Strelok的答案中显示的不同可能性。 我还为IEexception添加了一个可能的解决方法,如下所述: 为什么JavaScript只在IE中打开开发人员工具后才工作?

  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); 

另一种使用本地控制台的变体…

添加这个类:

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

然后,在启动应用程序的时候启用debuggingfunction:

 public class XXXXXX implements EntryPoint { @Override public void onModuleLoad() { Debug.enable(); ... } } 

然后就像这样使用它:

 Debug.log("Hello World!"); 

我也有这个问题。 GWT日志工作,但因为它都转换为JavaScript,它打印到客户端的输出,所以只要查看您的浏览器的控制台,他们将在那里。 在谷歌浏览器中点击右上angular的三行自定义button,点击工具 – >开发者工具,控制台将popup。 您的抢手声明将在那里。 此外,Ctrl + Shift + I是提供它的快捷方式。 如果你想打印到服务器,我相信logging器处理程序等是为了?

第一个答案中的文档url已经给出了不同的configuration选项来login到不同的地方。 我写的框架为您提供了一个有用的api,并允许您select服务器端日志logging实现。 看看: https : //code.google.com/p/gwt-usefull-logging/

我build议你使用GWT开发者模式它会增加一些开销,导致代码服务器上的自动编译和代码分配,但是当你的应用程序的客户端出现一些exception时,这很明显。 我的意思是,在某些情况下,Chrome控制台(或者萤火虫或任何浏览器debugging内置工具)在这些情况下并没有太多的说出来,相信我,find一个NullPointerException是一个痛苦的脖子,当你试图找出发生了什么通过提醒您的代码。

对于打印到浏览器控制台,我正在使用这样的东西:

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