简体   繁体   中英

Tomcat request logging - how to?

Env: Tomcat 7.

Would like to log http requests and their headers. Actually I could do without the headers as long as I can log the IP address of the caller, the resource he's requesting (the URL) and the type of request (GET, POST, etc)

This may seem like a trivial question, but it really isn't.

The standard way would be to use the AccessLogValve , but as far as I understand that one is actually not request logging, it is request/response logging, meaning that it will not log anything before at the end of the response cycle. It will only log those requests where a response has successfully been delivered to the http client. If something goes wrong before that AccessLogValve will not log the request.

Question 1: Is this correctly understood?

Question 2: Are there other options?

UPDATE 1:

I've done a test with Tomcat7 using a dummy-servlet that does blocking for x seconds based on an URL parameter. My findings are that indeed that request gets logged by the AccessLogValve ... although as expected this does not happen until the end of the response, ie after the x seconds. There will be a log entry regardless if the client has aborted before the request finishes and regardless if the servlet throws an exception during processing.

Therefore the answer to question 1 is : "No".

Conclusion

AcccesLogValve will eventually produce a log entry. At least I haven't been able to produce a scenario where this is not the case.

All the access logs that I have seen are written after the request/response has been processed because it is useful to log info like the size of the response or the total processing time.

"If something goes wrong before that AccessLogValve will not log the request.

Question 1: Is this correctly understood?"

No, not based on my experience. The request/response is always logged, even if there is an error processing it. In that case the HTTP status code field ( %s in the log pattern) will contain an error code, like 500.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM