Tomcat Access log configuration, logging Post request parameters (RPM)

Source: Internet
Author: User
Tags session id

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:

10.217.14.16--[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.

Filedateformat
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 127.0.0.1%a this is the IP of the server, in the log is 192.168.254.108%b send information bytes, not including the HTTP header, if the number of bytes is 0, display as-%B the number of bytes to send the message, excluding the HTTP header. The name of the%h server. If Resolvehosts is false, here is the IP address, for example, my log is 10.217.14.16%h Visitor's agreement, here is Http/1.0%l official explanation: Remote logical username from Identd (Possible translation: Record the name provided by the browser for authentication) (always returns '-'))%m access mode, is GET or post%p locally received port for access%q such as you are visiting is AAA.JSP?BBB=CCC, then this is shown here? bbb=CCC, that's what querystring means.%R first line of the requested method and URL for the request (method and request URI)%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 has been authenticated by the visitor, otherwise is "-"%u access URL address, I here is/rightmainima/leftbott4.swf%V Server name, maybe that's what you wrote in the URL, I'm localhost here.%D time taken to process the Request,in Millis, request consumption in milliseconds%T time taken to process the request,in seconds, request consumption in seconds

Attached: Reference Official document: Http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html

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 do I do when I 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.

 PackageCom.xiaoxiliuImportjava.io.IOException;Importjava.util.Enumeration;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory; Public Final classPostdatadumperfilterImplementsFilter {Logger Logger=Loggerfactory.getlogger (GetClass ()); PrivateFilterconfig Filterconfig =NULL;  Public voiddestroy () { This. Filterconfig =NULL; }     Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {if(Filterconfig = =NULL)            return; Enumeration<String> names =Request.getparameternames (); StringBuilder Output=NewStringBuilder ();  while(Names.hasmoreelements ()) {String name=(String) names.nextelement (); Output.append (name). Append ("="); String values[]=request.getparametervalues (name);  for(inti = 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 voidInit (Filterconfig filterconfig)throwsservletexception { This. Filterconfig =Filterconfig; }}

Add a configuration to the filter in Web. xml:

<filter>        <filter-name>post-data-dumper-filter</filter-name>        <filter-class >com.xiaoxiliu.postdatadumperfilter</filter-class>    </filter>    < filter-mapping>        <filter-name>post-data-dumper-filter</filter-name>        <url-pattern> /* </url-pattern>    </filter-mapping>

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.

Original address:

http://blog.csdn.net/qq_30121245/article/details/52861935

Tomcat Access log configuration, logging Post request parameters (RPM)

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: info-contact@alibabacloud.com 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.