From d453333a48ba25095fed2e1167936db9f0815da0 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 20 Feb 2020 12:57:49 -0600 Subject: [PATCH 1/2] Issue #4567 - Backport of StdErrLog 10.0.x for Throwable behavior change Signed-off-by: Joakim Erdfelt --- .../org/eclipse/jetty/util/log/StdErrLog.java | 304 +++++++++--------- .../eclipse/jetty/util/log/StdErrLogTest.java | 207 ++++++++---- 2 files changed, 306 insertions(+), 205 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java index 58835284e46..9bbb7b4bc7d 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java @@ -93,6 +93,7 @@ import org.eclipse.jetty.util.annotation.ManagedObject; public class StdErrLog extends AbstractLogger { private static final String EOL = System.getProperty("line.separator"); + private static final Object[] EMPTY_ARGS = new Object[0]; // 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 DateCache _dateCache; @@ -131,10 +132,12 @@ public class StdErrLog extends AbstractLogger __tagpad = pad; } + @SuppressWarnings("UnusedAssignment") private int _level = LEVEL_INFO; // Level that this Logger was configured as (remembered in special case of .setDebugEnabled()) private int _configuredLevel; - private PrintStream _stderr = null; + private PrintStream _stderr = System.err; + @SuppressWarnings("UnusedAssignment") private boolean _source = __source; // Print the long form names, otherwise use abbreviated private boolean _printLongNames = __long; @@ -285,9 +288,9 @@ public class StdErrLog extends AbstractLogger { if (_level <= LEVEL_WARN) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":WARN:", msg, args); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":WARN:", msg, args); + println(builder); } } @@ -302,9 +305,9 @@ public class StdErrLog extends AbstractLogger { if (_level <= LEVEL_WARN) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":WARN:", msg, thrown); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":WARN:", msg, thrown); + println(builder); } } @@ -313,9 +316,9 @@ public class StdErrLog extends AbstractLogger { if (_level <= LEVEL_INFO) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":INFO:", msg, args); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":INFO:", msg, args); + println(builder); } } @@ -330,9 +333,9 @@ public class StdErrLog extends AbstractLogger { if (_level <= LEVEL_INFO) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":INFO:", msg, thrown); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":INFO:", msg, thrown); + println(builder); } } @@ -392,17 +395,24 @@ public class StdErrLog extends AbstractLogger public void setStdErrStream(PrintStream stream) { - this._stderr = stream == System.err ? null : stream; + if (stream == null) + { + this._stderr = System.err; + } + else + { + this._stderr = stream; + } } @Override public void debug(String msg, Object... args) { - if (_level <= LEVEL_DEBUG) + if (isDebugEnabled()) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":DBUG:", msg, args); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":DBUG:", msg, args); + println(builder); } } @@ -411,9 +421,9 @@ public class StdErrLog extends AbstractLogger { if (isDebugEnabled()) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":DBUG:", msg, arg); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":DBUG:", msg, arg); + println(builder); } } @@ -426,145 +436,119 @@ public class StdErrLog extends AbstractLogger @Override public void debug(String msg, Throwable thrown) { - if (_level <= LEVEL_DEBUG) + if (isDebugEnabled()) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":DBUG:", msg, thrown); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":DBUG:", msg, thrown); + 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(); int ms = (int)(now % 1000); String d = _dateCache.formatNow(now); - tag(buffer, d, ms, level); - format(buffer, msg, args); - } + tag(builder, d, ms, level); - private void format(StringBuilder buffer, String level, String msg, Throwable thrown) - { - format(buffer, level, msg); - if (isHideStacks()) - { - format(buffer, ": " + thrown); - } - else - { - format(buffer, thrown); - } - } + Object[] msgArgs = EMPTY_ARGS; + int msgArgsLen = 0; + Throwable cause = null; - private void tag(StringBuilder buffer, String d, int ms, String tag) - { - buffer.setLength(0); - buffer.append(d); - if (ms > 99) + if (inArgs != null) { - 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++) + msgArgs = inArgs; + msgArgsLen = inArgs.length; + if (msgArgsLen > 0 && inArgs[msgArgsLen - 1] instanceof Throwable) { - 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; + cause = (Throwable)inArgs[msgArgsLen - 1]; + msgArgsLen--; } } - buffer.append(' '); - } - - private void format(StringBuilder builder, String msg, Object... args) - { if (msg == null) { msg = ""; - for (int i = 0; i < args.length; i++) + for (int i = 0; i < msgArgsLen; i++) { + //noinspection StringConcatenationInLoop msg += "{} "; } } String braces = "{}"; int start = 0; - for (Object arg : args) + for (int i = 0; i < msgArgsLen; i++) { + Object arg = msgArgs[i]; int bracesIndex = msg.indexOf(braces, start); if (bracesIndex < 0) { escape(builder, msg.substring(start)); builder.append(" "); - builder.append(arg); + if (arg != null) + builder.append(arg); start = msg.length(); } else { escape(builder, msg.substring(start, bracesIndex)); - builder.append(arg); + if (arg != null) + builder.append(arg); start = bracesIndex + braces.length(); } } 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) { - 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 (c == '\n') @@ -587,44 +571,76 @@ public class StdErrLog extends AbstractLogger } } 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, ""); - } - - protected void format(StringBuilder buffer, Throwable thrown, String indent) - { - if (thrown == null) + builder.setLength(0); + builder.append(d); + if (ms > 99) { - buffer.append("null"); + builder.append('.'); + } + else if (ms > 9) + { + builder.append(".0"); } else { - buffer.append(EOL).append(indent); - format(buffer, thrown.toString()); - StackTraceElement[] elements = thrown.getStackTrace(); - for (int i = 0; elements != null && i < elements.length; i++) - { - buffer.append(EOL).append(indent).append("\tat "); - format(buffer, elements[i].toString()); - } + builder.append(".00"); + } + builder.append(ms).append(tag); - for (Throwable suppressed : thrown.getSuppressed()) - { - buffer.append(EOL).append(indent).append("Suppressed: "); - format(buffer, suppressed, "\t|" + indent); - } + String name = _printLongNames ? _name : _abbrevname; + String tname = Thread.currentThread().getName(); - Throwable cause = thrown.getCause(); - if (cause != null && cause != thrown) + int p = __tagpad > 0 ? (name.length() + tname.length() - __tagpad) : 0; + + if (p < 0) + { + builder + .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) { - buffer.append(EOL).append(indent).append("Caused by: "); - format(buffer, cause, indent); + 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; } } + + builder.append(' '); } /** @@ -678,9 +694,9 @@ public class StdErrLog extends AbstractLogger { if (_level <= LEVEL_ALL) { - StringBuilder buffer = new StringBuilder(64); - format(buffer, ":IGNORED:", "", ignored); - (_stderr == null ? System.err : _stderr).println(buffer); + StringBuilder builder = new StringBuilder(64); + format(builder, ":IGNORED:", "", ignored); + println(builder); } } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java index 38dc6a9d88d..9d778d86a5c 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Properties; @@ -54,7 +53,7 @@ public class StdErrLogTest } @Test - public void testStdErrLogFormat() throws UnsupportedEncodingException + public void testStdErrLogFormat() { StdErrLog log = new StdErrLog(LogTest.class.getName(), new Properties()); StdErrCapture output = new StdErrCapture(log); @@ -69,12 +68,11 @@ public class StdErrLogTest 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 format2"); 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 null null"); - output.assertContains("INFO:oejul.LogTest:tname: testing:null"); + output.assertContains("INFO:oejul.LogTest:tname: testing:test,"); + 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 public void testStdErrLogName() { - StdErrLog log = new StdErrLog("test", new Properties()); + StdErrLog log = new StdErrLog("testX", new Properties()); log.setPrintLongNames(true); StdErrCapture output = new StdErrCapture(log); - assertThat("Log.name", log.getName(), is("test")); + assertThat("Log.name", log.getName(), is("testX")); 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"); - 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 public void testStdErrThrowable() { - // Common Throwable (for test) + // The test Throwable Throwable th = new Throwable("Message"); - // Capture raw string form - StringWriter tout = new StringWriter(); - th.printStackTrace(new PrintWriter(tout)); - String ths = tout.toString(); - - // Start test - StdErrLog log = new StdErrLog("test", new Properties()); + // Initialize Logger + StdErrLog log = new StdErrLog("testX", new Properties()); StdErrCapture output = new StdErrCapture(log); - log.warn("ex", th); - output.assertContains(ths); + // Test behavior + 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); - 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()); - output.assertNotContains("Message with \033 escape"); + output.assertNotContains("Message with \b backspace"); + output.assertContains("Message with ? backspace"); + } - log.warn("ex", th); - output.assertContains("Message with ? escape"); - log.info(th.toString()); - output.assertContains("Message with ? escape"); + private String asString(Throwable cause) + { + StringWriter tout = new StringWriter(); + 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 - * - * @throws Exception failed test */ @Test - public void testParameterizedMessageNullValues() throws Exception + public void testParameterizedMessageNullValues() { StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(), new Properties()); 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) - {} {}", null, null); @@ -313,14 +409,12 @@ public class StdErrLogTest * Tests StdErrLog.warn() methods with level filtering. *

* Should always see WARN level messages, regardless of set level. - * - * @throws UnsupportedEncodingException failed test */ @Test - public void testWarnFiltering() throws UnsupportedEncodingException + public void testWarnFiltering() { 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); @@ -355,14 +449,12 @@ public class StdErrLogTest * Tests StdErrLog.info() methods with level filtering. *

* Should only see INFO level messages when level is set to {@link StdErrLog#LEVEL_INFO} and below. - * - * @throws Exception failed test */ @Test - public void testInfoFiltering() throws Exception + public void testInfoFiltering() { 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); @@ -403,14 +495,12 @@ public class StdErrLogTest /** * Tests {@link StdErrLog#LEVEL_OFF} filtering. - * - * @throws Exception failed test */ @Test - public void testOffFiltering() throws Exception + public void testOffFiltering() { 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); @@ -434,14 +524,12 @@ public class StdErrLogTest * Tests StdErrLog.debug() methods with level filtering. *

* Should only see DEBUG level messages when level is set to {@link StdErrLog#LEVEL_DEBUG} and below. - * - * @throws Exception failed test */ @Test - public void testDebugFiltering() throws Exception + public void testDebugFiltering() { 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); @@ -485,14 +573,12 @@ public class StdErrLogTest * Tests StdErrLog with {@link Logger#ignore(Throwable)} use. *

* Should only see IGNORED level messages when level is set to {@link StdErrLog#LEVEL_ALL}. - * - * @throws Exception failed test */ @Test - public void testIgnores() throws Exception + public void testIgnores() { 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); @@ -516,10 +602,10 @@ public class StdErrLogTest } @Test - public void testIsDebugEnabled() throws Exception + public void testIsDebugEnabled() { 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); assertThat("log.level(all).isDebugEnabled", log.isDebugEnabled(), is(true)); @@ -542,7 +628,7 @@ public class StdErrLogTest public void testSetGetLevel() { 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); assertThat("log.level(all).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_ALL)); @@ -566,7 +652,7 @@ public class StdErrLogTest { String baseName = "jetty"; 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")); @@ -580,7 +666,7 @@ public class StdErrLogTest { String baseName = "jetty"; 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")); @@ -594,7 +680,7 @@ public class StdErrLogTest { String baseName = "jetty"; 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")); @@ -610,7 +696,7 @@ public class StdErrLogTest { String baseName = "jetty"; 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")); @@ -626,7 +712,7 @@ public class StdErrLogTest { String baseName = "jetty"; 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")); @@ -671,7 +757,7 @@ public class StdErrLogTest } @Test - public void testPrintSource() throws UnsupportedEncodingException + public void testPrintSource() { Properties props = new Properties(); props.put("test.SOURCE", "true"); @@ -690,7 +776,6 @@ public class StdErrLogTest assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:")); props.put("test.SOURCE", "false"); - log = new StdErrLog("other", props); } @Test From 0e536407a921ab73c4e5742c47d096b365604269 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 20 Feb 2020 13:45:40 -0600 Subject: [PATCH 2/2] Issue #4567 - Backport of StdErrLog 10.0.x for Throwable behavior change Signed-off-by: Joakim Erdfelt --- .../org/eclipse/jetty/util/log/StdErrLog.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java index 9bbb7b4bc7d..efc8d3780ca 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java @@ -92,7 +92,7 @@ import org.eclipse.jetty.util.annotation.ManagedObject; @ManagedObject("Jetty StdErr Logging Implementation") 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. private static int __tagpad = Integer.parseInt(Log.__props.getProperty("org.eclipse.jetty.util.log.StdErrLog.TAG_PAD", "0")); @@ -132,13 +132,11 @@ public class StdErrLog extends AbstractLogger __tagpad = pad; } - @SuppressWarnings("UnusedAssignment") - private int _level = LEVEL_INFO; + private int _level; // Level that this Logger was configured as (remembered in special case of .setDebugEnabled()) private int _configuredLevel; private PrintStream _stderr = System.err; - @SuppressWarnings("UnusedAssignment") - private boolean _source = __source; + private boolean _source; // Print the long form names, otherwise use abbreviated private boolean _printLongNames = __long; // The full log name, as provided by the system. @@ -353,26 +351,24 @@ public class StdErrLog extends AbstractLogger @Override public void setDebugEnabled(boolean enabled) { - if (enabled) - { - this._level = LEVEL_DEBUG; + int level = enabled ? LEVEL_DEBUG : this.getConfiguredLevel(); + this.setLevel(level); - for (Logger log : Log.getLoggers().values()) + String name = getName(); + for (Logger log : Log.getLoggers().values()) + { + if (log.getName().startsWith(name) && log instanceof StdErrLog) { - if (log.getName().startsWith(getName()) && log instanceof StdErrLog) - ((StdErrLog)log).setLevel(LEVEL_DEBUG); + StdErrLog logger = (StdErrLog)log; + level = enabled ? LEVEL_DEBUG : logger.getConfiguredLevel(); + logger.setLevel(level); } } - else - { - this._level = this._configuredLevel; + } - for (Logger log : Log.getLoggers().values()) - { - if (log.getName().startsWith(getName()) && log instanceof StdErrLog) - ((StdErrLog)log).setLevel(((StdErrLog)log)._configuredLevel); - } - } + private int getConfiguredLevel() + { + return _configuredLevel; } public int getLevel()