logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject logging-log4j2 git commit: [LOG4J2-2405] Better handling of %highlight pattern when using jul-bridge.
Date Tue, 06 Nov 2018 02:14:27 GMT
Repository: logging-log4j2
Updated Branches:
  refs/heads/release-2.x 4f8174a7a -> d60824c4e


[LOG4J2-2405] Better handling of %highlight pattern when using
jul-bridge.

Closes #225.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d60824c4
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d60824c4
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d60824c4

Branch: refs/heads/release-2.x
Commit: d60824c4e05293e6f8abbfee7d5827b3509c6552
Parents: 4f8174a
Author: Marco Herrn <hupfdule>
Authored: Mon Nov 5 19:14:24 2018 -0700
Committer: Gary Gregory <garydgregory@gmail.com>
Committed: Mon Nov 5 19:14:24 2018 -0700

----------------------------------------------------------------------
 .../log4j/core/pattern/HighlightConverter.java  | 58 +++++++++++---------
 .../core/pattern/HighlightConverterTest.java    | 30 +++++++++-
 src/changes/changes.xml                         |  3 +
 3 files changed, 63 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d60824c4/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java
index 330da9f..ce08319 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java
@@ -80,9 +80,9 @@ import org.apache.logging.log4j.util.Strings;
 @PerformanceSensitive("allocation")
 public final class HighlightConverter extends LogEventPatternConverter implements AnsiConverter
{
 
-    private static final Map<Level, String> DEFAULT_STYLES = new HashMap<>();
+    private static final Map<String, String> DEFAULT_STYLES = new HashMap<>();
 
-    private static final Map<Level, String> LOGBACK_STYLES = new HashMap<>();
+    private static final Map<String, String> LOGBACK_STYLES = new HashMap<>();
 
     private static final String STYLE_KEY = "STYLE";
 
@@ -90,23 +90,23 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
 
     private static final String STYLE_KEY_LOGBACK = "LOGBACK";
 
-    private static final Map<String, Map<Level, String>> STYLES = new HashMap<>();
+    private static final Map<String, Map<String, String>> STYLES = new HashMap<>();
 
