[英]Java - Rich logging in web services
我有這樣一個簡單的Web服務:
@WebService
public class MyWebService
{
@WebMethod
public String ProcessQuery(@WebParam(name="query") String q)
{
// Logging here: User IP, etc.
}
public static void main(String[] args) throws Exception
{
String address = "http://127.0.0.1:8023/_WebServiceDemo";
Endpoint.publish(address, new MyWebService());
new DocumentServer();
System.out.println("Listening: " + address);
}
}
我想為我的服務添加一個日志記錄方法來提取信息。 我聽說過NCSA格式和Log4J,但我不知道如何在服務中使用它們。 我想記錄用戶的IP和其他信息。 我該怎么做?
謝謝。
編輯:我應該注意,我的問題的主要部分是如何在Web方法中檢索一些數據,如用戶的IP,客戶端等。
將WebServiceContext
添加到您的類,以便您可以獲取HttpServletRequest
:
@WebService
public class MyWebService
{
@Resource
WebServiceContext wsContext;
@WebMethod
public String ProcessQuery(@WebParam(name="query") String q)
{
MessageContext messageContext = wsContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest) messageContext.get(SOAPMessageContext.SERVLET_REQUEST);
// now you can get anything you want from the request
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.