📄 extendedaccesslogvalve.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/valves/ExtendedAccessLogValve.java,v 1.7 2004/01/26 19:48:18 remm Exp $
* $Revision: 1.7 $
* $Date: 2004/01/26 19:48:18 $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.catalina.valves;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TimeZone;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.HttpResponse;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Request;
import org.apache.catalina.Response;
import org.apache.catalina.ValveContext;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.catalina.util.ServerInfo;
import org.apache.catalina.util.StringManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* An implementation of the W3c Extended Log File Format. See
* http://www.w3.org/TR/WD-logfile.html for more information about the format.
*
* The following fields are supported:
* <ul>
* <li><code>c-dns</code>: Client hostname</li>
* <li><code>c-ip</code>: Client ip address</li>
* <li><code>bytes</code>: bytes served</li>
* <li><code>cs-method</code>: request method</li>
* <li><code>cs-uri</code>: The full uri requested</li>
* <li><code>cs-uri-query</code>: The query string</li>
* <li><code>cs-uri-stem</code>: The uri without query string</li>
* <li><code>date</code>: The date in yyyy-mm-dd format for GMT</li>
* <li><code>s-dns</code>: The server dns entry </li>
* <li><code>s-ip</code>: The server ip address</li>
* <li><code>cs(XXX)</code>: The value of header XXX from client to server</li>
* <li><code>sc(XXX)</code>: The value of header XXX from server to client </li>
* <li><code>sc-status</code>: The status code</li>
* <li><code>time</code>: Time the request was served</li>
* <li><code>time-taken</code>: Time (in seconds) taken to serve the request</li>
* <li><code>x-A(XXX)</code>: Pull XXX attribute from the servlet context </li>
* <li><code>x-C(XXX)</code>: Pull the first cookie of the name XXX </li>
* <li><code>x-R(XXX)</code>: Pull XXX attribute from the servlet request </li>
* <li><code>x-S(XXX)</code>: Pull XXX attribute from the session </li>
* <li><code>x-P(...)</code>: Call request.getParameter(...)
* and URLencode it. Helpful to capture
* certain POST parameters.
* </li>
* <li>For any of the x-H(...) the following method will be called from the
* HttpServletRequestObject </li>
* <li><code>x-H(authType)</code>: getAuthType </li>
* <li><code>x-H(characterEncoding)</code>: getCharacterEncoding </li>
* <li><code>x-H(contentLength)</code>: getContentLength </li>
* <li><code>x-H(locale)</code>: getLocale</li>
* <li><code>x-H(protocol)</code>: getProtocol </li>
* <li><code>x-H(remoteUser)</code>: getRemoteUser</li>
* <li><code>x-H(requestedSessionId)</code>: getGequestedSessionId</li>
* <li><code>x-H(requestedSessionIdFromCookie)</code>:
* isRequestedSessionIdFromCookie </li>
* <li><code>x-H(requestedSessionIdValid)</code>:
* isRequestedSessionIdValid</li>
* <li><code>x-H(scheme)</code>: getScheme</li>
* <li><code>x-H(secure)</code>: isSecure</li>
* </ul>
*
*
*
* <p>
* Log rotation can be on or off. This is dictated by the rotatable
* property.
* </p>
*
* <p>
* For UNIX users, another field called <code>checkExists</code>is also
* available. If set to true, the log file's existence will be checked before
* each logging. This way an external log rotator can move the file
* somewhere and tomcat will start with a new file.
* </p>
*
* <p>
* For JMX junkies, a public method called </code>rotate</code> has
* been made available to allow you to tell this instance to move
* the existing log file to somewhere else start writing a new log file.
* </p>
*
* <p>
* Conditional logging is also supported. This can be done with the
* <code>condition</code> property.
* If the value returned from ServletRequest.getAttribute(condition)
* yields a non-null value. The logging will be skipped.
* </p>
*
* <p>
* For extended attributes coming from a getAttribute() call,
* it is you responsibility to ensure there are no newline or
* control characters.
* </p>
*
*
* @author Tim Funk
* @version $Revision: 1.7 $ $Date: 2004/01/26 19:48:18 $
*/
public final class ExtendedAccessLogValve
extends ValveBase
implements Lifecycle {
// ----------------------------------------------------------- Constructors
/**
* Construct a new instance of this class with default property values.
*/
public ExtendedAccessLogValve() {
super();
}
// ----------------------------------------------------- Instance Variables
private static Log log = LogFactory.getLog(ExtendedAccessLogValve.class);
/**
* The descriptive information about this implementation.
*/
protected static final String info =
"org.apache.catalina.valves.ExtendedAccessLogValve/1.0";
/**
* The lifecycle event support for this component.
*/
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
/**
* The string manager for this package.
*/
private StringManager sm =
StringManager.getManager(Constants.Package);
/**
* Has this component been started yet?
*/
private boolean started = false;
/**
* The as-of date for the currently open log file, or a zero-length
* string if there is no open log file.
*/
private String dateStamp = "";
/**
* The PrintWriter to which we are currently logging, if any.
*/
private PrintWriter writer = null;
/**
* The formatter for the date contained in the file name.
*/
private SimpleDateFormat fileDateFormatter = null;
/**
* A date formatter to format a Date into a date in the format
* "yyyy-MM-dd".
*/
private SimpleDateFormat dateFormatter = null;
/**
* A date formatter to format a Date into a time in the format
* "kk:mm:ss" (kk is a 24-hour representation of the hour).
*/
private SimpleDateFormat timeFormatter = null;
/**
* Time taken formatter for 3 decimal places.
*/
private DecimalFormat timeTakenFormatter = null;
/**
* My ip address. Look it up once and remember it. Dump this if we can
* determine another reliable way to get server ip address since this
* server may have many ip's.
*/
private String myIpAddress = null;
/**
* My dns name. Look it up once and remember it. Dump this if we can
* determine another reliable way to get server name address since this
* server may have many ip's.
*/
private String myDNSName = null;
/**
* Holder for all of the fields to log after the pattern is decoded.
*/
private FieldInfo[] fieldInfos;
/**
* The current log file we are writing to. Helpful when checkExists
* is true.
*/
private File currentLogFile = null;
/**
* The system time when we last updated the Date that this valve
* uses for log lines.
*/
private Date currentDate = null;
/**
* Instant when the log daily rotation was last checked.
*/
private long rotationLastChecked = 0L;
/**
* The directory in which log files are created.
*/
private String directory = "logs";
/**
* The pattern used to format our access log lines.
*/
private String pattern = null;
/**
* The prefix that is added to log file filenames.
*/
private String prefix = "access_log.";
/**
* Should we rotate our log file? Default is true (like old behavior)
*/
private boolean rotatable = true;
/**
* The suffix that is added to log file filenames.
*/
private String suffix = "";
/**
* Are we doing conditional logging. default false.
*/
private String condition = null;
/**
* Do we check for log file existence? Helpful if an external
* agent renames the log file so we can automagically recreate it.
*/
private boolean checkExists = false;
/**
* Date format to place in log file name. Use at your own risk!
*/
private String fileDateFormat = null;
// ------------------------------------------------------------- Properties
/**
* Return the directory in which we create log files.
*/
public String getDirectory() {
return (directory);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -