init
This commit is contained in:
49
java/org/apache/tomcat/util/log/CaptureLog.java
Normal file
49
java/org/apache/tomcat/util/log/CaptureLog.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.tomcat.util.log;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Per Thread System.err and System.out log capture data.
|
||||
*
|
||||
* @author Glenn L. Nielsen
|
||||
*/
|
||||
|
||||
class CaptureLog {
|
||||
|
||||
protected CaptureLog() {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
}
|
||||
|
||||
private final ByteArrayOutputStream baos;
|
||||
private final PrintStream ps;
|
||||
|
||||
protected PrintStream getStream() {
|
||||
return ps;
|
||||
}
|
||||
|
||||
protected void reset() {
|
||||
baos.reset();
|
||||
}
|
||||
|
||||
protected String getCapture() {
|
||||
return baos.toString();
|
||||
}
|
||||
}
|
||||
275
java/org/apache/tomcat/util/log/SystemLogHandler.java
Normal file
275
java/org/apache/tomcat/util/log/SystemLogHandler.java
Normal file
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
* 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.tomcat.util.log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.EmptyStackException;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* This helper class may be used to do sophisticated redirection of
|
||||
* System.out and System.err on a per Thread basis.
|
||||
*
|
||||
* A stack is implemented per Thread so that nested startCapture
|
||||
* and stopCapture can be used.
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
* @author Glenn L. Nielsen
|
||||
*/
|
||||
public class SystemLogHandler extends PrintStream {
|
||||
|
||||
|
||||
// ----------------------------------------------------------- Constructors
|
||||
|
||||
|
||||
/**
|
||||
* Construct the handler to capture the output of the given steam.
|
||||
*
|
||||
* @param wrapped The stream to capture
|
||||
*/
|
||||
public SystemLogHandler(PrintStream wrapped) {
|
||||
super(wrapped);
|
||||
out = wrapped;
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
/**
|
||||
* Wrapped PrintStream.
|
||||
*/
|
||||
private final PrintStream out;
|
||||
|
||||
|
||||
/**
|
||||
* Thread <-> CaptureLog associations.
|
||||
*/
|
||||
private static final ThreadLocal<Stack<CaptureLog>> logs = new ThreadLocal<>();
|
||||
|
||||
|
||||
/**
|
||||
* Spare CaptureLog ready for reuse.
|
||||
*/
|
||||
private static final Stack<CaptureLog> reuse = new Stack<>();
|
||||
|
||||
|
||||
// --------------------------------------------------------- Public Methods
|
||||
|
||||
|
||||
/**
|
||||
* Start capturing thread's output.
|
||||
*/
|
||||
public static void startCapture() {
|
||||
CaptureLog log = null;
|
||||
if (!reuse.isEmpty()) {
|
||||
try {
|
||||
log = reuse.pop();
|
||||
} catch (EmptyStackException e) {
|
||||
log = new CaptureLog();
|
||||
}
|
||||
} else {
|
||||
log = new CaptureLog();
|
||||
}
|
||||
Stack<CaptureLog> stack = logs.get();
|
||||
if (stack == null) {
|
||||
stack = new Stack<>();
|
||||
logs.set(stack);
|
||||
}
|
||||
stack.push(log);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stop capturing thread's output.
|
||||
*
|
||||
* @return The captured data
|
||||
*/
|
||||
public static String stopCapture() {
|
||||
Stack<CaptureLog> stack = logs.get();
|
||||
if (stack == null || stack.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
CaptureLog log = stack.pop();
|
||||
if (log == null) {
|
||||
return null;
|
||||
}
|
||||
String capture = log.getCapture();
|
||||
log.reset();
|
||||
reuse.push(log);
|
||||
return capture;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------ Protected Methods
|
||||
|
||||
|
||||
/**
|
||||
* Find PrintStream to which the output must be written to.
|
||||
* @return the print stream
|
||||
*/
|
||||
protected PrintStream findStream() {
|
||||
Stack<CaptureLog> stack = logs.get();
|
||||
if (stack != null && !stack.isEmpty()) {
|
||||
CaptureLog log = stack.peek();
|
||||
if (log != null) {
|
||||
PrintStream ps = log.getStream();
|
||||
if (ps != null) {
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------- PrintStream Methods
|
||||
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
findStream().flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
findStream().close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkError() {
|
||||
return findStream().checkError();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setError() {
|
||||
//findStream().setError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
findStream().write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b)
|
||||
throws IOException {
|
||||
findStream().write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buf, int off, int len) {
|
||||
findStream().write(buf, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(boolean b) {
|
||||
findStream().print(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(char c) {
|
||||
findStream().print(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(int i) {
|
||||
findStream().print(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(long l) {
|
||||
findStream().print(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(float f) {
|
||||
findStream().print(f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(double d) {
|
||||
findStream().print(d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(char[] s) {
|
||||
findStream().print(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String s) {
|
||||
findStream().print(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(Object obj) {
|
||||
findStream().print(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println() {
|
||||
findStream().println();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(boolean x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(char x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(long x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(float x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(double x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(char[] x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(Object x) {
|
||||
findStream().println(x);
|
||||
}
|
||||
|
||||
}
|
||||
149
java/org/apache/tomcat/util/log/UserDataHelper.java
Normal file
149
java/org/apache/tomcat/util/log/UserDataHelper.java
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* 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.tomcat.util.log;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
|
||||
/**
|
||||
* This helper class assists with the logging associated with invalid input
|
||||
* data. A developer may want all instances of invalid input data logged to
|
||||
* assist with debugging whereas in production it is likely to be desirable not
|
||||
* to log anything for invalid data. The following settings may be used:
|
||||
* <ul>
|
||||
* <li>NOTHING: Log nothing.</li>
|
||||
* <li>DEBUG_ALL: Log all problems at DEBUG log level.</li>
|
||||
* <li>INFO_THEN_DEBUG: Log first problem at INFO log level and any further
|
||||
* issues in the following TBD (configurable) seconds at DEBUG level</li>
|
||||
* <li>INFO_ALL: Log all problems at INFO log level.</li>
|
||||
* </ul>
|
||||
* By default, INFO_THEN_DEBUG is used with a suppression time of 24 hours.
|
||||
*
|
||||
* NOTE: This class is not completely thread-safe. When using INFO_THEN_DEBUG it
|
||||
* is possible that several INFO messages will be logged before dropping to
|
||||
* DEBUG.
|
||||
*/
|
||||
public class UserDataHelper {
|
||||
|
||||
private final Log log;
|
||||
|
||||
private final Config config;
|
||||
|
||||
// A value of 0 is equivalent to using INFO_ALL
|
||||
// A negative value will trigger infinite suppression
|
||||
// The value is milliseconds
|
||||
private final long suppressionTime;
|
||||
|
||||
private volatile long lastInfoTime = 0;
|
||||
|
||||
|
||||
public UserDataHelper(Log log) {
|
||||
this.log = log;
|
||||
|
||||
Config tempConfig;
|
||||
String configString = System.getProperty(
|
||||
"org.apache.juli.logging.UserDataHelper.CONFIG");
|
||||
if (configString == null) {
|
||||
tempConfig = Config.INFO_THEN_DEBUG;
|
||||
} else {
|
||||
try {
|
||||
tempConfig = Config.valueOf(configString);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
// Ignore - use default
|
||||
tempConfig = Config.INFO_THEN_DEBUG;
|
||||
}
|
||||
}
|
||||
|
||||
// Default suppression time of 1 day.
|
||||
suppressionTime = Integer.getInteger(
|
||||
"org.apache.juli.logging.UserDataHelper.SUPPRESSION_TIME",
|
||||
60 * 60 * 24).intValue() * 1000L;
|
||||
|
||||
if (suppressionTime == 0) {
|
||||
tempConfig = Config.INFO_ALL;
|
||||
}
|
||||
|
||||
config = tempConfig;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns log mode for the next log message, or <code>null</code> if the
|
||||
* message should not be logged.
|
||||
*
|
||||
* <p>
|
||||
* If <code>INFO_THEN_DEBUG</code> configuration option is enabled, this
|
||||
* method might change internal state of this object.
|
||||
*
|
||||
* @return Log mode, or <code>null</code>
|
||||
*/
|
||||
public Mode getNextMode() {
|
||||
if (Config.NONE == config) {
|
||||
return null;
|
||||
} else if (Config.DEBUG_ALL == config) {
|
||||
return log.isDebugEnabled() ? Mode.DEBUG : null;
|
||||
} else if (Config.INFO_THEN_DEBUG == config) {
|
||||
if (logAtInfo()) {
|
||||
return log.isInfoEnabled() ? Mode.INFO_THEN_DEBUG : null;
|
||||
} else {
|
||||
return log.isDebugEnabled() ? Mode.DEBUG : null;
|
||||
}
|
||||
} else if (Config.INFO_ALL == config) {
|
||||
return log.isInfoEnabled() ? Mode.INFO : null;
|
||||
}
|
||||
// Should never happen
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Not completely thread-safe but good enough for this use case. I couldn't
|
||||
* see a simple enough way to make it completely thread-safe that was not
|
||||
* likely to compromise performance.
|
||||
*/
|
||||
private boolean logAtInfo() {
|
||||
|
||||
if (suppressionTime < 0 && lastInfoTime > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
if (lastInfoTime + suppressionTime > now) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lastInfoTime = now;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private enum Config {
|
||||
NONE,
|
||||
DEBUG_ALL,
|
||||
INFO_THEN_DEBUG,
|
||||
INFO_ALL
|
||||
}
|
||||
|
||||
/**
|
||||
* Log mode for the next log message.
|
||||
*/
|
||||
public enum Mode {
|
||||
DEBUG,
|
||||
INFO_THEN_DEBUG,
|
||||
INFO
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user