📄 extendedaccesslogvalve.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.http.Cookie;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.catalina.util.ServerInfo;
import org.apache.catalina.util.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.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: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public 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);
}
/**
* Set the directory in which we create log files.
*
* @param directory The new log file directory
*/
public void setDirectory(String directory) {
this.directory = directory;
}
/**
* Return descriptive information about this implementation.
*/
public String getInfo() {
return (info);
}
/**
* Return the format pattern.
*/
public String getPattern() {
return (this.pattern);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -