Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of tomcat accessing access Log configuration and recording Post request parameters

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you tomcat access access log configuration and record Post request parameters example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

I. configuration and explanation

Tomcat access log format configuration, which is added under the Host tag in config/server.xml

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:

The official className document says: This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve. The directory where the directory log files are stored. It is usually set to the existing logs file under tomcat. The name prefix of the prefix log file. The name suffix of the suffix log file. The most important parameter of pattern. I'll talk about it in more detail below. ResolveHosts translates the server IP address to the host name via DNS if it is true,tomcat; if it is false, write the server IP address directly. Default false. By default, the file name generated by rotatable is prefix (prefix) +. + time (usually calculated by day) +. + suffix (suffix), such as: localhost_access_log.2007-09-22.txt. If set to false, tomcat ignores time and does not generate a new file, which is named localhost_access_log.txt. In the long run, the log file will be super large condition this parameter is not very practical, you can set any value, such as set to condition= "tkq", then the log will only be recorded if ServletRequest.getAttribute ("tkq") is empty.

FileDateFormat, as its name implies, is the time format. However, this time format works for log file names. We generated the full name of the log file: localhost_access_log.2016-09-22.txt, which is how 2016-09-22 came from. 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, you can also generate it in minutes. Change it yourself ^ _ ^

Let's focus on pattern. It has many parameters. Can be set to 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 to why the last two values of combined are like this, I'm not sure)

% a this is the IP that records the visitor. In the log, it is 127.0.0.1% A, which is the IP of the local server. In the log, it is 192.168.254.108% b bytes sent, excluding http headers. If the number of bytes is 0, it is shown as the number of bytes sent by -% B, excluding http headers. Name of the h server. If resolveHosts is false, here is the IP address. For example, in my log is the agreement of 10.217.14.16% H visitors. Here is the official explanation of HTTP/1.0% l: Remote logical username from identd (possible translation: record the name provided by the viewer for authentication) (always returns'-')% m access method Is it GET or POST% p local access port% Q for example, if you are accessing aaa.jsp?bbb=ccc, then the bbb=ccc is shown here, which means% r First line of the request (method and request URI) request method and URL% s http response status code% S user's session ID. You can also check this session ID for a detailed explanation. Anyway, each time will generate a different session ID% t request time% u authenticated visitors, otherwise it is the "-"% U access URL address, I here is / rightmainima/leftbott4.swf% v server name, may be the one written in your url, I here is localhost% D Time taken to process the request,in millis, the request time spent, in milliseconds to record% T Time taken to process the request,in seconds The time consumed by the request, recorded in seconds

Attached: refer to the official document: http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html

2. Configure print POST parameters

In addition, the% r parameter prints out the requested url and get parameters. If url specifies that the access method is post,post, the parameters cannot be printed. What should I do when I need to print post parameters?

Notice that I started with an example of% {postdata} r in the Valve configuration. Yes, this patterrn in combined format can be implemented. But configuring this thing in valve is not enough. Because postdata allows us to customize the parameter name. You need to set this value in request. The code to set postdata to request is attached below.

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;}}

Add and configure the filter in web.xml:

Post-data-dumper-filter com.xiaoxiliu.PostDataDumperFilter post-data-dumper-filter / *

Third, query the interfaces that take the most time to access

This will use the omnipotent awk to show the access time shown in the penultimate column of our log. Cat logs/localhost_access_log.2016-10-25.txt | awk'{print $(NF-1) "" $0}'| sort-n-r | awk'{$1 = "; print $0} 'displays the interface and access time from largest to smallest in the penultimate column. In this way, we can find out which excuses are more time-consuming, and then optimize them to improve the user experience.

The above is all the contents of the article "sample Analysis of tomcat access access Log configuration and logging Post request parameters". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report