有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把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. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

  2. iOS开发编码建议与编程经验

    作者:乞力马扎罗的雪(GitHub) 原文 在开发过程中,我们不仅要去看别人的代码,也要让别人看我们的代码.那么,有一个良好的编码习惯将会非常重要.下面将会罗列使用Objective-C来开发iOS的 ...

  3. 代码注册广播接收者&利用广播调用服务的方法服务声命周期(混合开启)

    1)说明文档: 2)效果演示: 3)代码演示:

  4. 生成元(Digit Generator ,ACM/ICPC Seoul 2005 ,UVa 1583)

    生成元:如果 x 加上 x 各个数字之和得到y,则说x是y的生成元. n(1<=n<=100000),求最小生成元,无解输出0. 例如:n=216 , 解是:198 198+1+9+8=2 ...

  5. iOS Xcode 调试技巧 全局断点这样加才有意思

    http://blog.sina.com.cn/s/blog_876a2c9901016ezh.html

  6. 新浪微博客户端(3)-封装UIBarButtonItem

    单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装. UIBarButtonItem+Extensio ...

  7. Base64封装类

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  8. [WPF学习笔记]动态加载XAML

    好久没写Blogs了,现在在看[WPF编程宝典],决定开始重新写博客,和大家一起分享技术. 在编程时我们常希望界面是动态的,可以随时变换而不需要重新编译自己的代码. 以下是动态加载XAML的一个事例代 ...

  9. Mate7微信指纹支付来了 比Touch ID整合微信早一点

    之前我们聊过微信将推指纹支付 "指付通"会与Touch ID整合吗这个话题,现在有国内厂商率先支持微信指纹支付,体验一下美国用户使用Apple Pay搭配Touch ID来实现便捷 ...

  10. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...