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 @@ } final PatternParser parser = PatternLayout.createPatternParser(config); final List 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 levelStyles; private final List 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 patternFormatters, final Map levelStyles) { + private HighlightConverter(final List patternFormatters, final Map levelStyles, final boolean noAnsi) { super("style", "style"); this.patternFormatters = patternFormatters; this.levelStyles = levelStyles; + this.noAnsi = noAnsi; } /** @@ -212,8 +220,12 @@ } 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()); + } } } 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..b7c0810 --- /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 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)); + } + +} 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..59adbc5 --- /dev/null +++ b/log4j-core/src/test/resources/log4j2-console-noConsoleNoAnsi.xml @@ -0,0 +1,34 @@ + + + + + + + %d %highlight{%p} %style{%logger}{bright,cyan} %C{1.} %msg%n + + + + + + + + + + + \ No newline at end of file