`

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

gwt 
阅读更多

开发人员指南 - 记录

This document is for developers interested in logging client-side code in their GWT applications.这份文件是在记录他们的GWT应用程序的客户端代码感兴趣的开发人员。 Logging is the process of recording events in an application to provide an audit trail to understand how the application executes and to diagnose problems.记录是在应用程序中记录的事件,以提供审计线索了解如何执行应用程序和诊断问题的过程。 Logging makes it easier to troubleshoot issues encountered by developers and users.记录使得更容易地解决开发者和用户所遇到的问题。

The following sections walk through a logging example application and introduce the basic functionality of the Logging framework and configuration options.以下各节走过一个日志示例应用程序,并引入日志框架和配置选项的基本功能。 Developers should already be familiar with developing a GWT application.开发人员应该已经熟悉开发GWT应用程序。

  1. Overview of the logging framework 日志框架概述
  2. Super Simple Recipe for Adding GWT Logging 超级简单的食谱中添加GWT的记录
  3. Building/Running the Logging Example 建筑/运行记录范例
  4. Loggers, Handlers and the Root Logger 伐木者,处理和根Logger
  5. Configuring GWT Logging 配置GWT的日志
  6. Different Types of Handlers 不同类型的处理程序
  7. Client vs. Server-side Logging 客户端与服务器端日志记录
  8. Remote Logging 远程日志记录
  9. Making All Logging Code Compile Out 所有日志代码编译
  10. Emulated and Non-Emulated Classes 仿真和非模拟类

Overview of the Logging Framework日志框架概述

The logging framework emulates java.util.logging, so it uses the same syntax and has the same behavior as server side logging code.日志框架模拟的java.util.logging,因此它使用相同的语法,并已作为服务器端日志记录代码相同的行为。 This allows you to share logging code between the client and server side code.这可以让你分享记录之间的客户端和服务器端代码的代码。 A good overview of java logging is here: http://download.oracle.com/javase/6/docs/technotes/guides/logging/overview.html ; you should familiarize yourself with java.util.logging to get a good feel for how to use GWT logging. Java记录的一个很好的概述是 http://download.oracle.com/javase/6/docs/technotes/guides/logging/overview.html;您应该熟悉自己的java.util.logging,以获得良好的手感如何使用GWT记录。

Unlike java.util.logging, GWT logging is configured using .gwt.xml files. java.util.logging的不同,GWT日志记录配置使用。gwt.xml文件。 You can use these files to enable/disable logging entirely, enable/disable particular handlers, and change the default logging level.您可以使用这些文件,启用/禁用日志记录完全,启用/禁用特定的处理程序,并更改默认的日志记录级别。 When logging is disabled, the code will compile out, so you can use logging when developing/debugging, and then have your production build compile it out to keep your JavaScript size small.禁用日志记录时,代码将编译出来,所以你可以使用日志记录开发/调试时,然后你的生产建​​设编译它,以保持您的JavaScript体积小。

Super Simple Recipe for Adding GWT Logging超级简单的食谱中添加GWT的记录

Adding GWT logging is really quite simple, as simple as the following code example.添加GWT的记录是很简单的的,如下面的代码示例简单。 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.然而 - 了解如何记录的作品,以及如何正确地配置它是重要的,所以请不要花时间去阅读本文的其余部分。

    # In your .gwt.xml file #在。gwt.xml文件 
    <inherits name="com.google.gwt.logging.Logging"/> <inherits name="com.google.gwt.logging.Logging"/> 
 
    # In your .java file #在你的。java文件 
    Logger logger = Logger.getLogger("NameOfYourLogger");记录器记录= Logger.getLogger(“NameOfYou​​rLogger”); 
    logger.log(Level.SEVERE, "this message should get logged"); logger.logLevel.SEVERE,“这个消息应该得到记录”);

Warning about inheriting logging: If you do not inherit com.google.gwt.logging.Logging , then logging will technically work, since the emulation of the Java code is always present. 有关继承采伐的警告 :如果你不com.google.gwt.logging.Logging com.google.gwt.logging.Logging,然后记录将技术上的工作,因为Java代码的仿真是始终存在的。 However, you will not get any default Handlers, or any ability to configure the Root Logger (as discussed in this document).但是,你不会得到任何默认处理程序,或任何配置根Logger(在本文件中所讨论的)的能力。 Not inheriting Logging is sometimes done by libraries that want to log errors or information but do not want to control how a customer application would display that information (they don't want to configure or turn on logging, but they do want to make logging information available if a user of the library does turn on logging).不是继承记录有时要记录错误或信息,但不希望控制客户应用程序将如何显示的信息(他们不想配置或启用日志记录,但他们想使日志信息的图书馆如果库的用户没有开启记录)。

Building/Running the Logging Example建筑/运行记录范例

You build and run LogExample the same way you would build and run any of the other GWT samples, like Hello.您构建和运行LogExample喜欢你好,你将建立和运行任何其他的GWT样品,以同样的方式。 The eclipse directory in the svn source contains a README.txt file to help. SVN源的eclipse目录包含一个README.txt的文件,以帮助。 When you run it, the following "Logging Example" web page appears with popup menus and checkboxes for triggering test exceptions and seeing how they log.当你运行它,会出现下面的“记录范例”网页触发测试异常,并看到他们是如何登录的弹出菜单和复选框。

记录范例网页

LogExample is configured using LogExample.gwt.xml . LogExample配置使用LogExample.gwt.xml The entry point for the app is LogExample.java — it simply creates and adds the various demo modules to the page.应用程序的入口点LogExample.java LogExample.java -它只是简单地创建和添加各种演示模块页面。 Each of these modules illustrates a different set of logging concepts; this tutorial will walk you through them.这些模块都说明了日志概念的不同,本教程将通过他们走。

Loggers, Handlers and the Root Logger伐木者,处理和根Logger

Loggers are organized in a tree structure, with the Root Logger at the root of the tree.记录仪组织在一个树状结构,在树的根,根Logger。 Parent/Child relationships are determined by the name of the logger, using "."父/子关系是由记录器的名称,使用“。” to separate sections of the name.单独的名称部分。 So if we have two loggers Foo.Bar and Foo.Baz, then they are siblings, with their parent being the logger named Foo.因此,如果我们有两个记录器foo.bar的和Foo.Baz,那么他们是兄弟,他们的父母是一个名为foo的记录器。 The Foo logger (and any logger with a name which does not contain a dot ".") has the Root Logger as a parent. Foo的记录(和任何记录,名称不包含一个圆点“。”),作为父母的根Logger。

When you log a message to a logger, if the Level of the message is high enough, it will pass the message on to its parent, which will pass it on to its parent, and so on, until the Root Logger is reached.当你记录器记录的消息,如果消息级别足够高时,它会通过其母公司的消息,将它传递到它的父,等等,直到根Logger是达到。 Along the way, any given logger (including the Root Logger) will also pass the message to any of its Handlers, and if the Level of the message is high enough, those handlers will output the message in some way (to a popup, to stderr, etc.).一路上,任何给定的记录器(包括根Logger)也将通过其处理的任何消息,如果消息级别足够高,这些处理程序以某种方式将输出消息(一个弹出, STDERR等)。 For a much more detailed explanation of this, see http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html .有关此更详细的解释,请参阅http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html

If you open LogExample.java you can see that we've created 3 loggers:如果你打开LogExample.java ,你可以看到我们已经创建了3伐木者:

 // LogExample.java / / LogExample.java 
 
  private static Logger childLogger = Logger.getLogger("ParentLogger.Child");私人静态记录器childLogger = Logger.getLogger(“ParentLogger.Child”); 
  private static Logger parentLogger = Logger.getLogger("ParentLogger");私人静态记录器parentLogger = Logger.getLogger(“ParentLogger”); 
  private static Logger rootLogger = Logger.getLogger("");私人静态记录器rootLogger = Logger.getLogger ("");

We've passed these 3 loggers into LoggerController , which in turn, creates an instance of OneLoggerController for each of them.我们已经传递到这3伐木者LoggerController ,这反过来,创建一个实例OneLoggerController为他们每个人。 In OneLoggerController.java you can see example code for changing the Level of the logger, logging to the logger, and logging an exception to the logger.OneLoggerController.java可以看到示例代码改变Logger级别,日志记录器,日志记录器的一个例外。

 // OneLoggerController / / OneLoggerController 
 
  // Change the level of the logger / /改变记录仪的水平 
  @UiHandler("levelTextBox") @ UiHandler(“levelTextBox”) 
  void handleLevelClick (ChangeEvent e) {无效handleLevelClickChangeEvent E){ 
    Level level = Level.parse(levelTextBox.getItemText(水平的高低= Level.parselevelTextBox.getItemText 
        levelTextBox.getSelectedIndex())); levelTextBox.getSelectedIndex ())); 
    logger.log(Level.SEVERE, logger.logLevel.SEVERE 
        "Setting level to: " + level.getName()); “设置水平:”level.getName()); 
    logger.setLevel(level); logger.setLevel(水平); 
  } } 
 
  // Log a message to the logger / /记录器记录一条消息 
  @UiHandler("logButton") @ UiHandler(“logBu​​tton”) 
  void handleLogClick (ClickEvent e) {无效handleLogClickClickEvent E){ 
    Level level = Level.parse(logTextBox.getItemText(水平的高低= Level.parselogTextBox.getItemText 
        logTextBox.getSelectedIndex())); logTextBox.getSelectedIndex ())); 
    logger.log(level, "This is a client log message"); logger.log(水平,“这是一个客户端的日志信息”); 
  } } 
 
  // Trigger an exception and log it to the logger / /触发异常和日志记录器 
  @UiHandler("exceptionButton") @ UiHandler(“exceptionButton”) 
  void handleExceptionClick (ClickEvent e) {无效handleExceptionClickClickEvent E){ 
    try {尝试{ 
      Level n = null;级别n = NULL; 
      n.getName(); n.getName(); 
    } catch (NullPointerException ex) { }赶上(NullPointerException异常前){ 
      logger.log(Level.SEVERE, "Null Exception Hit", ex); logger.logLevel.SEVERE,“空异常击中”,前); 
    } } 
  } }

You can play around with these 3 loggers.您可以与这3伐木者玩耍。 For now, the easiest place to look for log messages is the popup created in LogExample.java on the web page现在,最容易的地方寻找日志消息是在LogExample.java创建网页上的弹出 <!-- doog: add a link -->(different Handlers are discussed in the next section). (不同的处理将在下一节讨论)。

Configuring GWT Logging配置GWT的日志

In most simple logging implementations, we simply deal with the Root Logger.在最简单的日志记录实现,我们简单地处理根Logger。 All messages get propagated up the tree to the Root Logger.所有的消息得到传播树的根Logger。 The Level and Handlers attached to the Root Logger are what control which messages get logged and to where.等级和处理程序附加到根记录器是什么控制消息得到记录和到哪里。 This is the basic idea behind the default Handler configurations in GWT logging.这是GWT中的记录背后的默认处理程序配置的基本思路。

The simplest item you can configure is the Level of the Root Logger.最简单的项目,您可以配置根Logger的级别。 You can do this by adding logLevel query parameter to your URL.您可以通过加入这个logLevel查询参数您的网址。 Try this now, by adding "&logLevel=SEVERE" to the sample URL.现在试试这个加入“LOGLEVEL =严重”的范例URL。 Notice how the default level of all of the loggers is now set to SEVERE rather than to INFO.请注意,现在设置的所有的记录器的默认级别是如何严重的,而不是为INFO。

The other way to configure GWT logging is through a .gwt.xml file as follows: GWT的日志记录配置的另一种方法是通过如下gwt.xml文件。

# LogExample.gwt.xmlLogExample.gwt.xml 
 
    <set-property name="gwt.logging.logLevel" value="SEVERE"/> # To change the default logLevel <set-property name="gwt.logging.logLevel" value="SEVERE"/>要更改默认LOGLEVEL 
    <set-property name="gwt.logging.enabled" value="FALSE"/> # To disable logging <set-property name="gwt.logging.enabled" value="FALSE"/>要禁用日志记录 
    <set-property name="gwt.logging.consoleHandler" value="DISABLED"/> # To disable a default Handler <set-property name="gwt.logging.consoleHandler" value="DISABLED"/>#禁用默认的处理程序

You can experiment with configuring logging in the provided LogExample.gwt.xml file.您可以尝试配置所提供的LogExample.gwt.xml文件记录。

Different Types of Handlers不同类型的处理程序

GWT logging comes with a set of Handlers already defined and (by default) attached to the Root Logger. GWT的日志已经定义(默认)连接到根logger的处理程序。 You can disable these handlers in the .gwt.xml file as discussed above, extend them, attach them to other loggers, and so forth.您可以禁用。gwt.xml文件,正如上面所讨论的这些处理,延长它们,将它们附加到其他的伐木工人,等等。 You can experiment with adding/removing the various Handlers from the Root Logger using the checkboxes — the code behind this in HandlerController.java .你可以尝试用添加/删除从根记录器使用复选框的各种处理程序-这背后HandlerController.java代码HandlerController.java。 Note that if you disable a Handler using the .gwt.xml file, any instance of it will be replaced with a NullLogHandler (which does nothing and compiles out), so the handler cannot be added/removed using the checkboxes.请注意如果您禁用某个处理程序使用。gwt.xml文件,将它的任何实例与NullLogHandler(它什么也不做,并编译出)所取代,所以处理程序不能使用复选框添加/删除。

Here's an example of how a checkbox adds or removes a handler:这里的一个例子,如何添加一个复选框或删除一个处理程序:

 // HandlerController.java / / HandlerController.java 
 
    public void onValueChange(ValueChangeEvent公共无效onValueChangeValueChangeEvent   event) {事件){ 
      if (checkbox.getValue()) { checkbox.getValue()){ 
        logger.addHandler(handler); logger.addHandler(处理); 
      } else { }否则{ 
        logger.removeHandler(handler); logger.removeHandler(处理); 
      } } 
    } }

Most of the default Handlers are very straightforward大多数默认的处理程序是非常简单

  • SystemLogHandler - Logs to stdout. SystemLogHandler -日志到stdout。 These messages can only be seen in Development Mode — look for them in the DevMode window这些消息只能被看作在开发模式 - 看看他们的DEVMODE窗口
  • DevelopmentModeLogHandler - Logs by calling method GWT.log. DevelopmentModeLogHandler -通过调用方法GWT.log的记录。 These messages can only be seen in Development mode — look for them in the DevMode window这些消息只能被看作在开发模式 - 看看他们的DEVMODE窗口
  • ConsoleLogHandler - Logs to the javascript console, which is used by Firebug Lite (for IE), Safari and Chrome(?) ConsoleLogHandler -这是Firebug的精简版(IE),Safari和Chrome的JavaScript控制台,日志(?)
  • FirebugLogHandler - Logs to Firebug FirebugLogHandler - Firebug的日志
  • PopupLogHandler - Logs to the popup which appears in the upper left hand corner PopupLogHandler -出现在左上角弹出的日志
  • SimpleRemoteLogHandler - Discussed below, in the Remote Logging section SimpleRemoteLogHandler -下面讨论在远程日志记录“部分

Although the PopupLogHandler is easy to use, it is also a bit invasive.虽然PopupLogHandler易于使用,它也有点侵入。 A better solution for most apps is to disable the PopupLogHandler and instead send the log messages to a Panel somewhere in your app.对于大多数应用程序的一个更好的办法是PopupLogHandler的PopupLogHandler,而不是发送日志消息的一个小组在您的应用程序的某个地方。 GWT logging is set up to make this easy, and you can see an example of this in CustomLogArea.java . GWT记录是成立使这个容易,你可以看到这样一个例子CustomLogArea.java In this case, we have created a VerticalPanel (although any widget which extends HasWidgets and supports multiple add() calls can be used).在这种情况下,我们已经创建了一个VerticalPanel (虽然任何部件延伸HasWidgets ,并支持多个add()的调用可以使用) 。 Once we have one of these widgets, we simply pass it into the constructor of a HasWidgetsLogHandler and add that Handler to a logger.一旦我们有了这些部件之一,我们简单地传递到一个构造HasWidgetsLogHandler ,该处理程序添加一个logger。

 // VerticalPanel.java / / VerticalPanel.java 
 
    VerticalPanel customLogArea; VerticalPanel customLogArea; 
 
    // An example of adding our own custom logging area. / /加入我们自己的自定义日志记录区的一个例子。 Since VerticalPanel extends HasWidgets,由于VerticalPanel延伸HasWidgets, 
    // and handles multiple calls to add(widget) gracefully we simply create a new HasWidgetsLogHandler / /优雅地处理多个呼叫添加(部件),我们只需创建一个新的HasWidgetsLogHandler 
    // with it, and add that handler to a logger. / /有了它,该处理程序添加一个logger。 In this case, we add it to a particular logger in order在这种情况下,我们把它添加到一个特定的logger 
    // to demonstrate how the logger hierarchy works, but adding it to the root logger would be fine. / /证明记录器的层次结构是如何工作的,但将它添加到根logger将被罚款。 
    logger.addHandler(new HasWidgetsLogHandler(customLogArea)); logger.addHandler(新HasWidgetsLogHandlercustomLogArea));

Client vs. Server-side Logging客户端与服务器端日志记录

Although GWT emulates java.util.logging, it is important to understand the difference between server-side and client-side logging.虽然GWT模拟的java.util.logging,重要的是要了解服务器端和客户端日志记录之间的差异。 Client-side logging logs to handlers on the client side, while server-side logging logs to handlers on the server side.客户端日志记录在客户端的处理程序,而服务器端日志记录在服务器端的处理程序。

 

To make this clear, the client-side GWT code has a Root Logger (and logger hierarchy) that is separate from the server-side code; all of the handlers discussed above are only applicable to client-side code.要明确这一点,客户端GWT代码有一个根Logger(和记录器的层次结构),从服务器端代码是分开的;上面讨论的所有处理程序只适用于客户端代码。 If code shared by the client and server makes logging calls, then which Root Logger (and logger hierarchy) it logs to will depend on whether it is being executed on the client or server side.如果客户端和服务器共享的代码,使得日志记录调用,那么这将取决于无论是在客户端或服务器端执行的根Logger(和记录器的层次结构),它记录。 You should not add or manipulate Handlers in shared code, since this will not work as expected.你不应该添加或操纵在共享代码的处理程序,因为这将无法按预期工作。

In ServerLoggingArea.java , you can experiment with these concepts. ServerLoggingArea.java ,您可以尝试与这些概念。 The buttons in that section will trigger logging calls on the server, as well as logging calls in SharedClass.java from both the client and server side.该节中的按钮,将触发记录在服务器上调用,以及记录在SharedClass.java SharedClass.java从客户端和服务器端。 Note the slight differences in formatting between client-side and server-side logging, as well as the different handlers each is logged to (in the tutorial, server-side logging will simply log to stderr, while client-side logging will log to all of the Handlers discussed above).注意:客户端和服务器端日志记录格式之间细微的差别,以及不同的处理,每个记录(在本教程中,服务器端日志记录将只需登录到stderr,而客户端日志记录,将记录所有处理程序如上所述)。

Remote Logging远程日志记录

In order for events that are logged by client side code to be stored on the server side, you need to use a RemoteLogHandler .为了记录到存储在服务器端,客户端代码的事件,你需要RemoteLogHandler RemoteLogHandler 。 This handler will send log messages to the server, where they will be logged using the server side logging mechanism.此处理程序将日志消息发送到服务器,在那里他们将使用服务器端的日志记录机制记录。 GWT currently contains a SimpleRemoteLogHandler which will do this in the simplest possible way (using GWT-RPC) and no intelligent batching, exponential backoffs in case of failure, and so forth. GWT目前包含SimpleRemoteLogHandler会做最简单的方式(使用GWT - RPC)和没有智能配料,在失败的情况下指数backoffs,等等。 This logger is disabled by default, but you can enable it in the .gwt.xml file (see the section on Handlers above for more details on configuring the default Handlers).这个记录器默认是禁用的,但可以启用它。gwt.xml文件(见上述处理程序配置默认的处理程序的更多细节)。

# LogExample.gwt.xmlLogExample.gwt.xml 
 
  <set-property name="gwt.logging.simpleRemoteHandler" value="ENABLED" /> <set-property name="gwt.logging.simpleRemoteHandler" value="ENABLED"

You will also need to serve the remoteLoggingServlet.您还需要服务remoteLoggingServlet。

Making All Logging Code Compile Out所有日志代码编译

When logging is disabled, the compiler will used Deferred Binding to substitute Null implementations for the Logger and Level classes.禁用日志记录时,编译器将使用延迟绑定替代Logger和级别的类空实现。 Since these implementations just return Null, and do nothing, they will generally get trimmed by the GWT compiler (which does a pretty good job of removing useless code).由于这些实现返回null,什么也不做,他们一般会得到修整由GWT编译器(其中一个不错的工作,删除无用的代码)。 However, it is not guaranteed that other code you write related to logging will compile out.但是,它不能保证你写的有关伐木的其他代码将编译。 If you want to guarantee that some chunk of code is removed when logging is disabled, you can use the LogConfiguration.loggingIsEnabled() method:如果你想禁用日志记录时,以保证一些代码块被删除,您可以使用LogConfiguration.loggingIsEnabled()方法:

  if (LogConfiguration.loggingIsEnabled()) { LogConfiguration.loggingIsEnabled()){ 
    String logMessage = doSomethingExpensiveThatDoesNotNormallyCompileOut();字符串logMessage = doSomethingExpensiveThatDoesNotNormallyCompileOut(); 
    logger.severe(logMessage); logger.severelogMessage); 
  } } 

Code that normally compiles out will still be present in Development mode.通常编译出来的代码将仍然是目前的发展模式。 You can use the same condition as above to hide code from Development Mode, as shown here:您可以使用上述同样的条件,从发展模式中隐藏的代码,如下所示:

 // VerticalPanel.java / / VerticalPanel.java 
 
    // Although this code will compile out without this check in web mode, the guard will ensure / /虽然此代码将编译没有这个检查在Web模式下,将确保后卫 
    // that the handler does not show up in development mode. / /处理程序不显示在发展模式。 
    if (LogConfiguration.loggingIsEnabled()) { LogConfiguration.loggingIsEnabled()){ 
      logger.addHandler(new HasWidgetsLogHandler(customLogArea)); logger.addHandler(新HasWidgetsLogHandlercustomLogArea)); 
    } }

Emulated and Non-Emulated Classes仿真和非模拟类

The GWT logging framework does not emulate all parts of java.util.logging. GWT日志框架不模仿java.util.logging的所有部件。 See JRE Emulation Reference for a list of the emulated classes and members.的JRE仿真模拟类和成员名单参考。

The following Handlers and Formatters are provided:下列处理和格式化提供:

  HTMLFormatter HTMLFormatter
  TextFormatter TextFormatter
  SystemLogHandler SystemLogHandler
  ConsoleLogHandler ConsoleLogHandler
  FirebugLogHandler FirebugLogHandler
  DevelopmentModeLogHandler DevelopmentModeLogHandler
  HasWidgetsLogHandler (and LoggingPopup to use with it) HasWidgetsLogHandler(和LoggingPopup使用)
<!-- end gc-pagecontent -->
分享到:
评论
1 楼 ccx410 2011-12-19  
安装gwt报错,unable to retrieve osgi.bundle,com.google.gwt.eclipse.sdkbundle如何解决

相关推荐

Global site tag (gtag.js) - Google Analytics