当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Tomcat Access Log to display only HTTP Post method

问题描述:

For the application deployed on tomcat, is it possible to get the access logs for only POST method.Currently it displays the logs for both POST and GET.

In the valve component, I see %m attribute configured which prints both the methods (GET and POST).

Is there any configuration with which we can just display the POST requests.

Configuration in server.xml

 <Valve className="org.apache.catalina.valves.AccessLogValve" attribute directory="logs"

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t &quot;%r&quot; %s %b" />

网友答案:

You can use a live tail and filter on "POST" with a grep

$ tail -f localhost_access_log.txt | grep "POST"

This won't modify the output of the access log, but will allow you to monitor, in real time, log entries that only contain the word "POST"

If you are not worried about real time monitoring, but just want to look at all of the POST entries without all of the other noise, you can create a new file using grep

$ cat localhost_access_log.txt | grep "POST" >> new_post_log_file.txt

In the above example, the "new_post_log_file.txt" will contain POST log entries and nothing else. This approach would require you to rerun the command to get the most updated logs.

网友答案:

AccessLogValve does not support that out of the box. It should, however, be easy to extend this class and make the following adjustments:

1) Extend AccessLogValve
2) add a String flag, e.g. method
3) override public void log(Request request, Response response, long time) method. Currently it has the following source:

if (logElements == null || condition != null
            && null != request.getRequest().getAttribute(condition)) {
    return;
}
...

Your method should be something like

if (method != null && !method.equalsIgnoreCase(request.getMethod())) {
    return;
} else {
    super.log(request, response, time);
}

4) Compile your class and use the valve with your flag.

分享给朋友:
您可能感兴趣的文章:
随机阅读: