1. tomcat的日志分类

    http://blog.csdn.net/fly910905/article/details/78463909

  2. tomcat产生的访问日志数据  【localhost_access_log.Y-M-D.txt】

    它记录的访问的时间,IP,访问的资料等相关信息 

  3. 首先是配置tomcat访问日志数据,配置的方式如下

    Valve参数说明:

    className        官方文档上说了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。
    directory 日志文件存放的目录。通常设置为tomcat下已有的那个logs文件。
    prefix 日志文件的名称前缀。
    suffix 日志文件的名称后缀。
    pattern 最主要的参数。
    pattern。它的参数比较多。可以设置成common,combined两种格式。

    common的值:%h %l %u %t %r %s %b
    combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i

    resolveHosts 如果是true,tomcat会将这个服务器IP地址通过DNS转换为主机名;如果是false,就直接写服务器IP地址。默认false。
    rotatable  默认为true,tomcat生成的文件名为prefix(前缀)+.+时间(一般是按天算)+.+suffix(后缀),如:localhost_access_log.2007-09-22.txt。设置为false的话,tomcat会忽略时间,不会生成新文件,文件名就是:localhost_access_log.txt。长此以往,这个日志文件会超级大
    condition 这个参数不太实用,可设置任何值,比如设置成condition="tkq",那么只有当ServletRequest.getAttribute("tkq")为空的时候,该条日志才会被记录下来。

    fileDateFormat                                                                                 
    顾名思义,就是时间格式嘛。但这个时间格式是针对日志文件名起作用的。咱们生成的日志文件全名:localhost_access_log.2016-09-22.txt,这里面的2016-09-22就是这么来的。如果想让tomcat每小时生成一个日志文件,也很简单,将这个值设置为:fileDateFormat="yyyy-MM-dd.HH",当然也可以按分钟生成什么的,自己改改吧^_^
    1. 打开${catalina}/conf/server.xml文件
    2. 注:${catalina}是tomcat的安装目录
       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      tomcat访问日志关闭:

       只需要将${catalina}/conf/server.xml里面的下列申明注释掉再重启tomcat就好 

  4. 具体的日志产生样式说明如下(从官方文档中摘录)

    * %a - Remote IP address // 这是记录访问者的IP,在日志里是127.0.0.1
    * %A - Local IP address // 这是记录本地服务器的IP,在日志里是192.168.254.108
    * %b - Bytes sent, excluding HTTP headers, or '-' if zero // 发送信息的字节数,不包括http头,如果字节数为0的话,显示为-
    * %B - Bytes sent, excluding HTTP headers // 发送信息的字节数,不包括http头。
    * %h - Remote host name (or IP address if resolveHosts is false) //服务器的名称。如果resolveHosts为false的话,这里就是IP地址了,例如我的日志里是10.217.14.16
    * %H - Request protocol //访问者的协议,这里是HTTP/1.0
    * %l - Remote logical username from identd (always returns '-') // 官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns '-')
    * %m - Request method (GET, POST, etc.) // 访问的方式,是GET还是POST
    * %p - Local port on which this request was received // 本地接收访问的端口
    * %q - Query string (prepended with a '?' if it exists) // 比如你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,就是querystring的意思
    * %r - First line of the request (method and request URI) // First line of the request (method and request URI) 请求的方法和URL
    * %s - HTTP status code of the response // http的响应状态码
    * %S - User session ID // 用户的session ID,这个session ID大家可以另外查一下详细的解释,反正每次都会生成不同的session ID
    * %t - Date and time, in Common Log Format // 请求时间
    * %u - Remote user that was authenticated (if any), else '-' // 得到了验证的访问者,否则就是"-"
    * %U - Requested URL path // 访问的URL地址,我这里是/rightmainima/leftbott4.swf
    * %v - Local server name // 服务器名称,可能就是你url里面写的那个吧,我这里是localhost
    * %D - Time taken to process the request, in millis // 请求消耗的时间,以毫秒记
    * %T - Time taken to process the request, in seconds //请求消耗的时间,以秒记
    There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.
    It is modeled after the apache syntax:
    * %{xxx}i for incoming headers
    * %{xxx}c for a specific cookie
    * %{xxx}r xxx is an attribute in the ServletRequest
    * %{xxx}s xxx is an attribute in the HttpSession
  5. 示例【格式为 %h %l %u %t "%r" %s %b

    140.205.201.39 - - [31/Aug/2017:17:06:52 +0800] "GET /clusters.jsf HTTP/1.1" 404 -
    60.191.38.77 - - [31/Aug/2017:17:11:35 +0800] "GET / HTTP/1.1" 404 -
    61.232.253.54 - - [31/Aug/2017:19:04:53 +0800] "GET / HTTP/1.1" 404 -
    100.116.151.190 - - [31/Aug/2017:20:45:17 +0800] "GET /CloudPayment/bill/querySelfPayList.do HTTP/1.0" 200 60
    100.116.41.2 - - [31/Aug/2017:20:45:34 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    100.116.151.145 - - [31/Aug/2017:20:46:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    100.116.41.41 - - [31/Aug/2017:20:46:42 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    100.116.151.181 - - [31/Aug/2017:20:47:21 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    100.116.151.181 - - [31/Aug/2017:20:47:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

记录Post请求参数

另外%r参数能打印出请求的url和get参数。如果url指定访问方式是post,post的参数是打印不出来的。当需要打印post参数,该怎么办?

server.xml配置

tomcat访问日志格式配置,在config/server.xml里Host标签下加上

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

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

pattern="%h %l %u %t &quot;%r&quot; [%{postdata}r] %s %{Referer}i %{User-Agent}i %T %b" />

postdata过滤器

package com.xiaoxiliuimport java.io.IOException;
import java.util.Enumeration;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public final class PostDataDumperFilter implements Filter {Logger logger = LoggerFactory.getLogger(getClass());private FilterConfig filterConfig = null;public void destroy() {this.filterConfig = null;}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {if (filterConfig == null)return;Enumeration<String> names = request.getParameterNames();StringBuilder output = new StringBuilder();while (names.hasMoreElements()) {String name = (String) names.nextElement();output.append(name).append("=");String values[] = request.getParameterValues(name);for (int i = 0; i < values.length; i++) {if (i > 0) {output.append("' ");}output.append(values[i]);}if (names.hasMoreElements())output.append("&");}request.setAttribute("postdata", output);logger.debug("postdata: " + output);chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}
}

在web.xml中添加配置该filter

<filter>
<filter-name>post-data-dumper-filter</filter-name>
<filter-class>com.xiaoxiliu.PostDataDumperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>post-data-dumper-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

查询访问最耗时的接口

这就要用到万能的awk了 我们的日志倒数第二列显示的访问时间。

cat logs/localhost_access_log.2016-10-25.txt | awk '{print $(NF-1)" "$0}' | sort -n -r| awk '{$1="";print $0}'  按照倒数第二列由大到小显示接口以及访问时间。

这样我们就能找出那些借口耗时较大,然后对其进行优化,提高用户体验。

参考来源:http://blog.csdn.net/qq_30121245/article/details/52861935

参考来源:http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html

Tomcat的访问日志-localhost_access_log和记录Post请求参数相关推荐

  1. Tomcat日志记录post请求参数

    公司的项目向用户提供接口,但是最近偶尔会出现超时的情况,用户的调用设置的超时时间是1分钟,所以首先要排查代码的执行时间是否超过一分钟 @PostMapping("/updateattachm ...

  2. 通过Nginx,Tomcat访问日志(access log)记录请求耗时

    一.Nginx通过$upstream_response_time $request_time统计请求和后台服务响应时间 nginx.conf使用配置方式: log_format main '$remo ...

  3. tomcat 系统访问日志配置

    http://www.itzhuyi.com/Item/Show.asp?m=1&d=53460 在server.xml里的<host>标签下加上 <Valve classN ...

  4. tomcat 服务器访问日志配置方法

    最近需要配置tomcat访问日志,通过造访他人的博客,再根据apache官方的一些文章,才逐渐清晰了许多 配置方法很简单: 在server.xml里的<host>标签下加上 <Val ...

  5. tomcat的访问日志

    https://blog.csdn.net/qq_30121245/article/details/52861935 配置位置在这里,每一个域名设置都可以单独设置 %a 这是记录访问者的IP,如果用了 ...

  6. tomcat记录访问日志

    一般的web server有两部分日志: 一是运行的日志,它主要肌瘤运行的一些信息,尤其是一些异常错误日志信息 二是访问日志信息,他是记录的访问的时间,ip,url,sessionId等信息. 下面来 ...

  7. filebeat 收集json格式_Filebeat 采集日志实践经验记录

    Filebeat 日志采集工具在日常工作中,使用场景更多的是用来采集Nginx 日志和Java 应用运行的日志了.这里简单记录一下踩到坑,和大家分享. 一.Filebeat 收集Nginx 访问日志 ...

  8. 【linux】apache的访问日志详解及演练

    文章目录 前言 一. apache日志分类 二. apache的日志格式详解 1. 输出最近的一段访问日志 2. 日志的字段所代表的内容如下: 3. apache访问日志配置文件参数详解 三. 配置修 ...

  9. Tomcat访问日志详细配置

    在server.xml里的<host>标签下加上 <Valve className="org.apache.catalina.valves.AccessLogValve&q ...

最新文章

  1. python的优点-python的优点
  2. php退出登录后不能再登录了,php怎么实现退出登录功能?
  3. js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter
  4. 20165234 《Java程序设计》第六周学习总结
  5. linux查找文件限制时间,Linux查找命令find与文件三种时间
  6. ccd相机好修吗_「CCD购买指南 」CCD废片大公开
  7. 数据结构二之线段树Ⅰ——Count Color,Hotel,Transformation,Tree Generator™
  8. python高级玩法_python pandas to_excel 高级玩法
  9. 重启开源,分享无限--微软面试187题精选
  10. go mod常用命令
  11. DbUtils组件的使用小指南
  12. 【计算机网络自顶向下方法】Web页面请求的历程
  13. 微信红包系统设计方案
  14. Android之按钮点击事件(单击、双击、长按等)
  15. 零基础Python学习(2) --- 《我想你猜》1.0版
  16. 准备一个月,考过软件系统架构师
  17. 加密文件夹里的图片打不开
  18. android微单,用相机打电话 国产安卓系统微单了解一下
  19. MySQL--常见业务/笔试题
  20. CL-ReLKT: Cross-lingual Language Knowledge Transfer for MultilingualRetrieval Question Answering论文阅读

热门文章

  1. c语言子函数返回数组,main函数3种方法打印输出
  2. PHP_MYSQL 留言板
  3. this指向和改变其指向的方法
  4. 基于RT-Thread全向赛车控制算法开发
  5. Dell Alienware Area-51m装Ubuntu系统时遇到的问题
  6. #千锋逆战班,Cq# 在千锋“逆战”学习第 20天,今天学了内部类的四种书写格式
  7. pycharm社区版使用database navigator(DB Browser)
  8. 哪些星座最害怕被甩 、you没有你?
  9. 欧式距离、标准化欧式距离、马氏距离、余弦距离
  10. Mysql 之 插入数据(insert into 、 replace into 解析)