Tomcat Access log configuration, logging Post request parameters

Source: Internet
Author: User
Tags session id tomcat

First, configuration and description

Tomcat access log format configuration, under the host tag in Config/server.xml Plus

<valve classname= "Org.apache.catalina.valves.AccessLogValve" directory= "Logs"
prefix= "Localhost_access_log" suffix= ". txt"
pattern= "%h%l%u%t &quot;%r&quot; [%{postdata}r]%s%{referer}i%{user-agent}i%T%b "/>

We will see the following text in the log file:[21/oct/2016:15:48:54 +0800] "Post/updates/related_notice_num.json http/1.0" [Channel=app Store& gid=92391918-2173-4a66-8b31-b2fb3f8fb3df&os=2&plat=2&sver=10.000000&token= MZM1OTC0MJQ1MKB3ZWLIBY55BXGUY29TFHDLAWJVFDQ5ZGFMMJK0YJQ5YWQXMTZIZJBMYWM4ZDDHYZG3ZWQ0&UA=&VER=4.2.1] 200 -allapp/4.2.1 (IPhone; IOS 10.0.2; scale/3.00) 0.004 91

Parameter description:

ClassName The official document says: This must is set to Org.apache.catalina.valves.AccessLogValve to use the default access log valve.
Directory directory where log files are stored. Usually set to the logs file that is already in Tomcat.
Prefix The name prefix of the log file.
Suffix The name suffix of the log file.
Pattern The most important parameter. The following will be said in detail.
Resolvehosts If it is true,tomcat, the server IP address is converted to the hostname via DNS, and if False, the server IP address is written directly. False by default.
rotatable The default True,tomcat generated file name is prefix (prefix) +.+ time (typically by day) +.+suffix (suffix), such as: Localhost_access_log.2007-09-22.txt. If set to False, Tomcat ignores the time, does not generate a new file, and the file name is: Localhost_access_log.txt. This log file will be super large.
Condition This parameter is not very practical, can set any value, such as set to Condition= "TKQ", then only when Servletrequest.getattribute ("TKQ") is empty, the log will be recorded.

As the name implies, it is the time format. However, this time format works for the log file name. The full name of the log file we generated: Localhost_access_log.2016-09-22.txt, that was the 2016-09-22. If you want Tomcat to generate a log file every hour, it is also very simple, set this value to: filedateformat= "yyyy-mm-dd.hh", of course, can also be generated by the minute, you change it ^_^

Below is the emphasis on pattern. It has more parameters. Can be set into common,combined two formats.

Value of common:%h%l%u%t%r%s%b
Combined value:%h%l%u%t%r%s%b%{referer}i%{user-agent}i (as for the last two values of combined why, I'm not sure)

%a This is the record of the visitor's IP, in the log is
%a This is the IP of the server, in the log is
%b the number of bytes sent, not including the HTTP header, if the number of bytes is 0, displayed as-
%B the number of bytes to send information, excluding HTTP headers.
the name of the%h server. If Resolvehosts is false, here is the IP address, for example, my log is
%H Visitor's agreement, here is http/1.0
%l official explanation: Remote logical   Username from Identd (this can be translated: the name provided when the browser is authenticated) (always returns '-')
%m access, is the Get or post
%p locally received port of access
%q For example, if you are visiting AAA.JSP?BBB=CCC, then here is the BBB=CCC, which is what QueryString means.%r first line of the
request (method and request URI)    and URL
%s HTTP response status code
%s User's session ID, this session ID you can also check the detailed explanation, anyway each time will generate a different session ID
%t request time
%u Get authenticated visitor, otherwise is "-"
%u access URL address, I here is/rightmainima/leftbott4.swf
%v server name, may be your URL written in it, I here is localhost<   c16/>%d Time taken to process the Request,in Millis, the request consumed in milliseconds
%T time taken to process the request,in seconds, request consumed in seconds Remember
Attached: Reference Official document: Http://

Second, configure the print post parameters

In addition, the%r parameter can print out the requested URL and get parameters. If the URL specifies that the access method is Post,post, the parameter is not printed. What to do if you need to print the post parameters.

It is noted that I have the opening example of the%{postdata}r in valve configuration. Yes, the PATTERRN of this combined format can be implemented. But it's not enough to configure this stuff in valve. Because PostData gives us a custom parameter name. You need to set this value in Request. The code that sets PostData to request is attached below.

Package Com.xiaoxiliu import;

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, SERVL

		etexception {if (filterconfig = = null) return;
		enumeration<string> 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; }

Add a configuration to the filter in Web. xml:

		<filter-class> com.xiaoxiliu.postdatadumperfilter</filter-class>

iii. query access to the most time-consuming interface

This is going to use the almighty awk. The second column of our log shows the access time. Cat Logs/localhost_access_log.2016-10-25.txt | awk ' {print $ (NF-1) ' "$} ' | Sort-n-r| awk ' {$1= ' ";p rint $} ' displays the interface and access time by the second-to-last column from the large to the small. This way we can find out which excuses are time consuming and then optimize them to improve the user experience.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.