Tomcat 请求日志记录 - 如何? [英] Tomcat request logging - how to?

查看:41
本文介绍了Tomcat 请求日志记录 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Tomcat 7.

Env: Tomcat 7.

想要记录 http 请求及其标头.实际上,只要我可以记录调用者的 IP 地址、他请求的资源(URL)和请求类型(GET、POST 等),我就可以不用标头

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.

标准方法是使用 AccessLogValve,但据我了解,实际上不是请求日志记录,而是请求/响应日志记录,这意味着它不会在响应周期结束之前记录任何内容.它只会记录那些响应已成功传递到 http 客户端的请求.如果在 AccessLogValve 之前出现问题,将记录请求.

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.

问题 1:正确理解吗?

Question 1: Is this correctly understood?

问题 2:还有其他选择吗?

Question 2: Are there other options?

我已经使用一个基于 URL 参数阻塞 x 秒的虚拟 servlet 对 Tomcat7 进行了测试.我的发现是,该请求确实被 AccessLogValve 记录了......尽管正如预期的那样,这直到响应结束才会发生,即在 x 秒之后.无论客户端是否在请求完成之前中止,也无论 servlet 在处理过程中是否抛出异常,都会有一个日志条目.

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, i.e. 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.

因此问题 1 的答案是:否".

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

AcccesLogValve 最终生成一个日志条目.至少我没能创造出一个不是的场景.

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.

如果在该 AccessLogValve 之前出现问题,将不会记录请求.

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

问题 1:正确理解吗?"

Question 1: Is this correctly understood?"

不,不是基于我的经验.请求/响应总是被记录下来,即使在处理它时出现错误.在这种情况下,HTTP 状态代码字段(日志模式中的 %s)将包含一个错误代码,例如 500.

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.

这篇关于Tomcat 请求日志记录 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