Skip to content

Commit

Permalink
Polish "Add support for additional colors in Log4j2 and Logback"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Apr 5, 2023
1 parent 35994bb commit 6cf08a3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,23 +53,23 @@ public final class ColorConverter extends LogEventPatternConverter {

static {
Map<String, AnsiElement> ansiElements = new HashMap<>();
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("white", AnsiColor.WHITE);
ansiElements.put("faint", AnsiStyle.FAINT);
ansiElements.put("red", AnsiColor.RED);
ansiElements.put("green", AnsiColor.GREEN);
ansiElements.put("yellow", AnsiColor.YELLOW);
ansiElements.put("blue", AnsiColor.BLUE);
ansiElements.put("magenta", AnsiColor.MAGENTA);
ansiElements.put("cyan", AnsiColor.CYAN);
ansiElements.put("white", AnsiColor.WHITE);
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ELEMENTS = Collections.unmodifiableMap(ansiElements);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,23 +43,23 @@ public class ColorConverter extends CompositeConverter<ILoggingEvent> {

static {
Map<String, AnsiElement> ansiElements = new HashMap<>();
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("white", AnsiColor.WHITE);
ansiElements.put("faint", AnsiStyle.FAINT);
ansiElements.put("red", AnsiColor.RED);
ansiElements.put("green", AnsiColor.GREEN);
ansiElements.put("yellow", AnsiColor.YELLOW);
ansiElements.put("blue", AnsiColor.BLUE);
ansiElements.put("magenta", AnsiColor.MAGENTA);
ansiElements.put("cyan", AnsiColor.CYAN);
ansiElements.put("white", AnsiColor.WHITE);
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ELEMENTS = Collections.unmodifiableMap(ansiElements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ private ColorConverter newConverter(String styling) {
return ColorConverter.newInstance(null, new String[] { this.in, styling });
}

@Test
void black() {
StringBuilder output = new StringBuilder();
newConverter("black").format(this.event, output);
assertThat(output).hasToString("\033[30min\033[0;39m");
}

@Test
void white() {
StringBuilder output = new StringBuilder();
newConverter("white").format(this.event, output);
assertThat(output).hasToString("\033[37min\033[0;39m");
}

@Test
void faint() {
StringBuilder output = new StringBuilder();
Expand Down Expand Up @@ -107,24 +121,17 @@ void cyan() {
}

@Test
void white() {
StringBuilder output = new StringBuilder();
newConverter("white").format(this.event, output);
assertThat(output).hasToString("\033[37min\033[0;39m");
}

@Test
void black() {
void brightBlack() {
StringBuilder output = new StringBuilder();
newConverter("black").format(this.event, output);
assertThat(output).hasToString("\033[30min\033[0;39m");
newConverter("bright_black").format(this.event, output);
assertThat(output).hasToString("\033[90min\033[0;39m");
}

@Test
void brightBlack() {
void brightWhite() {
StringBuilder output = new StringBuilder();
newConverter("bright_black").format(this.event, output);
assertThat(output).hasToString("\033[90min\033[0;39m");
newConverter("bright_white").format(this.event, output);
assertThat(output).hasToString("\033[97min\033[0;39m");
}

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

@Test
void brightWhite() {
StringBuilder output = new StringBuilder();
newConverter("bright_white").format(this.event, output);
assertThat(output).hasToString("\033[97min\033[0;39m");
}

@Test
void highlightFatal() {
this.event.setLevel(Level.FATAL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,20 @@ static void resetAnsi() {
AnsiOutput.setEnabled(AnsiOutput.Enabled.DETECT);
}

@Test
void black() {
this.converter.setOptionList(Collections.singletonList("black"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[30min\033[0;39m");
}

@Test
void white() {
this.converter.setOptionList(Collections.singletonList("white"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[37min\033[0;39m");
}

@Test
void faint() {
this.converter.setOptionList(Collections.singletonList("faint"));
Expand Down Expand Up @@ -101,24 +115,17 @@ void cyan() {
}

@Test
void white() {
this.converter.setOptionList(Collections.singletonList("white"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[37min\033[0;39m");
}

@Test
void black() {
this.converter.setOptionList(Collections.singletonList("black"));
void brightBlack() {
this.converter.setOptionList(Collections.singletonList("bright_black"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[30min\033[0;39m");
assertThat(out).isEqualTo("\033[90min\033[0;39m");
}

@Test
void brightBlack() {
this.converter.setOptionList(Collections.singletonList("bright_black"));
void brightWhite() {
this.converter.setOptionList(Collections.singletonList("bright_white"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[90min\033[0;39m");
assertThat(out).isEqualTo("\033[97min\033[0;39m");
}

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

@Test
void brightWhite() {
this.converter.setOptionList(Collections.singletonList("bright_white"));
String out = this.converter.transform(this.event, this.in);
assertThat(out).isEqualTo("\033[97min\033[0;39m");
}

@Test
void highlightError() {
this.event.setLevel(Level.ERROR);
Expand Down

0 comments on commit 6cf08a3

Please sign in to comment.