Remove TTCCLayout and use PatternLayout instead
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2c019d6d Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2c019d6d Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2c019d6d Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure Commit: 2c019d6dd470e896161d50d08874acbc4317f7bc Parents: f07465b Author: Mikael StÃ¥ldal <mikael.stal...@magine.com> Authored: Thu Aug 18 14:46:09 2016 +0200 Committer: Mikael StÃ¥ldal <mikael.stal...@magine.com> Committed: Thu Aug 18 14:46:09 2016 +0200 ---------------------------------------------------------------------- .../config/Log4j1ConfigurationFactory.java | 21 +-- .../org/apache/log4j/layout/TTCCLayout.java | 131 ------------------- .../config/Log4j1ConfigurationFactoryTest.java | 15 +-- .../org/apache/log4j/layout/TTCCLayoutTest.java | 82 ------------ 4 files changed, 19 insertions(+), 230 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2c019d6d/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java index f8067e5..74563a5 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationFactory.java @@ -124,14 +124,19 @@ public class Log4j1ConfigurationFactory extends ConfigurationFactory { break; } case "org.apache.log4j.TTCCLayout": { - final LayoutComponentBuilder ttccLayout = builder.newLayout("TTCCLayout"); - ttccLayout.addAttribute("threadPrinting", - Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.ThreadPrinting", "true"))); - ttccLayout.addAttribute("categoryPrefixing", - Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.CategoryPrefixing", "true"))); - ttccLayout.addAttribute("contextPrinting", - Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.ContextPrinting", "true"))); - appenderBuilder.add(ttccLayout); + String pattern = "%r "; + if (Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.ThreadPrinting", "true"))) { + pattern += "[%t] "; + } + pattern += "%p "; + if (Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.CategoryPrefixing", "true"))) { + pattern += "%c "; + } + if (Boolean.parseBoolean(getLog4jAppenderValue(properties, name, "layout.ContextPrinting", "true"))) { + pattern += "%notEmpty{%ndc }"; + } + pattern += "- %m%n"; + appenderBuilder.add(newPatternLayout(builder, pattern)); break; } case "org.apache.log4j.HTMLLayout": { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2c019d6d/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java b/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java deleted file mode 100644 index 05aa2fc..0000000 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/layout/TTCCLayout.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.log4j.layout; - -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.config.Node; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; -import org.apache.logging.log4j.core.layout.AbstractStringLayout; -import org.apache.logging.log4j.core.layout.ByteBufferDestination; -import org.apache.logging.log4j.core.util.Constants; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.util.StringBuilderFormattable; - -import java.nio.charset.StandardCharsets; -import java.util.List; - -/** - * Port of TTCCLayout in Log4j 1.x. Provided for compatibility with existing Log4j 1 configurations. - * - * Originally developed by Ceki Gülcü, Heinz Richter, Christopher Williams, Mathias Bogaert. - */ -@Plugin(name = "TTCCLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) -public final class TTCCLayout extends AbstractStringLayout { - - private final boolean threadPrinting; - private final boolean categoryPrefixing; - private final boolean contextPrinting; - - final long startTime = System.currentTimeMillis(); - - @PluginFactory - public static TTCCLayout createLayout( - // @formatter:off - @PluginAttribute(value = "threadPrinting", defaultBoolean = true) final boolean threadPrinting, - @PluginAttribute(value = "categoryPrefixing", defaultBoolean = true) final boolean categoryPrefixing, - @PluginAttribute(value = "contextPrinting", defaultBoolean = true) final boolean contextPrinting - // @formatter:on - ) { - return new TTCCLayout(threadPrinting, categoryPrefixing, contextPrinting); - } - - private TTCCLayout(boolean threadPrinting, boolean categoryPrefixing, boolean contextPrinting) { - super(StandardCharsets.UTF_8); - this.threadPrinting = threadPrinting; - this.categoryPrefixing = categoryPrefixing; - this.contextPrinting = contextPrinting; - } - - public boolean isThreadPrinting() { - return threadPrinting; - } - - public boolean isCategoryPrefixing() { - return categoryPrefixing; - } - - public boolean isContextPrinting() { - return contextPrinting; - } - - @Override - public void encode(final LogEvent event, final ByteBufferDestination destination) { - final StringBuilder text = getStringBuilder(); - formatTo(event, text); - getStringBuilderEncoder().encode(text, destination); - } - - @Override - public String toSerializable(final LogEvent event) { - final StringBuilder text = getStringBuilder(); - formatTo(event, text); - return text.toString(); - } - - private void formatTo(final LogEvent event, final StringBuilder buf) { - buf.append(event.getTimeMillis() - startTime); - buf.append(' '); - - if (threadPrinting) { - buf.append('['); - buf.append(event.getThreadName()); - buf.append("] "); - } - - buf.append(event.getLevel().toString()); - buf.append(' '); - - if (categoryPrefixing) { - buf.append(event.getLoggerName()); - buf.append(' '); - } - - if (this.contextPrinting) { - List<String> ndc = event.getContextStack().asList(); - if (!ndc.isEmpty()) { - for (String ndcElement : ndc) { - buf.append(ndcElement); - buf.append(' '); - } - } - } - - buf.append("- "); - final Message message = event.getMessage(); - if (message instanceof StringBuilderFormattable) { - ((StringBuilderFormattable)message).formatTo(buf); - } else { - buf.append(message.getFormattedMessage()); - } - - buf.append(Constants.LINE_SEPARATOR); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2c019d6d/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java index 87fb70b..9f34563 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java @@ -16,8 +16,9 @@ */ package org.apache.log4j.config; +import java.net.URL; + import org.apache.log4j.layout.Log4j1XmlLayout; -import org.apache.log4j.layout.TTCCLayout; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.appender.ConsoleAppender; @@ -28,8 +29,6 @@ import org.apache.logging.log4j.core.layout.HtmlLayout; import org.apache.logging.log4j.core.layout.PatternLayout; import org.junit.Test; -import java.net.URL; - import static org.junit.Assert.*; public class Log4j1ConfigurationFactoryTest { @@ -60,7 +59,7 @@ public class Log4j1ConfigurationFactoryTest { @Test public void testConsoleHtmlLayout() throws Exception { - final HtmlLayout layout = (HtmlLayout)testConsole("config-1.2/log4j-console-HtmlLayout.properties"); + final HtmlLayout layout = (HtmlLayout) testConsole("config-1.2/log4j-console-HtmlLayout.properties"); assertEquals("Headline", layout.getTitle()); assertTrue(layout.isLocationInfo()); } @@ -79,15 +78,13 @@ public class Log4j1ConfigurationFactoryTest { @Test public void testConsoleTtccLayout() throws Exception { - final TTCCLayout layout = (TTCCLayout)testConsole("config-1.2/log4j-console-TTCCLayout.properties"); - assertTrue(layout.isThreadPrinting()); - assertFalse(layout.isCategoryPrefixing()); - assertTrue(layout.isContextPrinting()); + final PatternLayout layout = (PatternLayout) testConsole("config-1.2/log4j-console-TTCCLayout.properties"); + assertEquals("%r [%t] %p %notEmpty{%ndc }- %m%n", layout.getConversionPattern()); } @Test public void testConsoleXmlLayout() throws Exception { - final Log4j1XmlLayout layout = (Log4j1XmlLayout)testConsole("config-1.2/log4j-console-XmlLayout.properties"); + final Log4j1XmlLayout layout = (Log4j1XmlLayout) testConsole("config-1.2/log4j-console-XmlLayout.properties"); assertTrue(layout.isLocationInfo()); assertFalse(layout.isProperties()); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2c019d6d/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java deleted file mode 100644 index cc6af0b..0000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/layout/TTCCLayoutTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.log4j.layout; - -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.spi.DefaultThreadContextStack; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class TTCCLayoutTest { - - @Test - public void test0() { - DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); - contextStack.clear(); - test(contextStack, ""); - } - - @Test - public void test1() { - DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); - contextStack.clear(); - contextStack.push("foo"); - test(contextStack, "foo "); - } - - @Test - public void test2() { - DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); - contextStack.clear(); - contextStack.push("foo"); - contextStack.push("bar"); - test(contextStack, "foo bar "); - } - - private void test(ThreadContext.ContextStack contextStack, String stackOutput) { - TTCCLayout layout = TTCCLayout.createLayout(true, true, true); - - Log4jLogEvent event = Log4jLogEvent.newBuilder() - .setLoggerName("a.B") - .setLevel(org.apache.logging.log4j.Level.DEBUG) - .setMessage(new SimpleMessage("Msg")) - .setContextStack(contextStack) - .setTimeMillis(System.currentTimeMillis() + 17).build(); - - String result = layout.toSerializable(event); - - String expected = String.valueOf(event.getTimeMillis() - layout.startTime) + - ' ' + - '[' + - event.getThreadName() + - "] " + - event.getLevel().toString() + - ' ' + - event.getLoggerName() + - ' ' + - stackOutput + - "- " + - event.getMessage() + - System.getProperty("line.separator"); - - assertEquals(expected, result); - } - -}