有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把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 获取即可

最新文章

  1. jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用
  2. 理解MySQL数据库覆盖索引
  3. 修改oracle系统用户system的密码
  4. VB中 ByRef与ByVal区别
  5. git: No refs in common and none specified; doing no
  6. How to configure Veritas NetBackup (tm) to write Unified and Legacy log files to a different directory
  7. numpy中的broadcast
  8. iPhone开发中的技巧整理
  9. jquery mobile validation
  10. XML Drawable与9-Patches
  11. Keil MDK与h-jtag联调
  12. webstrom管理git
  13. Linux下C的线程同步机制
  14. Hbuilder实用技巧
  15. Java程序员的现代RPC指南
  16. Taurus.MVC 支持Asp.Net Core 的过程
  17. tensorflow实现RNN及Word2Vec
  18. IdentityServer4-介绍
  19. C++中的类模板
  20. 1.cs与bs结构

热门文章

  1. WDCP(WDlinux Control Panel) mysql/add_user.php、mysql/add_db.php Authentication Loss
  2. nginx在linux中安装
  3. FluentData,它是一个轻量级框架,关注性能和易用性。
  4. mysql大表如何优化
  5. Linux 磁盘分区
  6. poj2642 The Brick Stops Here(DP基础题)
  7. NPOI在.net中的操作Excel
  8. 转&lt;&lt;C#集合Dictionary中按值的降序排列
  9. 字符串模拟赛T3
  10. thinkphp中limit方法