2014-02-09 88 views
3

我想使用GWT Remote Logging。這是我做過什麼:GWT遠程日誌記錄不能記錄Throwable Stacktrace?

在我的web.xml文件中我所做的:

<servlet> 
     <servlet-name>remoteLogging</servlet-name> 
     <servlet-class>com.google.gwt.logging.server.RemoteLoggingServiceImpl</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>remoteLogging</servlet-name> 
     <url-pattern>/web/remote_logging</url-pattern> 
    </servlet-mapping> 

在我gwt.xml文件我所做的:

<module rename-to='web'> 

...  

<inherits name="com.google.gwt.logging.Logging" /> 
    <set-property name="gwt.logging.logLevel" value="INFO" /> 
    <set-property name="gwt.logging.enabled" value="TRUE" /> 
    <set-property name="gwt.logging.popupHandler" value="DISABLED" /> 
    <set-property name="gwt.logging.developmentModeHandler" value="ENABLED" /> 
    <set-property name="gwt.logging.simpleRemoteHandler" value="ENABLED" /> 
在我的GWT Java代碼我

而且某處做:

Logger logger = Logger.getLogger("NameOfYourLogger"); 
logger.log(Level.SEVERE, "this message should get logged"); 

但是,當我嘗試登錄堆棧跟蹤(的Throwable)是這樣的:

// Throwable throwable 

LogRecord lr = new LogRecord(Level.SEVERE, "test"); 
lr.setThrown(throwable); 
logger.log(lr); 

我得到一個錯誤:

16:24:26.887 [ERROR] [web] Sun Feb 09 16:24:26 CET 2014 WireActivityLogger 
SEVERE: Remote logging failed: 
com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: java.lang.ClassCastException: com.google.gwt.core.client.impl.SerializableThrowable cannot be cast to com.google.gwt.core.shared.SerializableThrowable 
    at com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException_FieldSerializer.instantiate(IncompatibleRemoteServiceException_FieldSerializer.java:16) 
    at com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException_FieldSerializer.create(IncompatibleRemoteServiceException_FieldSerializer.java:25) 
    at com.google.gwt.user.client.rpc.impl.SerializerBase.instantiate(SerializerBase.java:115) 
    at com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader.deserialize(ClientSerializationStreamReader.java:396) 
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamReader.readObject(AbstractSerializationStreamReader.java:119) 
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:216) 
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258) 
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412) 
    at sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338) 
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219) 
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279) 
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) 
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java) 
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242) 
    at sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293) 
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547) 
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364) 
    at java.lang.Thread.run(Thread.java:722) 

我怎麼能遠程日誌堆棧跟蹤?

編輯:我也試着

// Throwable throwable 
LogRecord lr = new LogRecord(Level.SEVERE, throwable.toString()); 
logger.log(lr); 

// Throwable throwable 
LogRecord lr = new LogRecord(Level.SEVERE, throwable.getMessage()); 
logger.log(lr); 

,但我得到了以下錯誤:

| Error 2014-02-19 02:21:04,017 [http-nio-8080-exec-7] ERROR [localhost].[/test] - Exception while dispatching incoming RPC call 
Message: Service method 'public abstract java.lang.String com.google.gwt.logging.shared.RemoteLoggingService.logOnServer(java.util.logging.LogRecord)' threw an unexpected exception: java.lang.NullPointerException 
    Line | Method 
->> 389 | encodeResponseForFailure in com.google.gwt.user.server.rpc.RPC 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 579 | invokeAndEncodeResponse in  '' 
| 265 | processCall . . . . . . in com.google.gwt.user.server.rpc.RemoteServiceServlet 
| 305 | processPost    in  '' 
|  62 | doPost . . . . . . . . . in com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet 
|  46 | doFilterInternal   in org.grails.jaxrs.web.JaxrsFilter 
|  70 | doFilter . . . . . . . . in com.github.greengerong.PreRenderSEOFilter 
| 1145 | runWorker    in java.util.concurrent.ThreadPoolExecutor 
| 615 | run . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 722 | run      in java.lang.Thread 
Caused by NullPointerException: null 
->> 262 | hash      in java.util.Hashtable 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 459 | get      in  '' 
| 489 | findLogger . . . . . . . in java.util.logging.LogManager$LoggerContext 
| 910 | getLogger    in java.util.logging.LogManager 
| 400 | demandLogger . . . . . . in  '' 
| 317 | demandLogger    in java.util.logging.Logger 
| 361 | getLogger . . . . . . . in  '' 
|  62 | logOnServer    in com.google.gwt.logging.server.RemoteLoggingServiceUtil 
|  46 | logOnServer . . . . . . in com.google.gwt.logging.server.RemoteLoggingServiceImpl 
| 561 | invokeAndEncodeResponse in com.google.gwt.user.server.rpc.RPC 
| 265 | processCall . . . . . . in com.google.gwt.user.server.rpc.RemoteServiceServlet 
| 305 | processPost    in  '' 
|  62 | doPost . . . . . . . . . in com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet 
|  46 | doFilterInternal   in org.grails.jaxrs.web.JaxrsFilter 
|  70 | doFilter . . . . . . . . in com.github.greengerong.PreRenderSEOFilter 
| 1145 | runWorker    in java.util.concurrent.ThreadPoolExecutor 
| 615 | run . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 722 | run      in java.lang.Thread 

