Skip to content

Commit 6cf08a3

Browse files
committed
Polish "Add support for additional colors in Log4j2 and Logback"
See gh-34749
1 parent 35994bb commit 6cf08a3

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,23 +53,23 @@ public final class ColorConverter extends LogEventPatternConverter {
5353

5454
static {
5555
Map<String, AnsiElement> ansiElements = new HashMap<>();
56+
ansiElements.put("black", AnsiColor.BLACK);
57+
ansiElements.put("white", AnsiColor.WHITE);
5658
ansiElements.put("faint", AnsiStyle.FAINT);
5759
ansiElements.put("red", AnsiColor.RED);
5860
ansiElements.put("green", AnsiColor.GREEN);
5961
ansiElements.put("yellow", AnsiColor.YELLOW);
6062
ansiElements.put("blue", AnsiColor.BLUE);
6163
ansiElements.put("magenta", AnsiColor.MAGENTA);
6264
ansiElements.put("cyan", AnsiColor.CYAN);
63-
ansiElements.put("white", AnsiColor.WHITE);
64-
ansiElements.put("black", AnsiColor.BLACK);
6565
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
66+
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
6667
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
6768
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
6869
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
6970
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
7071
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
7172
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
72-
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
7373
ELEMENTS = Collections.unmodifiableMap(ansiElements);
7474
}
7575

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ColorConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,23 +43,23 @@ public class ColorConverter extends CompositeConverter<ILoggingEvent> {
4343

4444
static {
4545
Map<String, AnsiElement> ansiElements = new HashMap<>();
46+
ansiElements.put("black", AnsiColor.BLACK);
47+
ansiElements.put("white", AnsiColor.WHITE);
4648
ansiElements.put("faint", AnsiStyle.FAINT);
4749
ansiElements.put("red", AnsiColor.RED);
4850
ansiElements.put("green", AnsiColor.GREEN);
4951
ansiElements.put("yellow", AnsiColor.YELLOW);
5052
ansiElements.put("blue", AnsiColor.BLUE);
5153
ansiElements.put("magenta", AnsiColor.MAGENTA);
5254
ansiElements.put("cyan", AnsiColor.CYAN);
53-
ansiElements.put("white", AnsiColor.WHITE);
54-
ansiElements.put("black", AnsiColor.BLACK);
5555
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
56+
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
5657
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
5758
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
5859
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
5960
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
6061
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
6162
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
62-
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
6363
ELEMENTS = Collections.unmodifiableMap(ansiElements);
6464
}
6565

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/ColorConverterTests.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ private ColorConverter newConverter(String styling) {
5757
return ColorConverter.newInstance(null, new String[] { this.in, styling });
5858
}
5959

60+
@Test
61+
void black() {
62+
StringBuilder output = new StringBuilder();
63+
newConverter("black").format(this.event, output);
64+
assertThat(output).hasToString("\033[30min\033[0;39m");
65+
}
66+
67+
@Test
68+
void white() {
69+
StringBuilder output = new StringBuilder();
70+
newConverter("white").format(this.event, output);
71+
assertThat(output).hasToString("\033[37min\033[0;39m");
72+
}
73+
6074
@Test
6175
void faint() {
6276
StringBuilder output = new StringBuilder();
@@ -107,24 +121,17 @@ void cyan() {
107121
}
108122

109123
@Test
110-
void white() {
111-
StringBuilder output = new StringBuilder();
112-
newConverter("white").format(this.event, output);
113-
assertThat(output).hasToString("\033[37min\033[0;39m");
114-
}
115-
116-
@Test
117-
void black() {
124+
void brightBlack() {
118125
StringBuilder output = new StringBuilder();
119-
newConverter("black").format(this.event, output);
120-
assertThat(output).hasToString("\033[30min\033[0;39m");
126+
newConverter("bright_black").format(this.event, output);
127+
assertThat(output).hasToString("\033[90min\033[0;39m");
121128
}
122129

123130
@Test
124-
void brightBlack() {
131+
void brightWhite() {
125132
StringBuilder output = new StringBuilder();
126-
newConverter("bright_black").format(this.event, output);
127-
assertThat(output).hasToString("\033[90min\033[0;39m");
133+
newConverter("bright_white").format(this.event, output);
134+
assertThat(output).hasToString("\033[97min\033[0;39m");
128135
}
129136

130137
@Test
@@ -169,13 +176,6 @@ void brightCyan() {
169176
assertThat(output).hasToString("\033[96min\033[0;39m");
170177
}
171178

172-
@Test
173-
void brightWhite() {
174-
StringBuilder output = new StringBuilder();
175-
newConverter("bright_white").format(this.event, output);
176-
assertThat(output).hasToString("\033[97min\033[0;39m");
177-
}
178-
179179
@Test
180180
void highlightFatal() {
181181
this.event.setLevel(Level.FATAL);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/ColorConverterTests.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,20 @@ static void resetAnsi() {
5151
AnsiOutput.setEnabled(AnsiOutput.Enabled.DETECT);
5252
}
5353

54+
@Test
55+
void black() {
56+
this.converter.setOptionList(Collections.singletonList("black"));
57+
String out = this.converter.transform(this.event, this.in);
58+
assertThat(out).isEqualTo("\033[30min\033[0;39m");
59+
}
60+
61+
@Test
62+
void white() {
63+
this.converter.setOptionList(Collections.singletonList("white"));
64+
String out = this.converter.transform(this.event, this.in);
65+
assertThat(out).isEqualTo("\033[37min\033[0;39m");
66+
}
67+
5468
@Test
5569
void faint() {
5670
this.converter.setOptionList(Collections.singletonList("faint"));
@@ -101,24 +115,17 @@ void cyan() {
101115
}
102116

103117
@Test
104-
void white() {
105-
this.converter.setOptionList(Collections.singletonList("white"));
106-
String out = this.converter.transform(this.event, this.in);
107-
assertThat(out).isEqualTo("\033[37min\033[0;39m");
108-
}
109-
110-
@Test
111-
void black() {
112-
this.converter.setOptionList(Collections.singletonList("black"));
118+
void brightBlack() {
119+
this.converter.setOptionList(Collections.singletonList("bright_black"));
113120
String out = this.converter.transform(this.event, this.in);
114-
assertThat(out).isEqualTo("\033[30min\033[0;39m");
121+
assertThat(out).isEqualTo("\033[90min\033[0;39m");
115122
}
116123

117124
@Test
118-
void brightBlack() {
119-
this.converter.setOptionList(Collections.singletonList("bright_black"));
125+
void brightWhite() {
126+
this.converter.setOptionList(Collections.singletonList("bright_white"));
120127
String out = this.converter.transform(this.event, this.in);
121-
assertThat(out).isEqualTo("\033[90min\033[0;39m");
128+
assertThat(out).isEqualTo("\033[97min\033[0;39m");
122129
}
123130

124131
@Test
@@ -163,13 +170,6 @@ void brightCyan() {
163170
assertThat(out).isEqualTo("\033[96min\033[0;39m");
164171
}
165172

166-
@Test
167-
void brightWhite() {
168-
this.converter.setOptionList(Collections.singletonList("bright_white"));
169-
String out = this.converter.transform(this.event, this.in);
170-
assertThat(out).isEqualTo("\033[97min\033[0;39m");
171-
}
172-
173173
@Test
174174
void highlightError() {
175175
this.event.setLevel(Level.ERROR);

0 commit comments

Comments
 (0)