Show URL in Tomcat Catalina Error Logs

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

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

GET and POST information will also be important.

Is it possible?

+4
source share
2 answers

Since you want this in your error logs along with (or close to) stacktrace, you probably need Filter .

Implement the doFilter() method to check and register the calling URL for each request. I have provided some code examples below.

Once you access the HttpServletRequest object, you can call any of its methods. Take a look at getRequestURI() and getMethod() which 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 assume you have a logger in place and you can send the output to the log file you want.

Map this Filter to a url pattern, for example /mywebservice in web.xml so that it is not called on other requests, but only on the 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> 
+7
source

Yes ... use AccessLogValve .

For example, in c:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\server.xml put something like the following in your <Host> element:

  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/access-logs/" prefix="localhost_access_log." suffix=".log" pattern="%h %l %t &quot;%r&quot; %s %b &quot;%{Referer}i&quot; &quot;%{User-Agent}i&quot; %T" resolveHosts="false"/> 

Refer to the docs for information on the% fields. For instance. You requested GET and POST information; the method is indicated by the% r (or% m) field. If you want query parameters, they will be part of the query string (% q) for the GET.

The above information will go into your access log. If you request this information in the error logs whenever (and only when) an error occurs: some components do this already, but I don’t know how to do it differently. When I get an error and there is no url, I use timestamps from error logs and access logs to correlate the two.

You can set the logging granularity for various objects: see Logging into Tomcat . This may give you more information, which may or may not be related to any errors that occur.

+1
source

All Articles