Configure Tomcat access log formatting output

Source: Internet
Author: User
This document describes how to configure tomcat access logs and output the specified log format as required. In addition, in the Nginx + Tomcat configuration environment, how can Tomcat record the real IP addresses of visitors?. This article is original content. For more information, see JDiy official website.

Http://jdiy.net/read.jsp? Id = y0hab3qs03 in tomcat server. in the xml file, the host Host configuration area is configured as follows (in red): UnpackWARs = "true" autoDeploy = "true">
<! --... Some content is omitted... -->
<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> the directory specifies the log storage path, which is located in the logs directory of tomcat by default. For example, we can change it to: directory = "c: /wwwlogs "puts logs in the c: \ wwwlogs directory. The prefix and suffic are used to specify the prefix and suffix of the log file. Now let's take a look at the pattern configuration segment, which is used to specify the log output format. For the valid log format mode, see the following strings. The corresponding information is replaced by the specified response content:

% A-remote IP Address
% A-local IP Address
% B-number of bytes sent, excluding the HTTP header, or "-" If no Bytes are sent
% B-number of bytes sent, excluding the HTTP Header
% H-remote host name
% H-Request Protocol
% L (lower-case L)-remote logic from identd User Name (always return '-')
% M-Request Method
% P-local port
% Q-query string (Add "?" to the front If it exists, otherwise it is an empty string
% R-requirements of the first line
% S-response HTTP status code
% S-user session ID
% T-date and time, in common log format % u-remote user authentication
% U-requested URL path
% V-local server name
% D-request processing time (in milliseconds)
% T-request processing time (in seconds)
% I (uppercase I)-name of the thread in the current request

In addition, you can specify the following aliases to set them to one of the commonly used modes:

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 also write the query parameters, session variable values, cookie values, or variable values in the HTTP request/response header to the log file.
It resembles apache Syntax:

% {XXX} I xxx represents the incoming header (HTTP Request) % {XXX} o xxx represents the outgoing Response Header (Http Resonse)
% {XXX} c xxx represents a specific Cookie name
% {XXX} r xxx represents the ServletRequest property name % {XXX} s xxx represents the property name in HttpSessionExample:For example, when we set up a jsp serverNginx + TomcatIn this configuration, requests are forwarded by Nginx to Tomcat. When you need to record the real IP address information of the visitor in the tomcat log, we need to make a little difference from other special matches, otherwise, all the visitor IP addresses recorded by tomcat are 127.0.0.1, because all requests are forwarded by the Nginx front-end server, and the front-end server is 127.0.0.1 for tomcat. Next, let's take a look at how to let tomcat record the real IP address of the User:1. Configure the nginx forwarding IP header:Add proxy_set_header Host $ host in the Nginx server Host configuration section;
Proxy_set_header X-Real-IP $ remote_addr; Note: The above two lines are used to send Real remote host names and IP addresses to tomcat. Host represents the Host name, and X-Real-IP represents the Host IP address. These variables are case-insensitive for HTTP header content.2. Configure the Tomcat log to record the customer's real IP Address:To record the visitor's real IP address in Tomcat, refer to the tomcat log configuration syntax described above. You only need to add the following mode in log mode: % {X-Real-IP} I is the complete Tomcat log configuration section below: <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 the above two changes, you should restart the nginx and tomcat services for your modifications to take effect. In this way, when a new request comes in, you can record the visitor's real IP address in the Tomcat log file.

This article from: http://jdiy.net/read.jsp? Id = y0hab3qs03

 

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.