Skip to content

Commit c971b2c

Browse files
awadammarvy
authored andcommitted
Warn on incorrect highlight style (#1545, #1637)
1 parent 6ddb544 commit c971b2c

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ public void testNoAnsiNonEmpty(final ListStatusListener listener) {
171171
assertEquals("INFO : message in a bottle", buffer.toString());
172172
}
173173

174+
/*
175+
This test ensure that a keyvalue pair separated by = sign must be provided in order to configure the highlighting style
176+
*/
177+
@Test
178+
@UsingStatusListener
179+
public void testBadStyleOption(final ListStatusListener listener) {
180+
String defaultWarnColor = "yellow";
181+
String defaultInfoColor = "green";
182+
final String[] options = {"%5level", PatternParser.NO_CONSOLE_NO_ANSI + "=false, " + PatternParser.DISABLE_ANSI
183+
+ "=false, " + "LOGBACK"};
184+
final HighlightConverter converter = HighlightConverter.newInstance(null, options);
185+
assertNotNull(converter);
186+
187+
// As the default highlighting WARN color is Yellow while the LOGBACK color is Red
188+
assertEquals(AnsiEscape.createSequence(defaultWarnColor), converter.getLevelStyle(Level.WARN));
189+
// As the default highlighting INFO color is Green while the LOGBACK color is Blue
190+
assertEquals(AnsiEscape.createSequence(defaultInfoColor), converter.getLevelStyle(Level.INFO));
191+
assertThat(listener.findStatusData(Level.WARN)).hasSize(1);
192+
}
193+
174194
private CharSequence toFormattedCharSeq(final HighlightConverter converter, final Level level) {
175195
final StringBuilder sb = new StringBuilder();
176196
converter.format(Log4jLogEvent.newBuilder().setLevel(level).build(), sb);

log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ public enum AnsiEscape {
347347
* Bright white background color.
348348
*/
349349
BG_BRIGHT_WHITE("107");
350+
private static final StatusLogger LOGGER = StatusLogger.getLogger();
350351

351352
private static final String DEFAULT_STYLE = CSI.getCode() + SUFFIX.getCode();
352353

@@ -431,6 +432,8 @@ public static Map<String, String> createMap(final String[] values, final String[
431432
final String value = keyValue[1];
432433
final boolean escape = Arrays.binarySearch(sortedIgnoreKeys, key) < 0;
433434
map.put(key, escape ? createSequence(value.split("\\s")) : value);
435+
} else {
436+
LOGGER.warn("Syntax error, missing '=': Expected \"{KEY1=VALUE, KEY2=VALUE, ...}");
434437
}
435438
}
436439
return map;

log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.logging.log4j.core.config.plugins.Plugin;
2828
import org.apache.logging.log4j.core.layout.PatternLayout;
2929
import org.apache.logging.log4j.util.PerformanceSensitive;
30-
import org.apache.logging.log4j.util.Strings;
3130

3231
import static org.apache.logging.log4j.util.Strings.toRootUpperCase;
3332

@@ -87,6 +86,10 @@ public final class HighlightConverter extends LogEventPatternConverter implement
8786

8887
private static final String STYLE_KEY = "STYLE";
8988

89+
private static final String DISABLE_ANSI_KEY = "DISABLEANSI";
90+
91+
private static final String NO_CONSOLE_NO_ANSI_KEY = "NOCONSOLENOANSI";
92+
9093
private static final String STYLE_KEY_DEFAULT = "DEFAULT";
9194

9295
private static final String STYLE_KEY_LOGBACK = "LOGBACK";
@@ -146,11 +149,8 @@ private static Map<String, String> createLevelStyleMap(final String[] options) {
146149
return DEFAULT_STYLES;
147150
}
148151
// Feels like a hack. Should String[] options change to a Map<String,String>?
149-
final String string = options[1]
150-
.replaceAll(PatternParser.DISABLE_ANSI + "=(true|false)", Strings.EMPTY)
151-
.replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY);
152-
//
153-
final Map<String, String> styles = AnsiEscape.createMap(string, new String[] {STYLE_KEY});
152+
final Map<String, String> styles = AnsiEscape.createMap(options[1], new String[]{STYLE_KEY, DISABLE_ANSI_KEY,
153+
NO_CONSOLE_NO_ANSI_KEY});
154154
final Map<String, String> levelStyles = new HashMap<>(DEFAULT_STYLES);
155155
for (final Map.Entry<String, String> entry : styles.entrySet()) {
156156
final String key = toRootUpperCase(entry.getKey());
@@ -163,7 +163,7 @@ private static Map<String, String> createLevelStyleMap(final String[] options) {
163163
} else {
164164
levelStyles.putAll(enumMap);
165165
}
166-
} else {
166+
} else if (!DISABLE_ANSI_KEY.equalsIgnoreCase(key) && !NO_CONSOLE_NO_ANSI_KEY.equalsIgnoreCase(key)) {
167167
final Level level = Level.toLevel(key, null);
168168
if (level == null) {
169169
LOGGER.warn("Setting style for yet unknown level name {}", key);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.0.xsd"
21+
type="fixed">
22+
<issue id="1545" link="https://github.com/apache/logging-log4j2/issues/1545"/>
23+
<author id="aawad6"/>
24+
<author name="Ammar Awad"/>
25+
<description format="asciidoc">
26+
Add a warning in case of incorrect syntax of highlighting style (without =).
27+
e.g. %highlight{%5level}{LOGBACK} should be %highlight{%5level}{STYLE=LOGBACK}
28+
</description>
29+
</entry>

0 commit comments

Comments
 (0)