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

在Spring boot中使用了内嵌的tomcat,可以通过server.tomcat.accesslog配置tomcat 的access日志,这里就以Spring boot 1.5.3为例。

server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.

比较常用的有(省略了前缀server.tomcat.accesslog.):

  • enabled,取值true、false,需要accesslog时设置为true
  • directory,指定access文件的路径
  • pattern,定义日志的格式,后续详述
  • rotate,指定是否启用日志轮转。默认为true。这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,并且file-date-format参数也会被忽略

pattern的配置:

  • %a - Remote IP address,远程ip地址,注意不一定是原始ip地址,中间可能经过nginx等的转发
  • %A - Local IP address,本地ip
  • %b - Bytes sent, excluding HTTP headers, or '-' if no bytes were sent
  • %B - Bytes sent, excluding 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 '-')
  • %m - Request method,请求方法(GET,POST)
  • %p - Local port,接受请求的本地端口
  • %q - Query string (prepended with a '?' if it exists, otherwise an empty string
  • %r - First line of the request,HTTP请求的第一行(包括请求方法,请求的URI)
  • %s - HTTP status code of the response,HTTP的响应代码,如:200,404
  • %S - User session ID
  • %t - Date and time, in Common Log Format format,日期和时间,Common Log Format格式
  • %u - Remote user that was authenticated
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis,处理请求的时间,单位毫秒
  • %T - Time taken to process the request, in seconds,处理请求的时间,单位秒
  • %I - current Request thread name (can compare later with stacktraces),当前请求的线程名,可以和打印的log对比查找问题

Access log 也支持将cookie、header、session或者其他在ServletRequest中的对象信息打印到日志中,其配置遵循Apache配置的格式({xxx}指值的名称):

  • %{xxx}i for incoming headers,request header信息
  • %{xxx}o for outgoing response headers,response header信息
  • %{xxx}c for a specific cookie
  • %{xxx}r xxx is an attribute in the ServletRequest
  • %{xxx}s xxx is an attribute in the HttpSession
  • %{xxx}t xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)

Access log内置了两个日志格式模板,可以直接指定pattern为模板名称,如:

server.tomcat.accesslog.pattern=common
  • common - %h %l %u %t "%r" %s %b,依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数
  • combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i",依次为:远程主机名称,远程用户名,被认证的远程用户,日期和时间,请求的第一行,response code,发送的字节数,request header的Referer信息,request header的User-Agent信息。

除了内置的模板,我们常用的配置有:

  • %t %a "%r" %s (%D ms),日期和时间,请求来自的IP(不一定是原始IP),请求第一行,response code,响应时间(毫秒),样例:[21/Mar/2017:00:06:40 +0800] 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 63,这里请求来自IP就是经过本机的nginx转发的。
  • %t [%I] %{X-Forwarded-For}i %a %r %s (%D ms),日期和时间,线程名,原始IP,请求来自的IP(不一定是原始IP),请求第一行,response code,响应时间(毫秒),样例:[21/Apr/2017:00:24:40 +0800][http-nio-7001-exec-4] 10.125.15.1 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 5,这里的第一个IP是Nginx配置了X-Forwarded-For记录了原始IP。

这里简要介绍下上面用到的HTTP请求头X-Forwarded-For,它是一个 HTTP 扩展头部,用来表示 HTTP 请求端真实 IP,其格式为:X-Forwarded-For: client, proxy1, proxy2,其中的值通过一个逗号+空格把多个IP地址区分开,最左边(client)是最原始客户端的IP地址,代理服务器每成功收到一个请求,就把请求来源IP地址添加到右边。

