在 Tomcat 的 Catalina 错误日志中显示 URL
我对 Java 世界有点陌生,我需要调试一个安静的 Javax Web 服务应用程序。
有些页面引发异常,并且它们被正确记录,但我希望在我的日志中包含调用代码的页面的 URL 以及堆栈跟踪。
GET 和 POST 信息也很重要。
是否可以?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您希望将其与堆栈跟踪一起(或接近)记录在错误日志中,
您可能需要一个
过滤器
。实现
doFilter()
方法来检查并记录每个请求的调用 URL。我在下面给出了一些示例代码。一旦您访问
HttpServletRequest< /code>
对象,您可以调用它的任何方法。请参阅您需要的
getRequestURI()
和getMethod()
。我假设您有一个记录器,并且您可以将输出发送到您想要的日志文件。
将此
Filter
映射到web.xml
中的/mywebservice
这样的url模式上,这样它就不会在其他请求上被调用,而只会在您的网络服务。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 thegetRequestURI()
andgetMethod()
which are the ones you need.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 theweb.xml
so that it does not get called on other requests but only on your web service.是的...使用 AccessLogValve。
例如,在
c:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\server.xml
中,将类似以下内容放入
元素中:有关 % 的信息,请参阅文档字段。
例如,您要求获取 GET 和 POST 信息;该方法由 %r(或 %m)字段显示。如果您需要查询参数,它们将成为 GET 的查询字符串 (%q) 的一部分。
上述信息将进入您指定的访问日志。
如果您要求每当(且仅当)发生错误时在错误日志中包含此信息:某些组件已经这样做了,但我不知道如何让它发生。当我收到错误并且没有给出 URL 时,我会使用错误日志和访问日志中的时间戳来将两者关联起来。
您可以为各种设施设置日志记录的粒度:请参阅登录 Tomcat。这可以为您提供更多信息,这些信息可能与发生的任何错误相关,也可能无关。
Yes... use AccessLogValve.
For example, in
c:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\server.xml
, put something like the following into your<Host>
element:See the docs for information about the % fields.
E.g. you asked for GET and POST information; the method is shown by the %r (or %m) field. If you want query parameters, they will be part of the query string (%q) for GETs.
The above information will go into an access log that you specify.
If you are asking to have this information in the error logs whenever (and only when) an error occurs: some components do that already, but I don't know how to make it happen otherwise. When I get an error and there's no URL given, I use the timestamps from the error logs and access logs to correlate the two.
You can set the granularity of logging for various facilities: see Logging in Tomcat. That can get you more information, which may or may not be relevant to any errors that occur.