Issue #4572 - Implementing old logging TAG_PAD as Message Alignment.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2020-06-24 10:54:33 -05:00
parent 22d5321628
commit e929d0c5d1
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 65 additions and 10 deletions

View File

@ -32,7 +32,7 @@ public class StdErrAppender implements JettyAppender
* Configuration keys specific to the StdErrAppender
*/
static final String NAME_CONDENSE_KEY = "org.eclipse.jetty.logging.appender.NAME_CONDENSE";
static final String THREAD_PADDING_KEY = "org.eclipse.jetty.logging.appender.THREAD_PADDING";
static final String MESSAGE_ALIGN_KEY = "org.eclipse.jetty.logging.appender.MESSAGE_ALIGN";
static final String MESSAGE_ESCAPE_KEY = "org.eclipse.jetty.logging.appender.MESSAGE_ESCAPE";
static final String ZONEID_KEY = "org.eclipse.jetty.logging.appender.ZONE_ID";
private static final String EOL = System.lineSeparator();
@ -50,9 +50,9 @@ public class StdErrAppender implements JettyAppender
private final boolean escapedMessages;
/**
* The fixed size of the thread name to use for output
* The column to align the start of all messages to
*/
private final int threadPadding;
private final int messageAlignColumn;
/**
* The stream to write logging events to.
@ -88,7 +88,7 @@ public class StdErrAppender implements JettyAppender
this.condensedNames = config.getBoolean(NAME_CONDENSE_KEY, true);
this.escapedMessages = config.getBoolean(MESSAGE_ESCAPE_KEY, true);
this.threadPadding = config.getInt(THREAD_PADDING_KEY, -1);
this.messageAlignColumn = config.getInt(MESSAGE_ALIGN_KEY, 0);
}
@Override
@ -116,9 +116,9 @@ public class StdErrAppender implements JettyAppender
return escapedMessages;
}
public int getThreadPadding()
public int getMessageAlignColumn()
{
return threadPadding;
return messageAlignColumn;
}
public PrintStream getStream()
@ -154,11 +154,15 @@ public class StdErrAppender implements JettyAppender
// Thread Name
builder.append(':');
builder.append(threadName); // TODO: support TAG_PAD configuration
builder.append(threadName);
builder.append(':');
// Message
builder.append(' ');
int padAmount = messageAlignColumn - builder.length();
if (padAmount > 0)
builder.append(" ".repeat(padAmount));
else
builder.append(' ');
FormattingTuple ft = MessageFormatter.arrayFormat(message, argumentArray);
appendEscaped(builder, ft.getMessage());

View File

@ -34,7 +34,7 @@ public class JettyLoggerConfigurationTest
Properties props = new Properties();
props.setProperty(StdErrAppender.MESSAGE_ESCAPE_KEY, "false");
props.setProperty(StdErrAppender.NAME_CONDENSE_KEY, "false");
props.setProperty(StdErrAppender.THREAD_PADDING_KEY, "10");
props.setProperty(StdErrAppender.MESSAGE_ALIGN_KEY, "10");
props.setProperty("com.mortbay.LEVEL", "WARN");
props.setProperty("com.mortbay.STACKS", "false");
@ -43,7 +43,7 @@ public class JettyLoggerConfigurationTest
assertFalse(appender.isEscapedMessages());
assertFalse(appender.isCondensedNames());
assertEquals(appender.getThreadPadding(), 10);
assertEquals(appender.getMessageAlignColumn(), 10);
JettyLevel level = config.getLevel("com.mortbay");
assertEquals(JettyLevel.WARN, level);

View File

@ -162,6 +162,57 @@ public class JettyLoggerTest
output.assertContains("Message with ? escape");
}
@Test
public void testStdErrLogMessageAlignment()
{
Properties props = new Properties();
props.setProperty(StdErrAppender.MESSAGE_ALIGN_KEY, "50");
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
JettyLoggerFactory factory = new JettyLoggerFactory(config);
StdErrAppender appender = (StdErrAppender)factory.getRootLogger().getAppender();
CapturedStream output = new CapturedStream();
appender.setStream(output);
JettyLogger logJetty = factory.getJettyLogger("jetty");
JettyLogger logJettyDeep = factory.getJettyLogger("jetty.from.deep");
JettyLogger logJettyDeeper = factory.getJettyLogger("jetty.component.deeper.still");
logJetty.setLevel(JettyLevel.DEBUG);
logJettyDeep.debug("testing {} {}", "test", "debug");
logJettyDeep.info("testing {} {}", "test", "info");
logJettyDeep.warn("testing {} {}", "test", "warn");
logJettyDeeper.debug("testing {} {}", "test", "debug");
logJettyDeeper.info("testing {} {}", "test", "info");
logJettyDeeper.warn("testing {} {}", "test", "warn");
Thread.currentThread().setName("otherThread");
logJetty.info("testing {} {}", "test", "info");
logJettyDeep.info("testing {} {}", "test", "info");
logJettyDeeper.info("testing {} {}", "test", "info");
Thread.currentThread().setName("veryLongThreadName");
logJetty.info("testing {} {}", "test", "info");
logJettyDeep.info("testing {} {}", "test", "info");
logJettyDeeper.info("testing {} {}", "test", "info");
output.assertContains("DEBUG:jf.deep:tname: testing test debug");
output.assertContains("INFO :jf.deep:tname: testing test info");
output.assertContains("WARN :jf.deep:tname: testing test warn");
output.assertContains("DEBUG:jcd.still:tname: testing test debug");
output.assertContains("INFO :jcd.still:tname: testing test info");
output.assertContains("WARN :jcd.still:tname: testing test warn");
output.assertContains("INFO :jetty:otherThread: testing test info");
output.assertContains("INFO :jf.deep:otherThread: testing test info");
output.assertContains("INFO :jcd.still:otherThread: testing test info");
output.assertContains("INFO :jetty:veryLongThreadName: testing test info");
output.assertContains("INFO :jf.deep:veryLongThreadName: testing test info");
output.assertContains("INFO :jcd.still:veryLongThreadName: testing test info");
}
/**
* Test to make sure that using a Null parameter on parameterized messages does not result in a NPE
*/