Gwt-logging-framework

提供:Dev Guides
移動先:案内検索

GWT-ロギングフレームワーク

ロギングフレームワークはjava.util.loggingをエミュレートするため、サーバー側のロギングコードと同じ構文を使用し、同じ動作をします

GWTロギングは、.gwt.xmlファイルを使用して構成されます。

ロギングを有効/無効に設定できます。特定のハンドラを有効/無効にし、デフォルトのログレベルを変更できます。

ロガーの種類

ロガーはツリー構造で編成され、ルートロガーはツリーのルートにあります。

ロガーの名前は、*。*を使用して名前のセクションを区切って親/子関係を決定します。

例として、Hospital.room1とHospital.room2の2つのロガーがある場合、それらは兄弟であり、親はHospitalという名前のロガーです。 病院ロガー(およびドット「。」を含まない名前のロガー)は、ルートロガーを親として持っています。

private static Logger room1Logger = Logger.getLogger("Hospital.room1");
private static Logger room2Logger = Logger.getLogger("Hospital.room2");
private static Logger hospitalLogger = Logger.getLogger("Hospital");
private static Logger rootLogger = Logger.getLogger("");

ログハンドラー

GWTは、ロガーを使用して作成されたログエントリを表示するデフォルトハンドラーを提供します。

Handler Logs to Description
SystemLogHandler stdout These messages can only be seen in Development Mode in the DevMode window.
DevelopmentModeLogHandler DevMode Window Logs by calling method GWT.log. These messages can only be seen in Development Mode in the DevMode window.
ConsoleLogHandler javascript console Logs to the javascript console, which is used by Firebug Lite (for IE), Safari and Chrome.
FirebugLogHandler Firebug Logs to the firebug console.
PopupLogHandler popup Logs to the popup which resides in the upper left hand corner of application when this handler is enabled.
SimpleRemoteLogHandler server This handler sends log messages to the server, where they will be logged using the server side logging mechanism.

GWTアプリケーションでロギングを構成する

HelloWorld.gwt.xmlファイルは、次のようにGWTロギングを有効にするように構成されます-

# add logging module
   <inherits name = "com.google.gwt.logging.Logging"/>
# To change the default logLevel
   <set-property name = "gwt.logging.logLevel" value = "SEVERE"/>
# To enable logging
   <set-property name = "gwt.logging.enabled" value = "TRUE"/>
# To disable a popup Handler
   <set-property name = "gwt.logging.popupHandler" value = "DISABLED"/>

ロガーを使用してユーザーアクションを記録する

/*Create Root Logger*/
private static Logger rootLogger = Logger.getLogger("");
...
rootLogger.log(Level.SEVERE, "pageIndex selected: " + event.getValue());
...

ロギングフレームワークの例

この例では、GWTアプリケーションのロギング機能を示す簡単な手順を紹介します。 次の手順に従って、_GWTで作成したGWTアプリケーションを更新します-アプリケーションの作成_の章-

Step Description
1 Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the application to verify the result of the implemented logic.

以下は、変更されたモジュール記述子 src/com.finddevguides/HelloWorld.gwt.xml の内容です。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>
   <inherits name = "com.google.gwt.logging.Logging"/>
   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.finddevguides.client.HelloWorld'/>
   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>
   <set-property name = "gwt.logging.logLevel" value="SEVERE"/>
   <set-property name = "gwt.logging.enabled" value = "TRUE"/>
   <set-property name = "gwt.logging.popupHandler" value=  "DISABLED"/>
</module>

以下は、変更されたスタイルシートファイル war/HelloWorld.css の内容です。

body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

以下は、変更されたHTMLホストファイル war/HelloWorldl の内容です。

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <iframe src = "javascript:''"id = "__gwt_historyFrame"
         style = "width:0;height:0;border:0"></iframe>
      <h1> Logging Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

Javaファイル src/com.finddevguides/HelloWorld.java の以下のコンテンツを使用して、GWTコードでのブックマークのデモを行います。

package com.finddevguides.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