編輯:

這是我的代碼一世使用方法:

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

new AsyncCallback<GetResults<ItemDto>>() { 


      @Override 
      public void onFailure(Throwable e) { 
       Logger logger = Logger.getLogger("test"); 
       logger.log(Level.SEVERE, "this message should get logged"); 

       LogRecord lr = new LogRecord(Level.SEVERE, e.toString()); 
       logger.log(lr); 

      } 

      ... 

} 
+0

'InvocationException:'然後呢?你的模塊的名稱是什麼(根據你的web.xml應該是'web')?你的服務器日誌中的任何輸出? –

+0

@ColinAlworth我更新了我的問題記錄作品的字符串,但不是Throwable,正如我指出的那樣。任何想法? – confile

回答

1

將SerializableThrowable從client.impl庫轉換爲core.shared庫時,您正在獲取異常。看起來你只是想將問題作爲字符串寫入日誌。我從來沒有使用這個類(只是從文件讀取),你有沒有試過以下...

// Throwable throwable 
LogRecord lr = new LogRecord(Level.SEVERE, throwable.toString()); 
logger.log(lr); 

// Throwable throwable 
LogRecord lr = new LogRecord(Level.SEVERE, throwable.getMessage()); 
logger.log(lr); 

如果這不起作用,你可能會發現以下鏈接有用:

http://www.summa-tech.com/blog/2012/06/11/7-tips-for-exception-handling-in-gwt/

注意,在上面的網址,在提示4,筆者將他的計劃了相同於你的。但他提醒在最後一段....

"[Log exceptions on the server] seems straight-forward, right? Be careful! First, not all exceptions are serializable or follow the rules of serializable (ugh!). For example, GWT’s JavascriptException inherits Serializable, but doesn’t implement a public no-arg constructor, so when you try to send this over the wire via your RPC, a run-time Serialization exception is thrown. And since this exception would be thrown inside your exception handler, it would effectively swallowed. Second, the stack trace within an exception is transient, and so [it] is lost from client to server (so if you need it on the server side, send it as a separate parameter). Which leads me to the next tip…"

在一段他說你有確切的原始問題 - 不是所有的例外是序列化所以序列化可能會引發異常(這可能也解釋了爲什麼當您試圖從AsyncCallback類的onFailure方法中輸出拋出的錯誤消息時,throwable對象爲null。然後繼續閱讀提示編號5和6.

這些鏈接可能也是有用的......

Best Practices for GWT services exceptions logging

http://cleancodematters.com/2011/05/29/improved-exceptionhandling-with-gwts-requestfactory/

+0

我試過你的解決方案,但失敗了。我在我的問題中發佈了錯誤。任何其他想法? – confile

+0

@ confile是否有一個名爲thowable的Throwable對象,在您的評論指出時定義?聽起來像是空的,當它被添加爲LogRecord的第二個參數時,它引起一個空指針異常。 – Nate

+0

我發佈了我正在使用的代碼。 Throwable不爲null。 – confile

1

看着你得到的錯誤,看來你的GWT應用正在發送的com.google.gwt.core.client.impl.SerializableThrowable實例,但您的服務器要求的com.google.gwt.core.shared.SerializableThrowable實例。

在我們的項目中,我們仍然使用GWT 2.5,我們在SDK中的類是第一個。

看來,GWT 2.6引入了第二個,並棄用了另一個。也許你的客戶端和你的服務器不在完全相同的GWT版本上?或者你有客戶端代碼以某種方式使用錯誤的SerializableThrowable。

而且,這裏有兩個屬性,我注意到,我們有,但你不會在GWT配置提及:

<set-property name="compiler.stackMode" value="emulated" /> 
<set-configuration-property name="compiler.emulatedStack.recordLineNumbers" value="true" />