Issue #4567 - null arguments on StdErrLog result in no output (avoiding "null")

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2020-02-19 15:10:20 -06:00
parent c9134df60a
commit 5df8b05327
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 147 additions and 66 deletions

View File

@ -474,12 +474,14 @@ public class StdErrLog extends AbstractLogger
{
escape(builder, msg.substring(start));
builder.append(" ");
if (arg != null)
builder.append(arg);
start = msg.length();
}
else
{
escape(builder, msg.substring(start, bracesIndex));
if (arg != null)
builder.append(arg);
start = bracesIndex + braces.length();
}

View File

@ -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,66 +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));
}
Throwable thr = new Throwable("Reasons Explained");
@Test
public void testStdErrThrowableNull()
{
// Initialize Logger
StdErrLog log = new StdErrLog("testX", new Properties());
StdErrCapture output = new StdErrCapture(log);
log.warn("Ex {}", "Reasons", thr);
// 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(thr.toString());
output.assertContains(asString(th));
}
th = new Throwable("Message with \033 escape");
@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");
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");
@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.assertContains("Message with ? escape");
output.assertNotContains("Message with \b backspace");
output.assertContains("Message with ? backspace");
}
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);
@ -319,14 +409,12 @@ public class StdErrLogTest
* Tests StdErrLog.warn() methods with level filtering.
* <p>
* 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);
@ -361,14 +449,12 @@ public class StdErrLogTest
* Tests StdErrLog.info() methods with level filtering.
* <p>
* 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);
@ -409,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);
@ -440,14 +524,12 @@ public class StdErrLogTest
* Tests StdErrLog.debug() methods with level filtering.
* <p>
* 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);
@ -491,14 +573,12 @@ public class StdErrLogTest
* Tests StdErrLog with {@link Logger#ignore(Throwable)} use.
* <p>
* 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);
@ -522,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));
@ -548,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));
@ -572,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"));
@ -586,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"));
@ -600,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"));
@ -616,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"));
@ -632,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"));
@ -677,7 +757,7 @@ public class StdErrLogTest
}
@Test
public void testPrintSource() throws UnsupportedEncodingException
public void testPrintSource()
{
Properties props = new Properties();
props.put("test.SOURCE", "true");
@ -696,7 +776,6 @@ public class StdErrLogTest
assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
props.put("test.SOURCE", "false");
log = new StdErrLog("other", props);
}
@Test