在 Tomcat 的 Catalina 错误日志中显示 URL

发布于 2024-09-16 00:52:28 字数 152 浏览 6 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-23 00:52:28

由于您希望将其与堆栈跟踪一起(或接近)记录在错误日志中,
您可能需要一个过滤器

实现 doFilter() 方法来检查并记录每个请求的调用 URL。我在下面给出了一些示例代码。

一旦您访问 HttpServletRequest< /code>对象,您可以调用它的任何方法。请参阅您需要的 getRequestURI()getMethod()

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);
}

我假设您有一个记录器,并且您可以将输出发送到您想要的日志文件。

将此Filter映射到web.xml中的/mywebservice这样的url模式上,这样它就不会在其他请求上被调用,而只会在您的网络服务。

<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>

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>
宫墨修音 2024-09-23 00:52:28

是的...使用 AccessLogValve

例如,在 c:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\server.xml 中,将类似以下内容放入 元素中:

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

有关 % 的信息,请参阅文档字段。
例如,您要求获取 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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文