Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show URL in Tomcat's Catalina error logs

I'm kind of new to the Java world, and I need to debug a restful Javax Web Service application.

Some pages raise exceptions, and they are logged correctly, but I would like to have the URL of the page that called the code, inside of my logs, as well as the stacktrace.

GET and POST information would be vital too.

Is it possible?

like image 244
Louis-Mathieu Houle Avatar asked Feb 26 '23 10:02

Louis-Mathieu Houle


1 Answers

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>
like image 128
JoseK Avatar answered Mar 07 '23 13:03

JoseK