import com.google.gwt.logging.client.HasWidgetsLogHandler;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {

   private TabPanel tabPanel;
  /*Create Root Logger*/
   private static Logger rootLogger = Logger.getLogger("");
   private VerticalPanel customLogArea;

   private void selectTab(String historyToken){
     /*parse the history token*/
      try {
         if (historyToken.substring(0, 9).equals("pageIndex")) {
            String tabIndexToken = historyToken.substring(9, 10);
            int tabIndex = Integer.parseInt(tabIndexToken);
           /*Select the specified tab panel*/
            tabPanel.selectTab(tabIndex);
         } else {
            tabPanel.selectTab(0);
         }
      } catch (IndexOutOfBoundsException e) {
         tabPanel.selectTab(0);
      }
   }

  /**
 *This is the entry point method.
   */
   public void onModuleLoad() {
     /*create a tab panel to carry multiple pages*/
      tabPanel = new TabPanel();

     /*create pages*/
      HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
      HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
      HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");

      String firstPageTitle = "First Page";
      String secondPageTitle = "Second Page";
      String thirdPageTitle = "Third Page";

      Hyperlink firstPageLink = new Hyperlink("1", "pageIndex0");
      Hyperlink secondPageLink = new Hyperlink("2", "pageIndex1");
      Hyperlink thirdPageLink = new Hyperlink("3", "pageIndex2");

      HorizontalPanel linksHPanel = new HorizontalPanel();
      linksHPanel.setSpacing(10);
      linksHPanel.add(firstPageLink);
      linksHPanel.add(secondPageLink);
      linksHPanel.add(thirdPageLink);

     /*If the application starts with no history token,
         redirect to a pageIndex0*/
      String initToken = History.getToken();

      if (initToken.length() == 0) {
         History.newItem("pageIndex0");
         initToken = "pageIndex0";
      }

      tabPanel.setWidth("400");
     /* add pages to tabPanel*/
      tabPanel.add(firstPage, firstPageTitle);
      tabPanel.add(secondPage,secondPageTitle);
      tabPanel.add(thirdPage, thirdPageTitle);

     /*add value change handler to History
      * this method will be called, when browser's Back button
 *or Forward button are clicked.
      * and URL of application changes.
       * */
      History.addValueChangeHandler(new ValueChangeHandler<String>() {
         @Override
         public void onValueChange(ValueChangeEvent<String> event) {
            selectTab(event.getValue());
            rootLogger.log(Level.SEVERE, "pageIndex selected: "
            + event.getValue());
         }
      });

      selectTab(initToken);

      VerticalPanel vPanel = new VerticalPanel();

      vPanel.setSpacing(10);
      vPanel.add(tabPanel);
      vPanel.add(linksHPanel);

      customLogArea = new VerticalPanel();
      vPanel.add(customLogArea);

     /*an example of using own custom logging area.*/
      rootLogger.addHandler(new HasWidgetsLogHandler(customLogArea));

     /*add controls to RootPanel*/
      RootPanel.get().add(vPanel);
   }
}

すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-

GWT Logging Demo

1、2、または3をクリックします。 1,2または3をクリックすると、pageIndexを表示してログが印刷されていることがわかります。 Eclipseでコンソール出力を確認します。 Eclipseコンソールでもログが印刷されていることがわかります。

Fri Aug 31 11:42:35 IST 2012
SEVERE: pageIndex selected: pageIndex0
Fri Aug 31 11:42:37 IST 2012
SEVERE: pageIndex selected: pageIndex1
Fri Aug 31 11:42:38 IST 2012
SEVERE: pageIndex selected: pageIndex2
Fri Aug 31 11:42:40 IST 2012
SEVERE: pageIndex selected: pageIndex0
Fri Aug 31 11:42:41 IST 2012
SEVERE: pageIndex selected: pageIndex1
Fri Aug 31 11:42:41 IST 2012
SEVERE: pageIndex selected: pageIndex2

モジュール記述子 src/com.finddevguides/HelloWorld.gwt.xml を更新して、popupHandlerを有効にします。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
  <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>
   <inherits name = "com.google.gwt.logging.Logging"/>
   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.finddevguides.client.HelloWorld'/>
   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>
   <set-property name = "gwt.logging.logLevel" value = "SEVERE"/>
   <set-property name = "gwt.logging.enabled" value = "TRUE"/>
   <set-property name="gwt.logging.popupHandler" value = "ENABLED"/>
</module>

すべての変更が完了したら、ブラウザウィンドウを更新してアプリケーションをリロードします(ブラウザのF5/リロードボタンを押します)。 アプリケーションの左上隅にポップアップウィンドウが表示されます。

1、2、または3をクリックします。 1,2または3をクリックすると、ポップアップウィンドウにpageIndexを表示してログが印刷されているのがわかります。

GWT Popup Logging Demo