📄 extendedaccesslogvalve.java
字号:
return wrap(r.getHeader(fieldInfo.value));
default:
;
}
return "-";
}
/**
* Get app specific data.
* @param fieldInfo The field to decode
* @param request Where we will pull the data from.
* @return The appropriate value
*/
private String getAppSpecific(FieldInfo fieldInfo, Request request) {
ServletRequest sr = request.getRequest();
HttpServletRequest hsr = null;
if (sr instanceof HttpServletRequest)
hsr = (HttpServletRequest)sr;
switch(fieldInfo.xType) {
case FieldInfo.X_PARAMETER:
return wrap(urlEncode(sr.getParameter(fieldInfo.value)));
case FieldInfo.X_REQUEST:
return wrap(sr.getAttribute(fieldInfo.value));
case FieldInfo.X_SESSION:
HttpSession session = null;
if (hsr!=null){
session = hsr.getSession(false);
if (session!=null)
return wrap(session.getAttribute(fieldInfo.value));
}
break;
case FieldInfo.X_COOKIE:
Cookie[] c = hsr.getCookies();
for (int i=0; c != null && i < c.length; i++){
if (fieldInfo.value.equals(c[i].getName())){
return wrap(c[i].getValue());
}
}
case FieldInfo.X_APP:
return wrap(request.getContext().getServletContext()
.getAttribute(fieldInfo.value));
case FieldInfo.X_SERVLET_REQUEST:
if (fieldInfo.location==FieldInfo.X_LOC_AUTHTYPE) {
return wrap(hsr.getAuthType());
} else if (fieldInfo.location==FieldInfo.X_LOC_REMOTEUSER) {
return wrap(hsr.getRemoteUser());
} else if (fieldInfo.location==
FieldInfo.X_LOC_REQUESTEDSESSIONID) {
return wrap(hsr.getRequestedSessionId());
} else if (fieldInfo.location==
FieldInfo.X_LOC_REQUESTEDSESSIONIDFROMCOOKIE) {
return wrap(""+hsr.isRequestedSessionIdFromCookie());
} else if (fieldInfo.location==
FieldInfo.X_LOC_REQUESTEDSESSIONIDVALID) {
return wrap(""+hsr.isRequestedSessionIdValid());
} else if (fieldInfo.location==FieldInfo.X_LOC_CONTENTLENGTH) {
return wrap(""+hsr.getContentLength());
} else if (fieldInfo.location==
FieldInfo.X_LOC_CHARACTERENCODING) {
return wrap(hsr.getCharacterEncoding());
} else if (fieldInfo.location==FieldInfo.X_LOC_LOCALE) {
return wrap(hsr.getLocale());
} else if (fieldInfo.location==FieldInfo.X_LOC_PROTOCOL) {
return wrap(hsr.getProtocol());
} else if (fieldInfo.location==FieldInfo.X_LOC_SCHEME) {
return wrap(hsr.getScheme());
} else if (fieldInfo.location==FieldInfo.X_LOC_SECURE) {
return wrap(""+hsr.isSecure());
}
break;
default:
;
}
return "-";
}
/**
* urlEncode the given string. If null or empty, return null.
*/
private String urlEncode(String value) {
if (null==value || value.length()==0) {
return null;
}
return URLEncoder.encode(value);
}
/**
* Wrap the incoming value into quotes and escape any inner
* quotes with double quotes.
*
* @param value - The value to wrap quotes around
* @return '-' if empty of null. Otherwise, toString() will
* be called on the object and the value will be wrapped
* in quotes and any quotes will be escaped with 2
* sets of quotes.
*/
private String wrap(Object value) {
String svalue;
// Does the value contain a " ? If so must encode it
if (value==null || "-".equals(value))
return "-";
try {
svalue = value.toString();
if ("".equals(svalue))
return "-";
} catch(Throwable e){
/* Log error */
return "-";
}
/* Wrap all quotes in double quotes. */
StringBuffer buffer = new StringBuffer(svalue.length()+2);
buffer.append('"');
int i=0;
while (i<svalue.length()) {
int j = svalue.indexOf('"', i);
if (j==-1) {
buffer.append(svalue.substring(i));
i=svalue.length();
} else {
buffer.append(svalue.substring(i, j+1));
buffer.append('"');
i=j+2;
}
}
buffer.append('"');
return buffer.toString();
}
/**
* Close the currently open log file (if any)
*/
private synchronized void close() {
if (writer == null)
return;
writer.flush();
writer.close();
writer = null;
currentLogFile = null;
}
/**
* Log the specified message to the log file, switching files if the date
* has changed since the previous log call.
*
* @param message Message to be logged
* @param date the current Date object (so this method doesn't need to
* create a new one)
*/
private void log(String message, Date date) {
if (rotatable){
// Only do a logfile switch check once a second, max.
long systime = System.currentTimeMillis();
if ((systime - rotationLastChecked) > 1000) {
// We need a new currentDate
currentDate = new Date(systime);
rotationLastChecked = systime;
// Check for a change of date
String tsDate = fileDateFormatter.format(currentDate);
// If the date has changed, switch log files
if (!dateStamp.equals(tsDate)) {
synchronized (this) {
if (!dateStamp.equals(tsDate)) {
close();
dateStamp = tsDate;
open();
}
}
}
}
}
/* In case something external rotated the file instead */
if (checkExists){
synchronized (this) {
if (currentLogFile!=null && !currentLogFile.exists()) {
try {
close();
} catch (Throwable e){
log.info("at least this wasn't swallowed", e);
}
/* Make sure date is correct */
currentDate = new Date(System.currentTimeMillis());
dateStamp = fileDateFormatter.format(currentDate);
open();
}
}
}
// Log this message
if (writer != null) {
writer.println(message);
}
}
/**
* Open the new log file for the date specified by <code>dateStamp</code>.
*/
private synchronized void open() {
// Create the directory if necessary
File dir = new File(directory);
if (!dir.isAbsolute())
dir = new File(System.getProperty("catalina.base"), directory);
dir.mkdirs();
// Open the current log file
try {
String pathname;
// If no rotate - no need for dateStamp in fileName
if (rotatable){
pathname = dir.getAbsolutePath() + File.separator +
prefix + dateStamp + suffix;
} else {
pathname = dir.getAbsolutePath() + File.separator +
prefix + suffix;
}
currentLogFile = new File(pathname);
writer = new PrintWriter(new FileWriter(pathname, true), true);
if (currentLogFile.length()==0) {
writer.println("#Fields: " + pattern);
writer.println("#Version: 1.0");
writer.println("#Software: " + ServerInfo.getServerInfo());
}
} catch (IOException e) {
writer = null;
currentLogFile = null;
}
}
/**
* This method returns a Date object that is accurate to within one
* second. If a thread calls this method to get a Date and it's been
* less than 1 second since a new Date was created, this method
* simply gives out the same Date again so that the system doesn't
* spend time creating Date objects unnecessarily.
*/
private Date getDate(long systime) {
/* Avoid extra call to System.currentTimeMillis(); */
if (0==systime) {
systime = System.currentTimeMillis();
}
// Only create a new Date once per second, max.
if ((systime - currentDate.getTime()) > 1000) {
currentDate.setTime(systime);
}
return currentDate;
}
// ------------------------------------------------------ Lifecycle Methods
/**
* Add a lifecycle event listener to this component.
*
* @param listener The listener to add
*/
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
/**
* Get the lifecycle listeners associated with this lifecycle. If this
* Lifecycle has no listeners registered, a zero-length array is returned.
*/
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}
/**
* Remove a lifecycle event listener from this component.
*
* @param listener The listener to add
*/
public void removeLifecycleListener(LifecycleListener listener) {
lifecycle.removeLifecycleListener(listener);
}
/**
* Prepare for the beginning of active use of the public methods of this
* component. This method should be called after <code>configure()</code>,
* and before any of the public methods of the component are utilized.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
public void start() throws LifecycleException {
// Validate and update our current component state
if (started)
throw new LifecycleException
(sm.getString("extendedAccessLogValve.alreadyStarted"));
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
// Initialize the timeZone, Date formatters, and currentDate
TimeZone tz = TimeZone.getTimeZone("GMT");
dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
dateFormatter.setTimeZone(tz);
timeFormatter = new SimpleDateFormat("HH:mm:ss");
timeFormatter.setTimeZone(tz);
currentDate = new Date(System.currentTimeMillis());
if (fileDateFormat==null || fileDateFormat.length()==0)
fileDateFormat = "yyyy-MM-dd";
fileDateFormatter = new SimpleDateFormat(fileDateFormat);
dateStamp = fileDateFormatter.format(currentDate);
timeTakenFormatter = new DecimalFormat("0.000");
/* Everybody say ick ... ick */
try {
InetAddress inetAddress = InetAddress.getLocalHost();
myIpAddress = inetAddress.getHostAddress();
myDNSName = inetAddress.getHostName();
} catch(Throwable e){
myIpAddress="127.0.0.1";
myDNSName="localhost";
}
open();
}
/**
* Gracefully terminate the active use of the public methods of this
* component. This method should be the last one called on a given
* instance of this component.
*
* @exception LifecycleException if this component detects a fatal error
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -