logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1519784 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/ log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ src/changes/ src/site/xdoc/manual/
Date Tue, 03 Sep 2013 18:26:16 GMT
Author: ggregory
Date: Tue Sep  3 18:26:15 2013
New Revision: 1519784

URL: http://svn.apache.org/r1519784
Log:
[LOG4J2-384][Pattern Layout] Customize level names to lower-case.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java?rev=1519784&r1=1519783&r2=1519784&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
Tue Sep  3 18:26:15 2013
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.core.pattern;
 
 import java.util.EnumMap;
+import java.util.Locale;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.LogEvent;
@@ -29,6 +30,7 @@ import org.apache.logging.log4j.core.con
 @ConverterKeys({ "p", "level" })
 public final class LevelPatternConverter extends LogEventPatternConverter {
     private static final String OPTION_LENGTH = "length";
+    private static final String OPTION_LOWER = "lowerCase";
 
     /**
      * Singleton.
@@ -59,6 +61,7 @@ public final class LevelPatternConverter
         }
         final EnumMap<Level, String> levelMap = new EnumMap<Level, String>(Level.class);
         int length = Integer.MAX_VALUE; // More than the longest level name.
+        boolean lowerCase = false;
         final String[] definitions = options[0].split(",");
         for (final String def : definitions) {
             final String[] pair = def.split("=");
@@ -70,6 +73,8 @@ public final class LevelPatternConverter
             final String value = pair[1].trim();
             if (OPTION_LENGTH.equalsIgnoreCase(key)) {
                 length = Integer.parseInt(value);
+            } else if (OPTION_LOWER.equalsIgnoreCase(key)) {
+                lowerCase = Boolean.parseBoolean(value);
             } else {
                 final Level level = Level.toLevel(key, null);
                 if (level == null) {
@@ -79,12 +84,13 @@ public final class LevelPatternConverter
                 }
             }
         }
-        if (levelMap.size() == 0 && length == Integer.MAX_VALUE) {
+        if (levelMap.size() == 0 && length == Integer.MAX_VALUE && !lowerCase)
{
             return INSTANCE;
         }
         for (final Level level : Level.values()) {
             if (!levelMap.containsKey(level)) {
-                levelMap.put(level, left(level, length));
+                final String left = left(level, length);
+                levelMap.put(level, lowerCase ? left.toLowerCase(Locale.US) : left);
             }
         }
         return new LevelPatternConverter(levelMap);

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java?rev=1519784&r1=1519783&r2=1519784&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
Tue Sep  3 18:26:15 2013
@@ -69,6 +69,25 @@ public class LevelPatternConverterTest {
     }
 
     @Test
+    public void testLevelLowerCase() {
+        final Message msg = new SimpleMessage("Hello");
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        final StringBuilder sb = new StringBuilder();
+        LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
+        converter.format(event, sb);
+        assertEquals(Level.DEBUG.toString(), sb.toString());
+        final String[] opts = new String[] { "lowerCase=true" };
+        converter = LevelPatternConverter.newInstance(opts);
+        sb.setLength(0);
+        converter.format(event, sb);
+        assertEquals("debug", sb.toString());
+        event = new Log4jLogEvent("MyLogger", null, null, Level.WARN, msg, null);
+        sb.setLength(0);
+        converter.format(event, sb);
+        assertEquals("warn", sb.toString());
+    }
+
+    @Test
     public void testLevelMap() {
         final Message msg = new SimpleMessage("Hello");
         LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
@@ -106,4 +125,23 @@ public class LevelPatternConverterTest {
         assertEquals("Warning", sb.toString());
     }
 
+    @Test
+    public void testLevelMapWithLengthAndLowerCase() {
+        final Message msg = new SimpleMessage("Hello");
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        final StringBuilder sb = new StringBuilder();
+        LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
+        converter.format(event, sb);
+        assertEquals(Level.DEBUG.toString(), sb.toString());
+        final String[] opts = new String[] { "WARN=Warning, length=2, lowerCase=true" };
+        converter = LevelPatternConverter.newInstance(opts);
+        sb.setLength(0);
+        converter.format(event, sb);
+        assertEquals("de", sb.toString());
+        event = new Log4jLogEvent("MyLogger", null, null, Level.WARN, msg, null);
+        sb.setLength(0);
+        converter.format(event, sb);
+        assertEquals("Warning", sb.toString());
+    }
+
 }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1519784&r1=1519783&r2=1519784&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Sep  3 18:26:15 2013
@@ -69,7 +69,10 @@
         Add more options to PatternLayout to display more detailed information about a Throwable.
       </action>
       <action issue="LOG4J2-383" dev="ggregory" type="add">
-        [Pattern Layout] Customize level name by length.
+        [Pattern Layout] Customize level names by length.
+      </action>
+      <action issue="LOG4J2-384" dev="ggregory" type="add">
+        [Pattern Layout] Customize level names to lower-case.
       </action>
       <action dev="nickwilliams" type="update">
         Cleaned up tests and cleared up documentation for the JPA appender following the
resolution of EclipseLink

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm?rev=1519784&r1=1519783&r2=1519784&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm Tue Sep  3 18:26:15 2013
@@ -678,8 +678,9 @@ WARN  [main]: Message 2</pre>
             </tr>
             <tr>
               <td align="center">
-                <b>p</b>|<b>level</b>{level=label, level=label, ...}
+                <b>p</b>|<b>level</b>{<em>level</em>=<em>label</em>,
<em>level</em>=<em>label</em>, ...}
                 <b>p</b>|<b>level</b>{length=<em>n</em>}
+                <b>p</b>|<b>level</b>{lowerCase=<em>true</em>|<em>false</em>}
               </td>
               <td>
                 <p>
@@ -705,6 +706,10 @@ WARN  [main]: Message 2</pre>
                   <pre>%level{ERROR=Error, length=2}</pre>
                   This give you the <code>Error</code> level name and all other
level names of length 2.
                 </p>
+                <p>
+                  Finally, you can output lower-case level names (the default is upper-case):
+                  <pre>%level{lowerCase=true}</pre>
+                </p>
               </td>
             </tr>
             <tr>



Mime
View raw message