Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public static Stream<Arguments> testRendering() {
"\u001b[37mkey\u001b[m = \u001b[36;1msome value\u001b[m"),
// Return broken escapes as is
Arguments.of("", "Hello @|crazy|@ world!", "Hello @|crazy|@ world!"),
Arguments.of("", "Hello @|world!", "Hello @|world!"));
Arguments.of("", "Hello @|world!", "Hello @|world!"),
// Predefined Style=Spock: Name is BG_RED + WHITE
Arguments.of("Style=Spock", "@|Name XYZ|@", "\u001b[41;37mXYZ\u001b[m"),
// Predefined Style=Kirk: Name is BG_RED + YELLOW + BOLD
Arguments.of("Style=Kirk", "@|Name XYZ|@", "\u001b[41;33;1mXYZ\u001b[m"),
// User-supplied ANSI names still work when a predefined style is selected
Arguments.of("Style=Spock", "@|bg_red,white XYZ|@", "\u001b[41;37mXYZ\u001b[m"));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ public static Map<String, String> createMap(final String[] values, final String[
static Map<String, String> createMap(
final String[] values, final String[] dontEscapeKeys, final String separatorRegex) {
final String[] sortedIgnoreKeys = dontEscapeKeys != null ? dontEscapeKeys.clone() : Strings.EMPTY_ARRAY;
for (int i = 0; i < sortedIgnoreKeys.length; i++) {
sortedIgnoreKeys[i] = toRootUpperCase(sortedIgnoreKeys[i]);
}
Arrays.sort(sortedIgnoreKeys);
final Map<String, String> map = new HashMap<>();
for (final String string : values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public final class JAnsiTextRenderer implements TextRenderer {
private static final int CSI_LENGTH = 2;

private static Map.Entry<String, String> entry(final String name, final AnsiEscape... codes) {
final StringBuilder sb = new StringBuilder(AnsiEscape.CSI.getCode());
for (final AnsiEscape code : codes) {
sb.append(code.getCode());
final String[] names = new String[codes.length];
for (int i = 0; i < codes.length; i++) {
names[i] = codes[i].name();
}
return new AbstractMap.SimpleImmutableEntry<>(name, sb.toString());
return new AbstractMap.SimpleImmutableEntry<>(name, AnsiEscape.createSequence(names));
}

@SafeVarargs
Expand Down Expand Up @@ -201,20 +201,20 @@ public JAnsiTextRenderer(final String[] formats, final Map<String, String> defau
if (formats.length > 1) {
final String stylesStr = formats[1];
final Map<String, String> map = AnsiEscape.createMap(
stylesStr.split("\\s", -1), new String[] {"BeginToken", "EndToken", "Style"}, ",");
stylesStr.split("\\s", -1), new String[] {"BEGINTOKEN", "ENDTOKEN", "STYLE"}, ",");

// Handle the special tokens
beginToken = Objects.toString(map.remove("BeginToken"), BEGIN_TOKEN);
endToken = Objects.toString(map.remove("EndToken"), END_TOKEN);
final String predefinedStyle = map.remove("Style");
// Handle the special tokens. createMap stores keys in root upper-case.
beginToken = Objects.toString(map.remove("BEGINTOKEN"), BEGIN_TOKEN);
endToken = Objects.toString(map.remove("ENDTOKEN"), END_TOKEN);
final String predefinedStyle = map.remove("STYLE");

// Create style map
final Map<String, String> styleMap = new HashMap<>(map.size() + defaultStyleMap.size());
defaultStyleMap.forEach((k, v) -> styleMap.put(toRootUpperCase(k), v));
if (predefinedStyle != null) {
final Map<String, String> predefinedMap = PREFEDINED_STYLE_MAPS.get(predefinedStyle);
if (predefinedMap != null) {
map.putAll(predefinedMap);
predefinedMap.forEach((k, v) -> map.put(toRootUpperCase(k), v));
} else {
LOGGER.warn(
"Unknown predefined map name {}, pick one of {}",
Expand Down
13 changes: 13 additions & 0 deletions src/changelog/.2.x.x/4254_fix_predefined_ansi_styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="4254" link="https://github.com/apache/logging-log4j2/issues/4254"/>
Comment thread
vy marked this conversation as resolved.
<issue id="4259" link="https://github.com/apache/logging-log4j2/pull/4259"/>
<description format="asciidoc">
Apply predefined ANSI styles (`Style=Spock`, `Style=Kirk`) in `JAnsiTextRenderer` and emit valid SGR sequences for those styles.
</description>
</entry>
Loading