From e929d0c5d13ccc04e6a596e1b1dbaa6dc626ddd0 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 24 Jun 2020 10:54:33 -0500 Subject: [PATCH] Issue #4572 - Implementing old logging TAG_PAD as Message Alignment. Signed-off-by: Joakim Erdfelt --- .../eclipse/jetty/logging/StdErrAppender.java | 20 +++++--- .../logging/JettyLoggerConfigurationTest.java | 4 +- .../jetty/logging/JettyLoggerTest.java | 51 +++++++++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/StdErrAppender.java b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/StdErrAppender.java index b851e76a4dd..d2e49e306fa 100644 --- a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/StdErrAppender.java +++ b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/StdErrAppender.java @@ -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()); diff --git a/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerConfigurationTest.java b/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerConfigurationTest.java index cdbdd5ebede..fd938c075a7 100644 --- a/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerConfigurationTest.java +++ b/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerConfigurationTest.java @@ -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); diff --git a/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerTest.java b/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerTest.java index 02a0167f45c..25228802a09 100644 --- a/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerTest.java +++ b/jetty-slf4j-impl/src/test/java/org/eclipse/jetty/logging/JettyLoggerTest.java @@ -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 */