springboot中配置tomcat的access log的更多相关文章

  1. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...

  2. 在Eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误

    在eclipse中配置Tomcat时,出现Cannot create a server using the selected type错误 原因:Tomcat被删除或者是重新安装,并且安装目录改变了. ...

  3. 在Eclipse中配置Tomcat 创建和运行Servlet/JSP

    在Eclipse中配置Tomcat 创建和运行Servlet/JSP 步骤一:在Eclipse中配置Tomcat(注意下载Eclipse IDE for Java EE Developers) (1) ...

  4. 如何在Eclipse中配置Tomcat

    1.Eclipse EE 配置Tomcat Eclipse EE 主要用于Java Web开发和J2EE项目开发.Eclipse EE中配置Tomcat比较简单,新建一个Tomcat Server即可 ...

  5. 如何在Eclipse中配置Tomcat(免安装版)

    如何在Eclipse中配置Tomcat(免安装版) 2013-10-09 23:19wgelgrsh | 分类:JAVA相关 | 浏览642次 分享到:   2013-10-10 17:10提问者采纳 ...

  6. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  7. eclipse中配置Tomcat服务器以及新建项目

    eclipse配置Tomcat服务器 http://jingyan.baidu.com/article/ca2d939dd90183eb6d31ce79.html eclipse中配置Tomcat服务 ...

  8. 手把手教你----MyEclipse中 配置 Tomcat

    电脑上配置Tomcatserver 安装Tomcat并配置环境变量 測试是否配置成功 MyEclipse中配置Tomcat 想要开发Java Web的程序.首先在MyEclipse中必须配置Tomca ...

  9. Eclipse中配置Tomcat容器

    Tomcat 安装与配置 Tomcat是Apache 软件基金会(Apache Software Foundation)核心项目之一,支持最新的Servlet 和JSP 规范.因为Tomcat 技术先 ...

随机推荐

  1. Android Studio环境下搭建ReactNative

    1.安装Android Studio首先肯定是 安装Android Studio(包含SDK)(国内推荐)ps:这里有一点要注意,需要为SDK配置环境变量,名称必须为ANDROID_HOME 2.安装 ...

  2. OpenCV 实现颜色直方图

    颜色直方图是在许多图像检索系统中被广泛采用的颜色特征.它所描述的是不同色彩在整幅图像中所占的比例,而并不关心每种色彩所处的空间位置,即无法描述图像中的对象或物体.颜色直方图特别适于描述那些难以进行自动 ...

  3. STL常用排序算法介绍

    merge()  以下是排序和通用算法:提供元素排序策略  merge: 合并两个有序序列,存放到另一个序列. #include <iostream> #include <cstdi ...

  4. 如何搭建modem编译环境

    [DESCRIPTION] (1)MT6577以及之前的chip平台(如MT6575,73等) 的modem编译环境和MTK的Feature Phone的编译环境一样,即Windows+RVCT (2 ...

  5. 【freeradius】使用radclient调试radius协议

    freeradius 自带了非常好的客户端程序 radtest, radclient 用来模拟和调试设备和通信过程.radtest多用于认证,radclient更为强大一些,认证,计费,coa都可以模 ...

  6. iOS9 系统分享调用(UIActivityViewController)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/49615109 ...

  7. HBase事务

    众所周知,ACID是指原子性(Atomicity),一致性(Consistency),隔离性(Isolation)和持久性(Durability). HBase对同一行数据的操作提供ACID保证.HB ...

  8. Mac OS 终端常用命令基础

    基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录" /" 下面,所以不在要有Windows 下的盘符概念.比如什么"C:"你在桌面上看到的硬盘都 ...

  9. miniUI Grid添加汇总行,Grid绑定数据,IDEA免编译设置

    坑1: 2017-6-5周二,上午解决了昨天摸索一下午的问题,使用miniui显示汇总行数据,要点有这么几个 在创建Grid div的时候一定要加上以下两个属性: //显示汇总行开关 showSumm ...

  10. jbpm 工作流(二)

    1           概述 本文主要介绍如何将JBPM+Struts+Spring+Hibernate整合在一块.并通过一个简单实例来说明.此实例为一个申请审批的简单流程,并将申请人和审批人记录到数 ...