Configure Tomcat's access log formatted output

Source: Internet
Author: User
Tags session id nginx server tomcat server

Turn from: http://goon.iteye.com/blog/1814609

This article describes how to configure a Tomcat access log to output the specified log format as we request.and in the Nginx+tomcat configuration environment, how to let Tomcat record the true IP address of the visitor。 This article for the original content, reproduced please specify the Source: Jdiy official website Http://jdiy.net/read.jsp?id=y0hab3qs03 in Tomcat Server.xml file, host host configuration area found similar to the following (red part) Is the configuration of the access log: Unpackwars= "true" autodeploy= "true" > <!--... Some of the content is slightly. -->
<valve classname= "Org.apache.catalina.valves.AccessLogValve" directory= "Logs"
Prefix= "Localhost_access_log." suffix= ". txt"
pattern= "%h%l%u%t &quot;%r&quot; %s%b "/>
</Host> directory is used to specify the path of the log to be stored by default in Tomcat's logs directory, such as we can modify to: directory= "c:/wwwlogs" to place the log in the C:\wwwlogs directory. The prefix and suffic are used to specify the prefix and suffix of the log file, and I don't need to say more. Now let's take a look at the pattern configuration segment, which specifies the output format for the log. A valid log format pattern can be seen below, with the following string whose corresponding information is replaced by the specified response content:

%a-Remote IP address
%a-Local IP address
%b-The number of bytes sent, excluding HTTP headers, or "-" if no bytes were sent
%b-The number of bytes sent, excluding HTTP headers
%H-Remote Host name
%H-Request Protocol
%l (lowercase L)-remote logic from IDENTD username (always return '-')
%m-Request Method
%p-Local Port
%q-The query string, preceded by a. "If it exists, otherwise it is an empty string
%r-Requirements for the first line
%s-HTTP status code for the response
%s-user session ID
%t-Date and time, in common log format%u-Remote user authentication
%u-URL path for request
%V-Local server name
%d-time (in milliseconds) to process the request
%t-the time (in seconds) to process the request
%i (uppercase I)-thread name of the current request

In addition, you can specify the following aliases to be set to one of the universally used patterns:
Common-%h%l%u%t "%r"%s%b
Combined-%h%l%u%t "%r"%s%b "%{referer}i" "%{user-agent}i"

In addition, you can write to the log file the query parameters for the request, the session variable value, the cookie value, or the variable value of the HTTP request/response header content.
It's modeled after the Apache syntax:

%{xxx}i XXX Represents the incoming header (HTTP Request)%{xxx}o XXX on behalf of the outgoing response header (HTTP resonse)
%{xxx}c XXX represents a specific cookie name
%{xxx}r XXX Represents the name of the property in HttpSession on behalf of the ServletRequest property name%{xxx}s XXXAn example is provided:For example, when we set up a JSP server, we useNginx+tomcatIn this configuration, the request is forwarded to Tomcat by Nginx, and when it is necessary to record the visitor's true IP address information in the Tomcat log, we need to do a little bit different from the other special match, otherwise the Tomcat's guest IP is all 127.0.0.1,  This is because all requests are forwarded by the Nginx front-end server, and the front-end server is 127.0.0.1 for Tomcat. Now let's take a look at how to get Tomcat to record the user's real IP address:First , configure Nginx forwarding IP header:Add in Nginx Server Host configuration segment: Proxy_set_header host $host;
Proxy_set_header X-real-ip $remote _addr; Description: The above two lines are used to send the true remote host name and IP address to Tomcat. Where the host represents the hostname, X-REAL-IP represents the host IP, and for HTTP header content, these variables are case-insensitive.Second, configure Tomcat log to record customer real IP: To record the real IP of the visitor in Tomcat, we refer to the Tomcat log configuration syntax described above, just add the following pattern to the log mode:%{x-real-ip}i as follows the complete Tomcat log configuration section: <valve classname= " Org.apache.catalina.valves.AccessLogValve "  directory=" c:/wwwlogs/"prefix=" cluster. "suffix=". Log "pattern=" %{x-real-ip}i %u%t%r%s%b "resolvehosts= false"/>   Note: After 22 modifications, you should restart the Nginx and Tomcat service to make your changes take effect. This way, when a new request comes in, the real IP address of the visitor can be recorded in the Tomcat log file.   ===================================== above for the reprint of the local configuration (without nginx): server.xml:  <valve classname= " Org.apache.catalina.valves.AccessLogValve "directory=" logs "                & Nbsp;prefix= "Localhost_access_log." suffix= ". txt"                  pattern= "%h%l%u%t%r%s%b%d%{cookie}i%{user-agent}i%{a}r"                  res Olvehosts= "false"/> Output: 127.0.0.1--[25/feb/2013:11:57:29 +0800] get/app_back/index.do?a=12345 http/1.1 200 59847 68 8 jsessionid=5f171789ddf29ab377f68b52bb18884e; Mysessionid=3af66463-79a3-4b6c-a4c5-e7cd39a0aead mozilla/5.0 (Windows NT 5.1; rv:16.0) gecko/20100101 Firefox/16.0-where Parameter A in the URL can be displayed in the URL, but the parameters in the request submitted by post cannot be logged.

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.