有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把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. 解决IE apk变成zip:Android 手机应用程序文件下载服务器Nginx+Tomcat配置解决方法

    APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Java ...

  2. DOM 元素 属性和方法

    console.dir() namespaceURI: "http://www.w3.org/1999/xhtml" nextElementSibling: null nextSi ...

  3. mysql 一个典型的数据库建表建用户过程

    cmd 命令不接 ";" mysql 命令后接 ";" 1.以管理员身份登录mysql mysql -u root –p 2.选择mysql数据库 use my ...

  4. JS面试题及答案总结

    1. 截取字符串abcdefg的efg  <div id="test">abcdefg</div> var mytext=document.getEleme ...

  5. 开源项目剖析之apache-common-pool

    前沿 该工程提供了对象池解决方案,该方案主要用于提高像文件句柄,数据库连接,socket通信这类大对象的调用效率.简单的说就是一种对象一次创建多次使用的技术. 整体结构 整个项目有三个包分别是org. ...

  6. LINUX下 my.cnf php.ini的位置

    my.cnf:/etc/ php.in:/usr/local/php/etc/

  7. linux学习笔记 2013-09-02

    1,解压一个tar.gz文件夹 tar -xvzf  filename.tar.gz 2,删除一个文件夹下所有的文件 rm -rf * 3,安装文件 sudo apt-get install XXX. ...

  8. Arbitrage(bellman_ford)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16652   Accepted: 7004 Descri ...

  9. Swift Tour 随笔总结 (3)

    关于Optional的Control Flow if let constantName = someOptional { statements } 如果该Optional为nil,则不进入if,否则执 ...

  10. Python字符串基础操作

    ==============字符串======== >>> s1='www.baidu.com' >>> type(s1) <type 'str'> & ...