【问题标题】:Tomcat Access Log to display only HTTP Post methodTomcat 访问日志仅显示 HTTP Post 方法
【发布时间】:2016-01-14 21:12:18
【问题描述】:

对于部署在tomcat上的应用,是否可以仅获取POST方式的访问日志。目前它显示POST和GET的日志。

在阀门组件中,我看到配置了 %m 属性,该属性打印了两种方法(GET 和 POST)。

是否有任何配置可以让我们只显示 POST 请求。

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

【问题讨论】:

标签: java api rest tomcat


【解决方案1】:

您可以使用 live tail 并使用 grep 过滤“POST”

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

这不会修改访问日志的输出,但允许您实时监控仅包含单词“POST”的日志条目

如果您不担心实时监控,而只想查看所有 POST 条目而没有其他干扰,您可以使用 grep 创建一个新文件

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

在上面的示例中,“new_post_log_file.txt”将包含 POST 日志条目,仅此而已。这种方法需要您重新运行命令以获取最新的日志。

【讨论】:

    【解决方案2】:

    AccessLogValve 不支持开箱即用。但是,扩展此类并进行以下调整应该很容易:

    1) 扩展AccessLogValve
    2)添加一个字符串标志,例如method
    3) 覆盖public void log(Request request, Response response, long time) 方法。目前有以下来源:

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

    你的方法应该是这样的

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

    4) 编译你的类并将阀门与你的标志一起使用。

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 2015-09-15
      • 2014-06-29
      • 1970-01-01
      • 2017-11-25
      • 2015-02-17
      • 2020-08-21
      • 2011-11-27
      • 2020-05-22
      相关资源
      最近更新 更多