Repository: logging-log4j2 Updated Branches: refs/heads/master c185dfb55 -> cbeab8131
[LOG4J2-984] PatternLayout %highlight to support noConsoleNoAnsi like %style. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/cbeab813 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/cbeab813 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/cbeab813 Branch: refs/heads/master Commit: cbeab8131b5d4a9cf215ca5b2c44a201db97aba4 Parents: c185dfb Author: Gary Gregory <[email protected]> Authored: Wed Mar 25 16:49:15 2015 -0700 Committer: Gary Gregory <[email protected]> Committed: Wed Mar 25 16:49:15 2015 -0700 ---------------------------------------------------------------------- .../log4j/core/pattern/HighlightConverter.java | 20 +++++-- .../log4j/core/pattern/NoConsoleNoAnsiTest.java | 61 ++++++++++++++++++++ .../log4j2-console-noConsoleNoAnsi.xml | 34 +++++++++++ src/changes/changes.xml | 3 + 4 files changed, 114 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cbeab813/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java index 3fc0ef0..02f994d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java @@ -182,23 +182,31 @@ public final class HighlightConverter extends LogEventPatternConverter implement } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); - return new HighlightConverter(formatters, createLevelStyleMap(options)); + final boolean noConsoleNoAnsi = options.length > 1 + && (PatternParser.NO_CONSOLE_NO_ANSI + "=true").equals(options[1]); + final boolean hideAnsi = noConsoleNoAnsi && System.console() == null; + return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi); } private final Map<Level, String> levelStyles; private final List<PatternFormatter> patternFormatters; + private boolean noAnsi; + /** * Construct the converter. * * @param patternFormatters * The PatternFormatters to generate the text to manipulate. + * @param noAnsi + * If true, do not output ANSI escape codes. */ - private HighlightConverter(final List<PatternFormatter> patternFormatters, final Map<Level, String> levelStyles) { + private HighlightConverter(final List<PatternFormatter> patternFormatters, final Map<Level, String> levelStyles, final boolean noAnsi) { super("style", "style"); this.patternFormatters = patternFormatters; this.levelStyles = levelStyles; + this.noAnsi = noAnsi; } /** @@ -212,8 +220,12 @@ public final class HighlightConverter extends LogEventPatternConverter implement } if (buf.length() > 0) { - toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). - append(AnsiEscape.getDefaultStyle()); + if (noAnsi) { + toAppendTo.append(buf.toString()); + } else { + toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). + append(AnsiEscape.getDefaultStyle()); + } } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cbeab813/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NoConsoleNoAnsiTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NoConsoleNoAnsiTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NoConsoleNoAnsiTest.java new file mode 100644 index 0000000..bdf50d6 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/NoConsoleNoAnsiTest.java @@ -0,0 +1,61 @@ +/* + * 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.logging.log4j.core.pattern; + +import java.util.List; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.junit.InitialLoggerContext; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class NoConsoleNoAnsiTest { + + private static final String EXPECTED = + "ERROR LoggerTest o.a.l.l.c.p.NoConsoleNoAnsiTest org.apache.logging.log4j.core.pattern.NoConsoleNoAnsiTest" + + Constants.LINE_SEPARATOR; + + @Rule + public InitialLoggerContext init = new InitialLoggerContext("log4j2-console-noConsoleNoAnsi.xml"); + + private Logger logger; + private ListAppender app; + + @Before + public void setUp() throws Exception { + this.logger = this.init.getLogger("LoggerTest"); + this.app = this.init.getListAppender("List").clear(); + } + + @Test + public void testReplacement() { + logger.error(this.getClass().getName()); + + final List<String> msgs = app.getMessages(); + assertNotNull(msgs); + assertEquals("Incorrect number of messages. Should be 1 is " + msgs.size(), 1, msgs.size()); + assertTrue("Replacement failed - expected ending " + EXPECTED + ", actual " + msgs.get(0), msgs.get(0).endsWith(EXPECTED)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cbeab813/log4j-core/src/test/resources/log4j2-console-noConsoleNoAnsi.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j2-console-noConsoleNoAnsi.xml b/log4j-core/src/test/resources/log4j2-console-noConsoleNoAnsi.xml new file mode 100644 index 0000000..dcdb6d6 --- /dev/null +++ b/log4j-core/src/test/resources/log4j2-console-noConsoleNoAnsi.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + +--> +<Configuration status="OFF" name="NoConsoleNoAnsiTest"> + <Appenders> + <List name="List"> + <PatternLayout noConsoleNoAnsi="true"> + <Pattern>%d %highlight{%p} %style{%logger}{bright,cyan} %C{1.} %msg%n</Pattern> + </PatternLayout> + </List> + </Appenders> + + <Loggers> + <Root level="trace"> + <AppenderRef ref="List"/> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cbeab813/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b6b16c6..dfe9131 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ </properties> <body> <release version="2.3" date="2015-0?-??" description="GA Release 2.3"> + <action issue="LOG4J2-984" dev="ggregory" type="add" due-to="Jonas Höpfner"> + PatternLayout %highlight to support noConsoleNoAnsi like %style. + </action> <action issue="LOG4J2-926" dev="ggregory" type="add" due-to="David Ohana"> Truncate from the end of text format modifier. </action>
