Skip to content

Commit

Permalink
Rewrite the WARN message and add additional assertion for testBadStyl…
Browse files Browse the repository at this point in the history
…eOption
  • Loading branch information
awadammar committed Aug 5, 2023
1 parent cc6d742 commit 6530e52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,20 @@ public void testNoAnsiNonEmpty(final ListStatusListener listener) {
This test ensure that a keyvalue pair separated by = sign must be provided in order to configure the highlighting style
*/
@Test
public void testBadStyleOption() {
@UsingStatusListener
public void testBadStyleOption(final ListStatusListener listener) {
String defaultWarnColor = "yellow";
String defaultInfoColor = "green";
final String[] options = {"%5level", PatternParser.NO_CONSOLE_NO_ANSI + "=false, " + PatternParser.DISABLE_ANSI
+ "=false" + "LOGBACK"};
+ "=false, " + "LOGBACK"};
final HighlightConverter converter = HighlightConverter.newInstance(null, options);
assertNotNull(converter);

// As the default highlighting WARN color is Yellow while the LOGBACK color is Red
assertEquals(AnsiEscape.createSequence(defaultWarnColor), converter.getLevelStyle(Level.WARN));
// As the default highlighting INFO color is Green while the LOGBACK color is Blue
assertEquals(AnsiEscape.createSequence(defaultInfoColor), converter.getLevelStyle(Level.INFO));
assertThat(listener.findStatusData(Level.WARN)).hasSize(1);
}

private CharSequence toFormattedCharSeq(final HighlightConverter converter, final Level level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public static Map<String, String> createMap(final String[] values, final String[
final boolean escape = Arrays.binarySearch(sortedIgnoreKeys, key) < 0;
map.put(key, escape ? createSequence(value.split("\\s")) : value);
} else {
LOGGER.warn("Usage of incorrect syntax for highlighting style: \"{}\". Provide style configuration as follows Key=Value", string);
LOGGER.warn("Syntax error, missing '=': Expected \"{KEY1=VALUE, KEY2=VALUE, ...}");
}
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.apache.logging.log4j.util.Strings;

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

Expand Down Expand Up @@ -87,6 +86,10 @@ public final class HighlightConverter extends LogEventPatternConverter implement

private static final String STYLE_KEY = "STYLE";

private static final String DISABLE_ANSI_KEY = "DISABLEANSI";

private static final String NO_CONSOLE_NO_ANSI_KEY = "NOCONSOLENOANSI";

private static final String STYLE_KEY_DEFAULT = "DEFAULT";

private static final String STYLE_KEY_LOGBACK = "LOGBACK";
Expand Down Expand Up @@ -146,11 +149,8 @@ private static Map<String, String> createLevelStyleMap(final String[] options) {
return DEFAULT_STYLES;
}
// Feels like a hack. Should String[] options change to a Map<String,String>?
final String string = options[1]
.replaceAll(PatternParser.DISABLE_ANSI + "=(true|false)", Strings.EMPTY)
.replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY);
//
final Map<String, String> styles = AnsiEscape.createMap(string, new String[] {STYLE_KEY});
final Map<String, String> styles = AnsiEscape.createMap(options[1], new String[]{STYLE_KEY, DISABLE_ANSI_KEY,
NO_CONSOLE_NO_ANSI_KEY});
final Map<String, String> levelStyles = new HashMap<>(DEFAULT_STYLES);
for (final Map.Entry<String, String> entry : styles.entrySet()) {
final String key = toRootUpperCase(entry.getKey());
Expand All @@ -163,7 +163,7 @@ private static Map<String, String> createLevelStyleMap(final String[] options) {
} else {
levelStyles.putAll(enumMap);
}
} else {
} else if (!DISABLE_ANSI_KEY.equalsIgnoreCase(key) && !NO_CONSOLE_NO_ANSI_KEY.equalsIgnoreCase(key)) {
final Level level = Level.toLevel(key, null);
if (level == null) {
LOGGER.warn("Setting style for yet unknown level name {}", key);
Expand Down

0 comments on commit 6530e52

Please sign in to comment.