You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/01/20 06:54:44 UTC

[logging-log4j2] 02/05: LOG4J2-3537 Fixes problem with wrong ANSI escape code for bright colors (#935)

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 603f260cd3d17d46526b75646f5a7b1acdd63e8f
Author: PashaTurok <oo...@mail.ru>
AuthorDate: Sun Jun 19 12:26:14 2022 +0300

    LOG4J2-3537 Fixes problem with wrong ANSI escape code for bright colors (#935)
    
    Fixes problem with wrong ANSI escape code for bright colors
---
 .../log4j/core/pattern/HighlightConverterTest.java |  32 ++---
 .../logging/log4j/core/pattern/AnsiEscape.java     | 131 ++++++++++++++++++++-
 2 files changed, 147 insertions(+), 16 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
index a704d44c6a..8a44c6096c 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
@@ -77,9 +77,7 @@ public class HighlightConverterTest {
         assertEquals(AnsiEscape.createSequence(colorName), converter.getLevelStyle(Level.DEBUG));
     }
 
-    @Test
-    public void testLevelNamesHexShort() {
-        final String colorName = "#1cd42b";
+   private void testLevelNames(final String colorName, final String expectedOutput) {
         final String[] options = {"%-5level: %msg", PatternParser.NO_CONSOLE_NO_ANSI + "=false, "
                 + PatternParser.DISABLE_ANSI + "=false, " + "INFO=" + colorName};
         final HighlightConverter converter = HighlightConverter.newInstance(null, options);
@@ -89,22 +87,26 @@ public class HighlightConverterTest {
                 new SimpleMessage("")).build();
         final StringBuilder buffer = new StringBuilder();
         converter.format(event, buffer);
-        assertEquals("\u001B[38;2;28;212;43mINFO : \u001B[m", buffer.toString());
+        assertEquals(expectedOutput, buffer.toString());
+   }
+
+    @Test
+    public void testLevelNamesBrightShort() {
+        testLevelNames("bright_red", "\u001B[91mINFO : \u001B[m");
+    }
+
+    public void testLevelNamesHexShort() {
+        testLevelNames("#1cd42b", "\u001B[38;2;28;212;43mINFO : \u001B[m");
     }
 
     @Test
-    public void testLevelNamesHexFull() {
-        final String colorName = "FG_#1cd42b BG_#000000";
-        final String[] options = {"%-5level: %msg", PatternParser.NO_CONSOLE_NO_ANSI + "=false, "
-                + PatternParser.DISABLE_ANSI + "=false, " + "INFO=" + colorName};
-        final HighlightConverter converter = HighlightConverter.newInstance(null, options);
-        assertNotNull(converter);
+    public void testLevelNamesBrightFull() {
+        testLevelNames("fg_bright_red bg_bright_blue bold", "\u001B[91;104;1mINFO : \u001B[m");
+    }
 
-        final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerName("a.b.c").setMessage(
-                new SimpleMessage("")).build();
-        final StringBuilder buffer = new StringBuilder();
-        converter.format(event, buffer);
-        assertEquals("\u001B[38;2;28;212;43;48;2;0;0;0mINFO : \u001B[m", buffer.toString());
+    @Test
+    public void testLevelNamesHexFull() {
+        testLevelNames("FG_#1cd42b BG_#000000", "\u001B[38;2;28;212;43;48;2;0;0;0mINFO : \u001B[m");
     }
 
     @Test
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java
index b7f703fa63..c7f8cca9e5 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java
@@ -61,9 +61,18 @@ public enum AnsiEscape {
 
     /**
      * Bright general attribute.
+     *
+     * @deprecated This attribute sets font-weight as "bold" and doesn't set color brightness. Use BOLD if you
+     * need to change font-weight and BRIGHT_* to use a bright color.
+     *
      */
     BRIGHT("1"),
 
+    /**
+     * Bold general attribute.
+     */
+    BOLD("1"),
+
     /**
      * Dim general attribute.
      */
@@ -217,7 +226,127 @@ public enum AnsiEscape {
     /**
      * White background color.
      */
-    BG_WHITE("47");
+    BG_WHITE("47"),
+
+    /**
+     * Bright black foreground color.
+     */
+    BRIGHT_BLACK("90"),
+
+    /**
+     * Bright black foreground color.
+     */
+    FG_BRIGHT_BLACK("90"),
+
+    /**
+     * Bright red foreground color.
+     */
+    BRIGHT_RED("91"),
+
+    /**
+     * Bright red foreground color.
+     */
+    FG_BRIGHT_RED("91"),
+
+    /**
+     * Bright green foreground color.
+     */
+    BRIGHT_GREEN("92"),
+
+    /**
+     * Bright green foreground color.
+     */
+    FG_BRIGHT_GREEN("92"),
+
+    /**
+     * Bright yellow foreground color.
+     */
+    BRIGHT_YELLOW("93"),
+
+    /**
+     * Bright yellow foreground color.
+     */
+    FG_BRIGHT_YELLOW("93"),
+
+    /**
+     * Bright blue foreground color.
+     */
+    BRIGHT_BLUE("94"),
+
+    /**
+     * Bright blue foreground color.
+     */
+    FG_BRIGHT_BLUE("94"),
+
+    /**
+     * Bright magenta foreground color.
+     */
+    BRIGHT_MAGENTA("95"),
+
+    /**
+     * Bright magenta foreground color.
+     */
+    FG_BRIGHT_MAGENTA("95"),
+
+    /**
+     * Bright cyan foreground color.
+     */
+    BRIGHT_CYAN("96"),
+
+    /**
+     * Bright cyan foreground color.
+     */
+    FG_BRIGHT_CYAN("96"),
+
+    /**
+     * Bright white foreground color.
+     */
+    BRIGHT_WHITE("97"),
+
+    /**
+     * Bright white foreground color.
+     */
+    FG_BRIGHT_WHITE("97"),
+
+    /**
+     * Bright black background color.
+     */
+    BG_BRIGHT_BLACK("100"),
+
+    /**
+     * Bright red background color.
+     */
+    BG_BRIGHT_RED("101"),
+
+    /**
+     * Bright green background color.
+     */
+    BG_BRIGHT_GREEN("102"),
+
+    /**
+     * Bright yellow background color.
+     */
+    BG_BRIGHT_YELLOW("103"),
+
+    /**
+     * Bright blue background color.
+     */
+    BG_BRIGHT_BLUE("104"),
+
+    /**
+     * Bright magenta background color.
+     */
+    BG_BRIGHT_MAGENTA("105"),
+
+    /**
+     * Bright cyan background color.
+     */
+    BG_BRIGHT_CYAN("106"),
+
+    /**
+     * Bright white background color.
+     */
+    BG_BRIGHT_WHITE("107");
 
     private static final String DEFAULT_STYLE = CSI.getCode() + SUFFIX.getCode();