Since you want this in your error logs along with (or close to) the stacktrace,
you probably need a Filter
.
Implement the doFilter()
method to check and log the calling URL for each request. I've given some sample code below.
Once you get access to the HttpServletRequest
object, you can call any of it's methods.See the getRequestURI()
and getMethod()
which are the ones you need.
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
{
HttpServletRequest hsr = (HttpServletRequest) request;
String incomingUrl=hsr.getRequestURI(); // fetch URL
String method =hsr.getMethod(); // fetch GET/POST method
logger.debug ("The caller was " + incomingUrl + method); // Send the output to any logger which you have defined
chain.doFilter(request, response);
}
I'm assuming you have a logger in place and you can send the output to the log file you wish to.
Map this Filter
on a url-pattern like /mywebservice
in the web.xml
so that it does not get called on other requests but only on your web service.
<filter>
<filter-name>Logging_URL_Filter</filter-name>
<filter-class>com.mysite.URLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Logging_URL_Filter</filter-name>
<url-pattern>/mywebservice</url-pattern>
</filter-mapping>