Merge pull request #4596 from eclipse/jetty-9.4.x-4567-log-throwable-lastarg
Issue #4567 - Backport of StdErrLog 10.0.x for Throwable behavior change
This commit is contained in:
commit
9325a5bc16
|
@ -92,7 +92,8 @@ import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||||
@ManagedObject("Jetty StdErr Logging Implementation")
|
@ManagedObject("Jetty StdErr Logging Implementation")
|
||||||
public class StdErrLog extends AbstractLogger
|
public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
private static final String EOL = System.getProperty("line.separator");
|
private static final String EOL = System.lineSeparator();
|
||||||
|
private static final Object[] EMPTY_ARGS = new Object[0];
|
||||||
// Do not change output format lightly, people rely on this output format now.
|
// Do not change output format lightly, people rely on this output format now.
|
||||||
private static int __tagpad = Integer.parseInt(Log.__props.getProperty("org.eclipse.jetty.util.log.StdErrLog.TAG_PAD", "0"));
|
private static int __tagpad = Integer.parseInt(Log.__props.getProperty("org.eclipse.jetty.util.log.StdErrLog.TAG_PAD", "0"));
|
||||||
private static DateCache _dateCache;
|
private static DateCache _dateCache;
|
||||||
|
@ -131,11 +132,11 @@ public class StdErrLog extends AbstractLogger
|
||||||
__tagpad = pad;
|
__tagpad = pad;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int _level = LEVEL_INFO;
|
private int _level;
|
||||||
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
|
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
|
||||||
private int _configuredLevel;
|
private int _configuredLevel;
|
||||||
private PrintStream _stderr = null;
|
private PrintStream _stderr = System.err;
|
||||||
private boolean _source = __source;
|
private boolean _source;
|
||||||
// Print the long form names, otherwise use abbreviated
|
// Print the long form names, otherwise use abbreviated
|
||||||
private boolean _printLongNames = __long;
|
private boolean _printLongNames = __long;
|
||||||
// The full log name, as provided by the system.
|
// The full log name, as provided by the system.
|
||||||
|
@ -285,9 +286,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_WARN)
|
if (_level <= LEVEL_WARN)
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":WARN:", msg, args);
|
format(builder, ":WARN:", msg, args);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,9 +303,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_WARN)
|
if (_level <= LEVEL_WARN)
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":WARN:", msg, thrown);
|
format(builder, ":WARN:", msg, thrown);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,9 +314,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_INFO)
|
if (_level <= LEVEL_INFO)
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":INFO:", msg, args);
|
format(builder, ":INFO:", msg, args);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,9 +331,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_INFO)
|
if (_level <= LEVEL_INFO)
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":INFO:", msg, thrown);
|
format(builder, ":INFO:", msg, thrown);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,26 +351,24 @@ public class StdErrLog extends AbstractLogger
|
||||||
@Override
|
@Override
|
||||||
public void setDebugEnabled(boolean enabled)
|
public void setDebugEnabled(boolean enabled)
|
||||||
{
|
{
|
||||||
if (enabled)
|
int level = enabled ? LEVEL_DEBUG : this.getConfiguredLevel();
|
||||||
{
|
this.setLevel(level);
|
||||||
this._level = LEVEL_DEBUG;
|
|
||||||
|
|
||||||
|
String name = getName();
|
||||||
for (Logger log : Log.getLoggers().values())
|
for (Logger log : Log.getLoggers().values())
|
||||||
{
|
{
|
||||||
if (log.getName().startsWith(getName()) && log instanceof StdErrLog)
|
if (log.getName().startsWith(name) && log instanceof StdErrLog)
|
||||||
((StdErrLog)log).setLevel(LEVEL_DEBUG);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
this._level = this._configuredLevel;
|
StdErrLog logger = (StdErrLog)log;
|
||||||
|
level = enabled ? LEVEL_DEBUG : logger.getConfiguredLevel();
|
||||||
|
logger.setLevel(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (Logger log : Log.getLoggers().values())
|
private int getConfiguredLevel()
|
||||||
{
|
{
|
||||||
if (log.getName().startsWith(getName()) && log instanceof StdErrLog)
|
return _configuredLevel;
|
||||||
((StdErrLog)log).setLevel(((StdErrLog)log)._configuredLevel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLevel()
|
public int getLevel()
|
||||||
|
@ -392,17 +391,24 @@ public class StdErrLog extends AbstractLogger
|
||||||
|
|
||||||
public void setStdErrStream(PrintStream stream)
|
public void setStdErrStream(PrintStream stream)
|
||||||
{
|
{
|
||||||
this._stderr = stream == System.err ? null : stream;
|
if (stream == null)
|
||||||
|
{
|
||||||
|
this._stderr = System.err;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this._stderr = stream;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void debug(String msg, Object... args)
|
public void debug(String msg, Object... args)
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_DEBUG)
|
if (isDebugEnabled())
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":DBUG:", msg, args);
|
format(builder, ":DBUG:", msg, args);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,9 +417,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (isDebugEnabled())
|
if (isDebugEnabled())
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":DBUG:", msg, arg);
|
format(builder, ":DBUG:", msg, arg);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,145 +432,119 @@ public class StdErrLog extends AbstractLogger
|
||||||
@Override
|
@Override
|
||||||
public void debug(String msg, Throwable thrown)
|
public void debug(String msg, Throwable thrown)
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_DEBUG)
|
if (isDebugEnabled())
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":DBUG:", msg, thrown);
|
format(builder, ":DBUG:", msg, thrown);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void format(StringBuilder buffer, String level, String msg, Object... args)
|
private void println(StringBuilder builder)
|
||||||
|
{
|
||||||
|
_stderr.println(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void format(StringBuilder builder, String level, String msg, Object... inArgs)
|
||||||
{
|
{
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
int ms = (int)(now % 1000);
|
int ms = (int)(now % 1000);
|
||||||
String d = _dateCache.formatNow(now);
|
String d = _dateCache.formatNow(now);
|
||||||
tag(buffer, d, ms, level);
|
tag(builder, d, ms, level);
|
||||||
format(buffer, msg, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void format(StringBuilder buffer, String level, String msg, Throwable thrown)
|
Object[] msgArgs = EMPTY_ARGS;
|
||||||
|
int msgArgsLen = 0;
|
||||||
|
Throwable cause = null;
|
||||||
|
|
||||||
|
if (inArgs != null)
|
||||||
{
|
{
|
||||||
format(buffer, level, msg);
|
msgArgs = inArgs;
|
||||||
if (isHideStacks())
|
msgArgsLen = inArgs.length;
|
||||||
|
if (msgArgsLen > 0 && inArgs[msgArgsLen - 1] instanceof Throwable)
|
||||||
{
|
{
|
||||||
format(buffer, ": " + thrown);
|
cause = (Throwable)inArgs[msgArgsLen - 1];
|
||||||
}
|
msgArgsLen--;
|
||||||
else
|
|
||||||
{
|
|
||||||
format(buffer, thrown);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tag(StringBuilder buffer, String d, int ms, String tag)
|
|
||||||
{
|
|
||||||
buffer.setLength(0);
|
|
||||||
buffer.append(d);
|
|
||||||
if (ms > 99)
|
|
||||||
{
|
|
||||||
buffer.append('.');
|
|
||||||
}
|
|
||||||
else if (ms > 9)
|
|
||||||
{
|
|
||||||
buffer.append(".0");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer.append(".00");
|
|
||||||
}
|
|
||||||
buffer.append(ms).append(tag);
|
|
||||||
|
|
||||||
String name = _printLongNames ? _name : _abbrevname;
|
|
||||||
String tname = Thread.currentThread().getName();
|
|
||||||
|
|
||||||
int p = __tagpad > 0 ? (name.length() + tname.length() - __tagpad) : 0;
|
|
||||||
|
|
||||||
if (p < 0)
|
|
||||||
{
|
|
||||||
buffer
|
|
||||||
.append(name)
|
|
||||||
.append(':')
|
|
||||||
.append(" ", 0, -p)
|
|
||||||
.append(tname);
|
|
||||||
}
|
|
||||||
else if (p == 0)
|
|
||||||
{
|
|
||||||
buffer.append(name).append(':').append(tname);
|
|
||||||
}
|
|
||||||
buffer.append(':');
|
|
||||||
|
|
||||||
if (_source)
|
|
||||||
{
|
|
||||||
Throwable source = new Throwable();
|
|
||||||
StackTraceElement[] frames = source.getStackTrace();
|
|
||||||
for (int i = 0; i < frames.length; i++)
|
|
||||||
{
|
|
||||||
final StackTraceElement frame = frames[i];
|
|
||||||
String clazz = frame.getClassName();
|
|
||||||
if (clazz.equals(StdErrLog.class.getName()) || clazz.equals(Log.class.getName()))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty."))
|
|
||||||
{
|
|
||||||
buffer.append(condensePackageString(clazz));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer.append(clazz);
|
|
||||||
}
|
|
||||||
buffer.append('#').append(frame.getMethodName());
|
|
||||||
if (frame.getFileName() != null)
|
|
||||||
{
|
|
||||||
buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
|
|
||||||
}
|
|
||||||
buffer.append(':');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.append(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
private void format(StringBuilder builder, String msg, Object... args)
|
|
||||||
{
|
|
||||||
if (msg == null)
|
if (msg == null)
|
||||||
{
|
{
|
||||||
msg = "";
|
msg = "";
|
||||||
for (int i = 0; i < args.length; i++)
|
for (int i = 0; i < msgArgsLen; i++)
|
||||||
{
|
{
|
||||||
|
//noinspection StringConcatenationInLoop
|
||||||
msg += "{} ";
|
msg += "{} ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String braces = "{}";
|
String braces = "{}";
|
||||||
int start = 0;
|
int start = 0;
|
||||||
for (Object arg : args)
|
for (int i = 0; i < msgArgsLen; i++)
|
||||||
{
|
{
|
||||||
|
Object arg = msgArgs[i];
|
||||||
int bracesIndex = msg.indexOf(braces, start);
|
int bracesIndex = msg.indexOf(braces, start);
|
||||||
if (bracesIndex < 0)
|
if (bracesIndex < 0)
|
||||||
{
|
{
|
||||||
escape(builder, msg.substring(start));
|
escape(builder, msg.substring(start));
|
||||||
builder.append(" ");
|
builder.append(" ");
|
||||||
|
if (arg != null)
|
||||||
builder.append(arg);
|
builder.append(arg);
|
||||||
start = msg.length();
|
start = msg.length();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
escape(builder, msg.substring(start, bracesIndex));
|
escape(builder, msg.substring(start, bracesIndex));
|
||||||
|
if (arg != null)
|
||||||
builder.append(arg);
|
builder.append(arg);
|
||||||
start = bracesIndex + braces.length();
|
start = bracesIndex + braces.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
escape(builder, msg.substring(start));
|
escape(builder, msg.substring(start));
|
||||||
|
|
||||||
|
if (cause != null)
|
||||||
|
{
|
||||||
|
if (isHideStacks())
|
||||||
|
{
|
||||||
|
builder.append(": ").append(cause);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
formatCause(builder, cause, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void escape(StringBuilder builder, String string)
|
private void formatCause(StringBuilder builder, Throwable cause, String indent)
|
||||||
|
{
|
||||||
|
builder.append(EOL).append(indent);
|
||||||
|
escape(builder, cause.toString());
|
||||||
|
StackTraceElement[] elements = cause.getStackTrace();
|
||||||
|
for (int i = 0; elements != null && i < elements.length; i++)
|
||||||
|
{
|
||||||
|
builder.append(EOL).append(indent).append("\tat ");
|
||||||
|
escape(builder, elements[i].toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Throwable suppressed : cause.getSuppressed())
|
||||||
|
{
|
||||||
|
builder.append(EOL).append(indent).append("Suppressed: ");
|
||||||
|
formatCause(builder, suppressed, "\t|" + indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
Throwable by = cause.getCause();
|
||||||
|
if (by != null && by != cause)
|
||||||
|
{
|
||||||
|
builder.append(EOL).append(indent).append("Caused by: ");
|
||||||
|
formatCause(builder, by, indent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void escape(StringBuilder builder, String str)
|
||||||
{
|
{
|
||||||
if (__escape)
|
if (__escape)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < string.length(); ++i)
|
for (int i = 0; i < str.length(); ++i)
|
||||||
{
|
{
|
||||||
char c = string.charAt(i);
|
char c = str.charAt(i);
|
||||||
if (Character.isISOControl(c))
|
if (Character.isISOControl(c))
|
||||||
{
|
{
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
|
@ -587,44 +567,76 @@ public class StdErrLog extends AbstractLogger
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
builder.append(string);
|
builder.append(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void format(StringBuilder buffer, Throwable thrown)
|
private void tag(StringBuilder builder, String d, int ms, String tag)
|
||||||
{
|
{
|
||||||
format(buffer, thrown, "");
|
builder.setLength(0);
|
||||||
|
builder.append(d);
|
||||||
|
if (ms > 99)
|
||||||
|
{
|
||||||
|
builder.append('.');
|
||||||
}
|
}
|
||||||
|
else if (ms > 9)
|
||||||
protected void format(StringBuilder buffer, Throwable thrown, String indent)
|
|
||||||
{
|
{
|
||||||
if (thrown == null)
|
builder.append(".0");
|
||||||
{
|
|
||||||
buffer.append("null");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
buffer.append(EOL).append(indent);
|
builder.append(".00");
|
||||||
format(buffer, thrown.toString());
|
}
|
||||||
StackTraceElement[] elements = thrown.getStackTrace();
|
builder.append(ms).append(tag);
|
||||||
for (int i = 0; elements != null && i < elements.length; i++)
|
|
||||||
|
String name = _printLongNames ? _name : _abbrevname;
|
||||||
|
String tname = Thread.currentThread().getName();
|
||||||
|
|
||||||
|
int p = __tagpad > 0 ? (name.length() + tname.length() - __tagpad) : 0;
|
||||||
|
|
||||||
|
if (p < 0)
|
||||||
{
|
{
|
||||||
buffer.append(EOL).append(indent).append("\tat ");
|
builder
|
||||||
format(buffer, elements[i].toString());
|
.append(name)
|
||||||
|
.append(':')
|
||||||
|
.append(" ", 0, -p)
|
||||||
|
.append(tname);
|
||||||
|
}
|
||||||
|
else if (p == 0)
|
||||||
|
{
|
||||||
|
builder.append(name).append(':').append(tname);
|
||||||
|
}
|
||||||
|
builder.append(':');
|
||||||
|
|
||||||
|
if (_source)
|
||||||
|
{
|
||||||
|
Throwable source = new Throwable();
|
||||||
|
StackTraceElement[] frames = source.getStackTrace();
|
||||||
|
for (final StackTraceElement frame : frames)
|
||||||
|
{
|
||||||
|
String clazz = frame.getClassName();
|
||||||
|
if (clazz.equals(StdErrLog.class.getName()) || clazz.equals(Log.class.getName()))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!_printLongNames && clazz.startsWith("org.eclipse.jetty."))
|
||||||
|
{
|
||||||
|
builder.append(condensePackageString(clazz));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.append(clazz);
|
||||||
|
}
|
||||||
|
builder.append('#').append(frame.getMethodName());
|
||||||
|
if (frame.getFileName() != null)
|
||||||
|
{
|
||||||
|
builder.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
|
||||||
|
}
|
||||||
|
builder.append(':');
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Throwable suppressed : thrown.getSuppressed())
|
builder.append(' ');
|
||||||
{
|
|
||||||
buffer.append(EOL).append(indent).append("Suppressed: ");
|
|
||||||
format(buffer, suppressed, "\t|" + indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
Throwable cause = thrown.getCause();
|
|
||||||
if (cause != null && cause != thrown)
|
|
||||||
{
|
|
||||||
buffer.append(EOL).append(indent).append("Caused by: ");
|
|
||||||
format(buffer, cause, indent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -678,9 +690,9 @@ public class StdErrLog extends AbstractLogger
|
||||||
{
|
{
|
||||||
if (_level <= LEVEL_ALL)
|
if (_level <= LEVEL_ALL)
|
||||||
{
|
{
|
||||||
StringBuilder buffer = new StringBuilder(64);
|
StringBuilder builder = new StringBuilder(64);
|
||||||
format(buffer, ":IGNORED:", "", ignored);
|
format(builder, ":IGNORED:", "", ignored);
|
||||||
(_stderr == null ? System.err : _stderr).println(buffer);
|
println(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@ -54,7 +53,7 @@ public class StdErrLogTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStdErrLogFormat() throws UnsupportedEncodingException
|
public void testStdErrLogFormat()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(LogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(LogTest.class.getName(), new Properties());
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
@ -69,12 +68,11 @@ public class StdErrLogTest
|
||||||
|
|
||||||
System.err.println(output);
|
System.err.println(output);
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing:test,format1");
|
output.assertContains("INFO:oejul.LogTest:tname: testing:test,format1");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing:test,format1");
|
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing:test format2");
|
output.assertContains("INFO:oejul.LogTest:tname: testing:test format2");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing test format3");
|
output.assertContains("INFO:oejul.LogTest:tname: testing test format3");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing:test,null");
|
output.assertContains("INFO:oejul.LogTest:tname: testing:test,");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing null null");
|
output.assertContains("INFO:oejul.LogTest:tname: testing");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing:null");
|
output.assertContains("INFO:oejul.LogTest:tname: testing:");
|
||||||
output.assertContains("INFO:oejul.LogTest:tname: testing");
|
output.assertContains("INFO:oejul.LogTest:tname: testing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,60 +117,158 @@ public class StdErrLogTest
|
||||||
@Test
|
@Test
|
||||||
public void testStdErrLogName()
|
public void testStdErrLogName()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog("test", new Properties());
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
log.setPrintLongNames(true);
|
log.setPrintLongNames(true);
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
assertThat("Log.name", log.getName(), is("test"));
|
assertThat("Log.name", log.getName(), is("testX"));
|
||||||
Logger next = log.getLogger("next");
|
Logger next = log.getLogger("next");
|
||||||
assertThat("Log.name(child)", next.getName(), is("test.next"));
|
assertThat("Log.name(child)", next.getName(), is("testX.next"));
|
||||||
next.info("testing {} {}", "next", "info");
|
next.info("testing {} {}", "next", "info");
|
||||||
|
|
||||||
output.assertContains(":test.next:tname: testing next info");
|
output.assertContains(":testX.next:tname: testing next info");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStdErrMsgThrowable()
|
||||||
|
{
|
||||||
|
// The test Throwable
|
||||||
|
Throwable th = new Throwable("Message");
|
||||||
|
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
|
log.warn("ex", th); // Behavior here is being tested
|
||||||
|
output.assertContains(asString(th));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
@Test
|
||||||
|
public void testStdErrMsgThrowableNull()
|
||||||
|
{
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
|
Throwable th = null;
|
||||||
|
log.warn("ex", th);
|
||||||
|
output.assertContains("testX");
|
||||||
|
output.assertNotContains("null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStdErrThrowable()
|
public void testStdErrThrowable()
|
||||||
{
|
{
|
||||||
// Common Throwable (for test)
|
// The test Throwable
|
||||||
Throwable th = new Throwable("Message");
|
Throwable th = new Throwable("Message");
|
||||||
|
|
||||||
// Capture raw string form
|
// Initialize Logger
|
||||||
StringWriter tout = new StringWriter();
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
th.printStackTrace(new PrintWriter(tout));
|
|
||||||
String ths = tout.toString();
|
|
||||||
|
|
||||||
// Start test
|
|
||||||
StdErrLog log = new StdErrLog("test", new Properties());
|
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
log.warn("ex", th);
|
// Test behavior
|
||||||
output.assertContains(ths);
|
log.warn(th);
|
||||||
|
output.assertContains(asString(th));
|
||||||
|
}
|
||||||
|
|
||||||
th = new Throwable("Message with \033 escape");
|
@Test
|
||||||
|
public void testStdErrThrowableNull()
|
||||||
|
{
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
|
Throwable th = null;
|
||||||
|
log.warn(th); // Behavior here is being tested
|
||||||
|
output.assertContains("testX");
|
||||||
|
output.assertNotContains("null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStdErrFormatArgsThrowable()
|
||||||
|
{
|
||||||
|
// The test throwable
|
||||||
|
Throwable th = new Throwable("Reasons Explained");
|
||||||
|
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
|
log.warn("Ex {}", "Reasons", th);
|
||||||
|
output.assertContains("Reasons");
|
||||||
|
output.assertContains(asString(th));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
@Test
|
||||||
|
public void testStdErrFormatArgsThrowableNull()
|
||||||
|
{
|
||||||
|
// The test throwable
|
||||||
|
Throwable th = null;
|
||||||
|
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
|
log.warn("Ex {}", "Reasons", th);
|
||||||
|
output.assertContains("Reasons");
|
||||||
|
output.assertNotContains("null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStdErrMsgThrowableWithControlChars()
|
||||||
|
{
|
||||||
|
// The test throwable, using "\b" (backspace) character
|
||||||
|
Throwable th = new Throwable("Message with \b backspace");
|
||||||
|
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
log.warn("ex", th);
|
log.warn("ex", th);
|
||||||
output.assertNotContains("Message with \033 escape");
|
output.assertNotContains("Message with \b backspace");
|
||||||
|
output.assertContains("Message with ? backspace");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStdErrMsgStringThrowableWithControlChars()
|
||||||
|
{
|
||||||
|
// The test throwable, using "\b" (backspace) character
|
||||||
|
Throwable th = new Throwable("Message with \b backspace");
|
||||||
|
|
||||||
|
// Initialize Logger
|
||||||
|
StdErrLog log = new StdErrLog("testX", new Properties());
|
||||||
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
// Test behavior
|
||||||
log.info(th.toString());
|
log.info(th.toString());
|
||||||
output.assertNotContains("Message with \033 escape");
|
output.assertNotContains("Message with \b backspace");
|
||||||
|
output.assertContains("Message with ? backspace");
|
||||||
|
}
|
||||||
|
|
||||||
log.warn("ex", th);
|
private String asString(Throwable cause)
|
||||||
output.assertContains("Message with ? escape");
|
{
|
||||||
log.info(th.toString());
|
StringWriter tout = new StringWriter();
|
||||||
output.assertContains("Message with ? escape");
|
cause.printStackTrace(new PrintWriter(tout));
|
||||||
|
return tout.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test to make sure that using a Null parameter on parameterized messages does not result in a NPE
|
* Test to make sure that using a Null parameter on parameterized messages does not result in a NPE
|
||||||
*
|
|
||||||
* @throws Exception failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testParameterizedMessageNullValues() throws Exception
|
public void testParameterizedMessageNullValues()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
log.setLevel(StdErrLog.LEVEL_DEBUG);
|
log.setLevel(StdErrLog.LEVEL_DEBUG);
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
log.info("Testing info(msg,null,null) - {} {}", "arg0", "arg1");
|
log.info("Testing info(msg,null,null) - {} {}", "arg0", "arg1");
|
||||||
log.info("Testing info(msg,null,null) - {} {}", null, null);
|
log.info("Testing info(msg,null,null) - {} {}", null, null);
|
||||||
|
@ -313,14 +409,12 @@ public class StdErrLogTest
|
||||||
* Tests StdErrLog.warn() methods with level filtering.
|
* Tests StdErrLog.warn() methods with level filtering.
|
||||||
* <p>
|
* <p>
|
||||||
* Should always see WARN level messages, regardless of set level.
|
* Should always see WARN level messages, regardless of set level.
|
||||||
*
|
|
||||||
* @throws UnsupportedEncodingException failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testWarnFiltering() throws UnsupportedEncodingException
|
public void testWarnFiltering()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
@ -355,14 +449,12 @@ public class StdErrLogTest
|
||||||
* Tests StdErrLog.info() methods with level filtering.
|
* Tests StdErrLog.info() methods with level filtering.
|
||||||
* <p>
|
* <p>
|
||||||
* Should only see INFO level messages when level is set to {@link StdErrLog#LEVEL_INFO} and below.
|
* Should only see INFO level messages when level is set to {@link StdErrLog#LEVEL_INFO} and below.
|
||||||
*
|
|
||||||
* @throws Exception failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testInfoFiltering() throws Exception
|
public void testInfoFiltering()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
@ -403,14 +495,12 @@ public class StdErrLogTest
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link StdErrLog#LEVEL_OFF} filtering.
|
* Tests {@link StdErrLog#LEVEL_OFF} filtering.
|
||||||
*
|
|
||||||
* @throws Exception failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOffFiltering() throws Exception
|
public void testOffFiltering()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
log.setLevel(StdErrLog.LEVEL_OFF);
|
log.setLevel(StdErrLog.LEVEL_OFF);
|
||||||
|
|
||||||
|
@ -434,14 +524,12 @@ public class StdErrLogTest
|
||||||
* Tests StdErrLog.debug() methods with level filtering.
|
* Tests StdErrLog.debug() methods with level filtering.
|
||||||
* <p>
|
* <p>
|
||||||
* Should only see DEBUG level messages when level is set to {@link StdErrLog#LEVEL_DEBUG} and below.
|
* Should only see DEBUG level messages when level is set to {@link StdErrLog#LEVEL_DEBUG} and below.
|
||||||
*
|
|
||||||
* @throws Exception failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testDebugFiltering() throws Exception
|
public void testDebugFiltering()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
@ -485,14 +573,12 @@ public class StdErrLogTest
|
||||||
* Tests StdErrLog with {@link Logger#ignore(Throwable)} use.
|
* Tests StdErrLog with {@link Logger#ignore(Throwable)} use.
|
||||||
* <p>
|
* <p>
|
||||||
* Should only see IGNORED level messages when level is set to {@link StdErrLog#LEVEL_ALL}.
|
* Should only see IGNORED level messages when level is set to {@link StdErrLog#LEVEL_ALL}.
|
||||||
*
|
|
||||||
* @throws Exception failed test
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testIgnores() throws Exception
|
public void testIgnores()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
StdErrCapture output = new StdErrCapture(log);
|
StdErrCapture output = new StdErrCapture(log);
|
||||||
|
|
||||||
|
@ -516,10 +602,10 @@ public class StdErrLogTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsDebugEnabled() throws Exception
|
public void testIsDebugEnabled()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
log.setLevel(StdErrLog.LEVEL_ALL);
|
log.setLevel(StdErrLog.LEVEL_ALL);
|
||||||
assertThat("log.level(all).isDebugEnabled", log.isDebugEnabled(), is(true));
|
assertThat("log.level(all).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||||
|
@ -542,7 +628,7 @@ public class StdErrLogTest
|
||||||
public void testSetGetLevel()
|
public void testSetGetLevel()
|
||||||
{
|
{
|
||||||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
log.setLevel(StdErrLog.LEVEL_ALL);
|
log.setLevel(StdErrLog.LEVEL_ALL);
|
||||||
assertThat("log.level(all).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_ALL));
|
assertThat("log.level(all).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_ALL));
|
||||||
|
@ -566,7 +652,7 @@ public class StdErrLogTest
|
||||||
{
|
{
|
||||||
String baseName = "jetty";
|
String baseName = "jetty";
|
||||||
StdErrLog log = new StdErrLog(baseName, new Properties());
|
StdErrLog log = new StdErrLog(baseName, new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
assertThat("Logger.name", log.getName(), is("jetty"));
|
assertThat("Logger.name", log.getName(), is("jetty"));
|
||||||
|
|
||||||
|
@ -580,7 +666,7 @@ public class StdErrLogTest
|
||||||
{
|
{
|
||||||
String baseName = "jetty";
|
String baseName = "jetty";
|
||||||
StdErrLog log = new StdErrLog(baseName, new Properties());
|
StdErrLog log = new StdErrLog(baseName, new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
assertThat("Logger.name", log.getName(), is("jetty"));
|
assertThat("Logger.name", log.getName(), is("jetty"));
|
||||||
|
|
||||||
|
@ -594,7 +680,7 @@ public class StdErrLogTest
|
||||||
{
|
{
|
||||||
String baseName = "jetty";
|
String baseName = "jetty";
|
||||||
StdErrLog log = new StdErrLog(baseName, new Properties());
|
StdErrLog log = new StdErrLog(baseName, new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
assertThat("Logger.name", log.getName(), is("jetty"));
|
assertThat("Logger.name", log.getName(), is("jetty"));
|
||||||
|
|
||||||
|
@ -610,7 +696,7 @@ public class StdErrLogTest
|
||||||
{
|
{
|
||||||
String baseName = "jetty";
|
String baseName = "jetty";
|
||||||
StdErrLog log = new StdErrLog(baseName, new Properties());
|
StdErrLog log = new StdErrLog(baseName, new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
assertThat("Logger.name", log.getName(), is("jetty"));
|
assertThat("Logger.name", log.getName(), is("jetty"));
|
||||||
|
|
||||||
|
@ -626,7 +712,7 @@ public class StdErrLogTest
|
||||||
{
|
{
|
||||||
String baseName = "jetty";
|
String baseName = "jetty";
|
||||||
StdErrLog log = new StdErrLog(baseName, new Properties());
|
StdErrLog log = new StdErrLog(baseName, new Properties());
|
||||||
try (StacklessLogging stackless = new StacklessLogging(log))
|
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||||
{
|
{
|
||||||
assertThat("Logger.name", log.getName(), is("jetty"));
|
assertThat("Logger.name", log.getName(), is("jetty"));
|
||||||
|
|
||||||
|
@ -671,7 +757,7 @@ public class StdErrLogTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrintSource() throws UnsupportedEncodingException
|
public void testPrintSource()
|
||||||
{
|
{
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.put("test.SOURCE", "true");
|
props.put("test.SOURCE", "true");
|
||||||
|
@ -690,7 +776,6 @@ public class StdErrLogTest
|
||||||
assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
|
assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
|
||||||
|
|
||||||
props.put("test.SOURCE", "false");
|
props.put("test.SOURCE", "false");
|
||||||
log = new StdErrLog("other", props);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue