有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式

c-ip s-ip x-InstancePort date time-taken x-Protocol cs-method cs-uri sc-status x-ResponseSize bytes x-Referrer x-UserAgent

在tomcat的conf/server.xml加入配置:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%{X-Real-IP}i %A %p %{yyyy-MM-dd HH:mm:ss}t %D %H %m %U %s %B %B %{Referer}i %{User-Agent}i" />

关于accessLog的官方说明,请看 http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Logging

常用配置如下:

  • %a - Remote IP address  --远程IP地址
  • %A - Local IP address --本地IP地址
  • %b - Bytes sent, excluding HTTP headers, or '-' if zero --发送的字节数(Bytes sent), 不包括HTTP headers的字节,如果为0则展示'-'
  • %B - Bytes sent, excluding HTTP headers --发送的字节数(Bytes sent), 不包括HTTP headers的字节
  • %h - Remote host name (or IP address if enableLookups for the connector is false) --远程主机名称(如果resolveHosts为false则展示IP)
  • %H - Request protocol --请求协议
  • %l - Remote logical username from identd (always returns '-') --远程用户名,始终为'-'(Remote logical username from identd)
  • %m - Request method (GET, POST, etc.) --请求的方法(GET, POST等)
  • %p - Local port on which this request was received. See also %{xxx}p below. --接受请求的本地端口
  • %q - Query string (prepended with a '?' if it exists) --查询字符串,如果存在,有一个前置的'?'
  • %r - First line of the request (method and request URI) --请求的第一行(包括请求方法和请求的URI)
  • %s - HTTP status code of the response --response的HTTP状态码(200,404等)
  • %S - User session ID --用户的session ID
  • %t - Date and time, in Common Log Format --日期和时间,Common Log Format格式
  • %u - Remote user that was authenticated (if any), else '-' --被认证的远程用户, 不存在则展示'-'
  • %U - Requested URL path --请求URL路径
  • %v - Local server name --本地服务名
  • %D - Time taken to process the request, in millis --处理请求的时间,单位为毫秒
  • %T - Time taken to process the request, in seconds --处理请求的时间,单位为秒
  • %F - Time taken to commit the response, in millis --提交响应的时间,以毫秒为单位
  • %I - Current request thread name (can compare later with stacktraces) --当前请求的线程名

Access Log中也支持cookie,请求header,响应headers,Session或者其他在ServletRequest中的对象的信息

  • %{xxx}i write value of incoming header with name xxx
  • %{xxx}o write value of outgoing header with name xxx
  • %{xxx}c write value of cookie with name xxx
  • %{xxx}r write value of ServletRequest attribute with name xxx
  • %{xxx}s write value of HttpSession attribute with name xxx
  • %{xxx}p write local (server) port (xxx==local) or remote (client) port (xxx=remote)
  • %{xxx}t write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern xxx

如果tomcat前面有nginx做负载均衡,则要修改nginx的配置

Nginx的server主机配置段中添加:

proxy_set_header X-Real-IP $remote_addr; #真实的远端IP地址

然后再tomcat accessLog用%{X-Real-IP}i 获取即可

Tomcat AccessLog 格式化的更多相关文章

  1. tomcat accesslog日志扩展

    由于工作需要,最近对tomcat的日志进行了一些研究,发现其日志大致可以分为两类,一类是运行日志,即平常我们所说的catalina.out日志,由tomcat内部代码调用logger打印出来的:另一类 ...

  2. 配置spring boot 内置tomcat的accessLog日志

    #配置内置tomcat的访问日志server.tomcat.accesslog.buffered=trueserver.tomcat.accesslog.directory=/home/hygw/lo ...

  3. Tomcat access log配置

    在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP.线程号.访问url.返回状态码.访问时间.持续时间. 在Spring boot中使用了内嵌的tomcat, ...

  4. springboot中配置tomcat的access log

    在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP.线程号.访问url.返回状态码.访问时间.持续时间. 在Spring boot中使用了内嵌的tomcat, ...

  5. Access Logging Tomcat

    73.6 Configure Access Logging server.tomcat.accesslog.buffered=true # Buffer output such that it is ...

  6. springboot tomcat配置参数列表

    springboot tomcat的配置选项大全 server. Port = xxxx server. Address = server. contextPath = server. display ...

  7. tomcat和springboot访问日志及分析

    1.Tomcat设置访问日志 <Host name="localhost" appBase="webapps" unpackWARs="true ...

  8. Spring Boot tomcat

    定制内嵌 Tomcat 设置内嵌Tomcat的端口 Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口.如果,我们需要修改Tomcat的端口,我们可以在 src/main/ ...

  9. Spring Boot Tomcat配置详解

    参数配置容器 server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似. 所有参数绑定配置类:org.springframe ...

随机推荐

  1. Java输入、输入、IO流 类层次关系梳理

    本文主要关注在Java编程中涉及到的IO相关的类库.方法.以及对各个层次(抽线.接口继承)的流之间的关系进行梳理 相关学习资料 http://baike.baidu.com/view/1007958. ...

  2. boost解析json(2)

    "{ "A":1, "B":{ "C":2, "D":3 }, "E":[ {" ...

  3. --hdu 2570 迷瘴(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2570 Ac code: #include<stdio.h> #include<std ...

  4. php闭包支持

    匿名函数提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() { }; //带结束符 可以看到 ...

  5. Dynamic Virtual Channels

    refer http://blogs.msdn.com/b/rds/archive/2007/09/20/dynamic-virtual-channels.aspx An important goal ...

  6. html表单验证程序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 模式串匹配之KMP算法

    模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1,  第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...

  8. dedecms删除没有文章的标签

    要批量的删除织梦TAG标签,那我们就只能在数据库里做修改了. 登录数据库,在数据库里执行以下SQL语句: delete FROM dede_tagindex where typeid not in ( ...

  9. Caesar's Legions(三维dp)

    Caesar's Legions Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  10. Android四大组件之Service

    Android四大组件之Service Android支持服务的概念,服务是在后台运行的组件,没有用户界面,Android服务可用有与活动独立的生命周期.Android支持两种类型的服务: 本地服务: ...