Merge pull request #4997 from eclipse/jetty-10.0.x-4572-message-indent
Issue #4572 - Implementing old logging TAG_PAD as Message Alignment.
This commit is contained in:
commit
c0dc907a30
|
@ -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());
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under
|
||||
// the terms of the Eclipse Public License 2.0 which is available at
|
||||
// https://www.eclipse.org/legal/epl-2.0
|
||||
//
|
||||
// This Source Code may also be made available under the following
|
||||
// Secondary Licenses when the conditions for such availability set
|
||||
// forth in the Eclipse Public License, v. 2.0 are satisfied:
|
||||
// the Apache License v2.0 which is available at
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.logging.jmh;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Threads;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import org.openjdk.jmh.profile.GCProfiler;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@Threads(4)
|
||||
@Warmup(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
@Measurement(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||
public class IndentBenchmark
|
||||
{
|
||||
private static final int SMALL = 13;
|
||||
private static final int LARGE = 43;
|
||||
private String bigstring = " ";
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void testStringSubStringSmall(Blackhole blackhole)
|
||||
{
|
||||
blackhole.consume(bigstring.substring(0, SMALL));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void testStringSubStringLarge(Blackhole blackhole)
|
||||
{
|
||||
blackhole.consume(bigstring.substring(0, LARGE));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void testStringRepeatSmall(Blackhole blackhole)
|
||||
{
|
||||
blackhole.consume(" ".repeat(SMALL));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void testStringRepeatLarge(Blackhole blackhole)
|
||||
{
|
||||
blackhole.consume(" ".repeat(LARGE));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException
|
||||
{
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(IndentBenchmark.class.getSimpleName())
|
||||
.addProfiler(GCProfiler.class)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue