tomcat post html,Tomcat的访问日志-localhost_access_log和记录Post请求参数

tomcat的日志分类

http://blog.csdn.net/fly910905/article/details/78463909html

tomcat产生的访问日志数据  【localhost_access_log.Y-M-D.txt】

它记录的访问的时间,IP,访问的资料等相关信息java

首先是配置tomcat访问日志数据,配置的方式以下

Valve参数说明:

className

官方文档上说了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。

directory

日志文件存放的目录。一般设置为tomcat下已有的那个logs文件。

prefix

日志文件的名称前缀。

suffix

日志文件的名称后缀。

pattern

最主要的参数。

pattern。它的参数比较多。能够设置成common,combined两种格式。

common的值:%h %l %u %t %r %s %b

combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i   web

resolveHosts

若是是true,tomcat会将这个服务器IP地址经过DNS转换为主机名;若是是false,就直接写服务器IP地址。默认false。

rotatable

默认为true,tomcat生成的文件名为prefix(前缀)+.+时间(通常是按天算)+.+suffix(后缀),如:localhost_access_log.2007-09-22.txt。设置为false的话,tomcat会忽略时间,不会生成新文件,文件名就是:localhost_access_log.txt。久而久之,这个日志文件会超级大

condition

这个参数不太实用,可设置任何值,好比设置成condition="tkq",那么只有当ServletRequest.getAttribute("tkq")为空的时候,该条日志才会被记录下来。

fileDateFormat

顾名思义,就是时间格式嘛。但这个时间格式是针对日志文件名起做用的。我们生成的日志文件全名:localhost_access_log.2016-09-22.txt,这里面的2016-09-22就是这么来的。若是想让tomcat每小时生成一个日志文件,也很简单,将这个值设置为:fileDateFormat="yyyy-MM-dd.HH",固然也能够按分钟生成什么的,本身改改吧^_^

打开${catalina}/conf/server.xml文件

注:${catalina}是tomcat的安装目录

prefix="localhost_access_log" suffix=".txt"

pattern="%h %l %u %t "%r" %s %b" />

tomcat访问日志关闭:

只须要将${catalina}/conf/server.xml里面的下列申明注释掉再重启tomcat就好apache

具体的日志产生样式说明以下(从官方文档中摘录) * %a - Remote IP address // 这是记录访问者的IP,在日志里是127.0.0.1

* %A - Local IP address // 这是记录本地服务器的IP,在日志里是192.168.254.108

* %b - Bytes sent, excluding HTTP headers, or '-' if zero // 发送信息的字节数,不包括http头,若是字节数为0的话,显示为-

* %B - Bytes sent, excluding HTTP headers // 发送信息的字节数,不包括http头。

* %h - Remote host name (or IP address if resolveHosts is false) //服务器的名称。若是resolveHosts为false的话,这里就是IP地址了,例如个人日志里是10.217.14.16

* %H - Request protocol //访问者的协议,这里是HTTP/1.0

* %l - Remote logical username from identd (always returns '-') // 官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns '-')

* %m - Request method (GET, POST, etc.) // 访问的方式,是GET仍是POST

* %p - Local port on which this request was received // 本地接收访问的端口

* %q - Query string (prepended with a '?' if it exists) // 好比你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,就是querystring的意思

* %r - First line of the request (method and request URI) // First line of the request (method and request URI) 请求的方法和URL

* %s - HTTP status code of the response // http的响应状态码

* %S - User session ID // 用户的session ID,这个session ID你们能够另外查一下详细的解释,反正每次都会生成不一样的session ID

* %t - Date and time, in Common Log Format // 请求时间

* %u - Remote user that was authenticated (if any), else '-' // 获得了验证的访问者,不然就是"-"

* %U - Requested URL path // 访问的URL地址,我这里是/rightmainima/leftbott4.swf

* %v - Local server name // 服务器名称,可能就是你url里面写的那个吧,我这里是localhost

* %D - Time taken to process the request, in millis // 请求消耗的时间,以毫秒记

* %T - Time taken to process the request, in seconds //请求消耗的时间,以秒记

There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.

It is modeled after the apache syntax:

* %{xxx}i for incoming headers

* %{xxx}c for a specific cookie

* %{xxx}r xxx is an attribute in the ServletRequest

* %{xxx}s xxx is an attribute in the HttpSession

示例【格式为 %h %l %u %t "%r" %s %b】 140.205.201.39 - - [31/Aug/2017:17:06:52 +0800] "GET /clusters.jsf HTTP/1.1" 404 -

60.191.38.77 - - [31/Aug/2017:17:11:35 +0800] "GET / HTTP/1.1" 404 -

61.232.253.54 - - [31/Aug/2017:19:04:53 +0800] "GET / HTTP/1.1" 404 -

100.116.151.190 - - [31/Aug/2017:20:45:17 +0800] "GET /CloudPayment/bill/querySelfPayList.do HTTP/1.0" 200 60

100.116.41.2 - - [31/Aug/2017:20:45:34 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

100.116.151.145 - - [31/Aug/2017:20:46:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

100.116.41.41 - - [31/Aug/2017:20:46:42 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

100.116.151.181 - - [31/Aug/2017:20:47:21 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

100.116.151.181 - - [31/Aug/2017:20:47:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

记录Post请求参数

另外%r参数能打印出请求的url和get参数。若是url指定访问方式是post,post的参数是打印不出来的。当须要打印post参数,该怎么办?tomcat

server.xml配置

tomcat访问日志格式配置,在config/server.xml里Host标签下加上服务器

prefix="localhost_access_log."suffix=".txt"session

pattern="%h %l %u %t "%r" [%{postdata}r] %s %{Referer}i %{User-Agent}i %T %b"/>app

postdata过滤器

package com.xiaoxiliu

import java.io.IOException;

import java.util.Enumeration;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public final class PostDataDumperFilter implements Filter {

Logger logger = LoggerFactory.getLogger(getClass());

private FilterConfig filterConfig = null;

public void destroy() {

this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

if (filterConfig == null)

return;

Enumeration names = request.getParameterNames();

StringBuilder output = new StringBuilder();

while (names.hasMoreElements()) {

String name = (String) names.nextElement();

output.append(name).append("=");

String values[] = request.getParameterValues(name);

for (int i = 0; i < values.length; i++) {

if (i > 0) {

output.append("' ");

}

output.append(values[i]);

}

if (names.hasMoreElements())

output.append("&");

}

request.setAttribute("postdata", output);

logger.debug("postdata: " + output);

chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

}

在web.xml中添加配置该filter

post-data-dumper-filter

com.xiaoxiliu.PostDataDumperFilter

post-data-dumper-filter

/*

查询访问最耗时的接口

这就要用到万能的awk了 咱们的日志倒数第二列显示的访问时间。jsp

cat logs/localhost_access_log.2016-10-25.txt | awk '{print $(NF-1)" "$0}' | sort -n -r| awk '{$1="";print $0}'  按照倒数第二列由大到小显示接口以及访问时间。

这样咱们就能找出那些借口耗时较大,而后对其进行优化,提升用户体验。

参考来源:http://blog.csdn.net/qq_30121245/article/details/52861935

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值