有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把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. 深入理解 Javascript 面向对象编程

    一:理解构造函数原型(prototype)机制 prototype是javascript实现与管理继承的一种机制,也是面向对象的设计思想.构造函数的原型存储着引用对象的一个指针,该指针指向与一个原型对 ...

  2. JSP 使用

    JSP教程: http://www.w3cschool.cc/jsp/jsp-tutorial.html jsp语法: 任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依 ...

  3. 浏览器的中的 XMLHttpRequest 对象的使用

    使用XMLHttpRequest浏览器对象(IE5.IE6中是ActiveXObject对象)可以实现异步请求,Ajax就是以此为基础进行的封装. 1.同步与异步: <script type=& ...

  4. The Joys of Conjugate Priors

    The Joys of Conjugate Priors (Warning: this post is a bit technical.) Suppose you are a Bayesian rea ...

  5. Java中使用split、sort函数

    public static void main(String[] args) { // TODO Auto-generated method stub String str = null ; Scan ...

  6. SQLServer 删除所有表和删除所有存储过程

    1.删除所有表 use 数据库declare @tname varchar(8000)set @tname=''select @tname=@tname + Name + ',' from sysob ...

  7. Java JNI 编程进阶 实例+c++数据类型与jni数据类型转换

    原文:http://www.iteye.com/topic/295776 JNI一直以来都很少去关注,但却是我心中的一个结,最近这几天刚好手头有点时间,因此抽空看了一下这方面的东西,整理了一份文档,J ...

  8. apue第四章学习总结

    apue第四章学习总结 4.1.若以stat函数去替换lstat函数,会发生: 原来的目录路径: $:~/workspace/apue2/include$ ls -l apue.h abc lrwxr ...

  9. POJ 1837 Balance

    Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9240 Accepted: 5670 Description G ...

  10. 完美串(区间dp)

    完美串 Description 爱美之心人皆有之,GG也不例外.所以GG他对于完美串有一种热衷的爱.在GG眼中完美串是一个具有无比魅力的01子串.这个子串有之其魅力之处,对它取反后水平翻转,它又和它原 ...