Java-Web服务中的丰富日志记录 [英] Java - Rich logging in web services
本文介绍了Java-Web服务中的丰富日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的Web服务,如下所示:
I have a simple web service like this:
@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和其他信息.我该怎么办?
I want to add a logging method for my service to extract information. I've heard about NCSA format and Log4J but I don't know how to use them in the service. I want to log user's ip and other info. How can I do it?
谢谢.
我应该注意,我的问题的主要部分是如何通过网络方法检索某些数据,例如用户的IP,客户端等.
I should note that the main part of my question is how can I retrieve some data such as user's IP, client, etc. in the web method.
推荐答案
将WebServiceContext
添加到您的班级中,以便获得HttpServletRequest
:
Add WebServiceContext
to your class, so you can get the 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
}
}
这篇关于Java-Web服务中的丰富日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文