     static {
         // Default styles:
-        DEFAULT_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BRIGHT", "RED"));
-        DEFAULT_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED"));
-        DEFAULT_STYLES.put(Level.WARN, AnsiEscape.createSequence("YELLOW"));
-        DEFAULT_STYLES.put(Level.INFO, AnsiEscape.createSequence("GREEN"));
-        DEFAULT_STYLES.put(Level.DEBUG, AnsiEscape.createSequence("CYAN"));
-        DEFAULT_STYLES.put(Level.TRACE, AnsiEscape.createSequence("BLACK"));
+        DEFAULT_STYLES.put(Level.FATAL.name(), AnsiEscape.createSequence("BRIGHT", "RED"));
+        DEFAULT_STYLES.put(Level.ERROR.name(), AnsiEscape.createSequence("BRIGHT", "RED"));
+        DEFAULT_STYLES.put(Level.WARN.name(), AnsiEscape.createSequence("YELLOW"));
+        DEFAULT_STYLES.put(Level.INFO.name(), AnsiEscape.createSequence("GREEN"));
+        DEFAULT_STYLES.put(Level.DEBUG.name(), AnsiEscape.createSequence("CYAN"));
+        DEFAULT_STYLES.put(Level.TRACE.name(), AnsiEscape.createSequence("BLACK"));
         // Logback styles:
-        LOGBACK_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BLINK", "BRIGHT", "RED"));
-        LOGBACK_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED"));
-        LOGBACK_STYLES.put(Level.WARN, AnsiEscape.createSequence("RED"));
-        LOGBACK_STYLES.put(Level.INFO, AnsiEscape.createSequence("BLUE"));
-        LOGBACK_STYLES.put(Level.DEBUG, AnsiEscape.createSequence((String[]) null));
-        LOGBACK_STYLES.put(Level.TRACE, AnsiEscape.createSequence((String[]) null));
+        LOGBACK_STYLES.put(Level.FATAL.name(), AnsiEscape.createSequence("BLINK", "BRIGHT",
"RED"));
+        LOGBACK_STYLES.put(Level.ERROR.name(), AnsiEscape.createSequence("BRIGHT", "RED"));
+        LOGBACK_STYLES.put(Level.WARN.name(), AnsiEscape.createSequence("RED"));
+        LOGBACK_STYLES.put(Level.INFO.name(), AnsiEscape.createSequence("BLUE"));
+        LOGBACK_STYLES.put(Level.DEBUG.name(), AnsiEscape.createSequence((String[]) null));
+        LOGBACK_STYLES.put(Level.TRACE.name(), AnsiEscape.createSequence((String[]) null));
         // Style map:
         STYLES.put(STYLE_KEY_DEFAULT, DEFAULT_STYLES);
         STYLES.put(STYLE_KEY_LOGBACK, LOGBACK_STYLES);
@@ -140,7 +140,7 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
      *        The second slot can optionally contain the style map.
      * @return a new map
      */
-    private static Map<Level, String> createLevelStyleMap(final String[] options) {
+    private static Map<String, String> createLevelStyleMap(final String[] options)
{
         if (options.length < 2) {
             return DEFAULT_STYLES;
         }
@@ -150,12 +150,12 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
                 .replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY);
         //
         final Map<String, String> styles = AnsiEscape.createMap(string, new String[]
{STYLE_KEY});
-        final Map<Level, String> levelStyles = new HashMap<>(DEFAULT_STYLES);
+        final Map<String, String> levelStyles = new HashMap<>(DEFAULT_STYLES);
         for (final Map.Entry<String, String> entry : styles.entrySet()) {
             final String key = entry.getKey().toUpperCase(Locale.ENGLISH);
             final String value = entry.getValue();
             if (STYLE_KEY.equalsIgnoreCase(key)) {
-                final Map<Level, String> enumMap = STYLES.get(value.toUpperCase(Locale.ENGLISH));
+                final Map<String, String> enumMap = STYLES.get(value.toUpperCase(Locale.ENGLISH));
                 if (enumMap == null) {
                     LOGGER.error("Unknown level style: " + value + ". Use one of " +
                         Arrays.toString(STYLES.keySet().toArray()));
@@ -165,9 +165,10 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
             } else {
                 final Level level = Level.toLevel(key, null);
                 if (level == null) {
-                    LOGGER.error("Unknown level name: {}; use one of {}", key, Arrays.toString(Level.values()));
+                    LOGGER.warn("Setting style for yet unknown level name {}", key);
+                    levelStyles.put(key, value);
                 } else {
-                    levelStyles.put(level, value);
+                    levelStyles.put(level.name(), value);
                 }
             }
         }
@@ -199,7 +200,7 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
         return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi);
     }
 
-    private final Map<Level, String> levelStyles;
+    private final Map<String, String> levelStyles;
 
     private final List<PatternFormatter> patternFormatters;
 
@@ -215,7 +216,7 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
      * @param noAnsi
      *            If true, do not output ANSI escape codes.
      */
-    private HighlightConverter(final List<PatternFormatter> patternFormatters, final
Map<Level, String> levelStyles, final boolean noAnsi) {
+    private HighlightConverter(final List<PatternFormatter> patternFormatters, final
Map<String, String> levelStyles, final boolean noAnsi) {
         super("style", "style");
         this.patternFormatters = patternFormatters;
         this.levelStyles = levelStyles;
@@ -230,14 +231,17 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
     public void format(final LogEvent event, final StringBuilder toAppendTo) {
         int start = 0;
         int end = 0;
+        String levelStyle = levelStyles.get(event.getLevel().name());
         if (!noAnsi) { // use ANSI: set prefix
             start = toAppendTo.length();
-            toAppendTo.append(levelStyles.get(event.getLevel()));
+            if (levelStyle != null) {
+                toAppendTo.append(levelStyle);
+            }
             end = toAppendTo.length();
         }
 
-        //noinspection ForLoopReplaceableByForEach
-        for (int i = 0, size = patternFormatters.size(); i <  size; i++) {
+        // noinspection ForLoopReplaceableByForEach
+        for (int i = 0, size = patternFormatters.size(); i < size; i++) {
             patternFormatters.get(i).format(event, toAppendTo);
         }
 
@@ -246,14 +250,14 @@ public final class HighlightConverter extends LogEventPatternConverter
implement
         if (!noAnsi) {
             if (empty) {
                 toAppendTo.setLength(start); // erase prefix
-            } else {
+            } else if (levelStyle != null) {
                 toAppendTo.append(defaultStyle); // add postfix
             }
         }
     }
 
     String getLevelStyle(final Level level) {
-        return levelStyles.get(level);
+        return levelStyles.get(level.name());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d60824c4/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
index 442c5ab..589367b 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/HighlightConverterTest.java
@@ -77,6 +77,28 @@ public class HighlightConverterTest {
     }
 
     @Test
+    public void testLevelNamesUnknown() {
+        String colorName = "blue";
+        final String[] options = { "%level", PatternParser.NO_CONSOLE_NO_ANSI + "=false,
" + PatternParser.DISABLE_ANSI
+                + "=false, " + "DEBUG=" + colorName + ", CUSTOM1=" + colorName };
+        final HighlightConverter converter = HighlightConverter.newInstance(null, options);
+        Assert.assertNotNull(converter);
+        Assert.assertNotNull(converter.getLevelStyle(Level.INFO));
+        Assert.assertNotNull(converter.getLevelStyle(Level.DEBUG));
+        Assert.assertNotNull(converter.getLevelStyle(Level.forName("CUSTOM1", 412)));
+        Assert.assertNull(converter.getLevelStyle(Level.forName("CUSTOM2", 512)));
+
+        assertArrayEquals(new byte[] { 27, '[', '3', '4', 'm', 'D', 'E', 'B', 'U', 'G', 27,
'[', 'm' },
+                toFormattedCharSeq(converter, Level.DEBUG).toString().getBytes());
+        assertArrayEquals(new byte[] { 27, '[', '3', '2', 'm', 'I', 'N', 'F', 'O', 27, '[',
'm' },
+                toFormattedCharSeq(converter, Level.INFO).toString().getBytes());
+        assertArrayEquals(new byte[] { 27, '[', '3', '4', 'm', 'C', 'U', 'S', 'T', 'O', 'M',
'1', 27, '[', 'm' },
+                toFormattedCharSeq(converter, Level.forName("CUSTOM1", 412)).toString().getBytes());
+        assertArrayEquals(new byte[] { 'C', 'U', 'S', 'T', 'O', 'M', '2' },
+                toFormattedCharSeq(converter, Level.forName("CUSTOM2", 512)).toString().getBytes());
+    }    
+
+    @Test
     public void testLevelNamesNone() {
         final String[] options = { "%-5level: %msg",
                 PatternParser.NO_CONSOLE_NO_ANSI + "=false, " + PatternParser.DISABLE_ANSI
+ "=false" };
@@ -109,4 +131,10 @@ public class HighlightConverterTest {
         converter.format(event, buffer);
         assertEquals("INFO : message in a bottle", buffer.toString());
     }
-}
\ No newline at end of file
+
+    private CharSequence toFormattedCharSeq(final HighlightConverter converter, final Level
level) {
+        final StringBuilder sb = new StringBuilder();
+        converter.format(Log4jLogEvent.newBuilder().setLevel(level).build(), sb);
+        return sb;
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d60824c4/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 554a4b6..763668f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -118,6 +118,9 @@
       <action issue="LOG4J2-2496" dev="ggregory" type="add">
         JDBC Appender should reconnect to the database when a connection goes stale.
       </action>
+      <action issue="LOG4J2-2405" dev="ggregory" type="update" due-to="Marco Herrn">
+        Better handling of %highlight pattern when using jul-bridge.
+      </action>
     </release>
     <release version="2.11.1" date="2018-07-22" description="GA Release 2.11.1">
       <action issue="LOG4J2-2389" dev="rgoers" type="fix" due-to="Liu Wen">


Mime
View raw message