logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1519760 - 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 17:14:05 GMT
Author: ggregory
Date: Tue Sep  3 17:14:05 2013
New Revision: 1519760

URL: http://svn.apache.org/r1519760
Log:
[LOG4J2-383][Pattern Layout] Customize level name by length.

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=1519760&r1=1519759&r2=1519760&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 17:14:05 2013
@@ -26,7 +26,7 @@ import org.apache.logging.log4j.core.con
  * Returns the event's level in a StringBuilder.
  */
 @Plugin(name = "LevelPatternConverter", category = "Converter")
-@ConverterKeys({"p", "level" })
+@ConverterKeys({ "p", "level" })
 public final class LevelPatternConverter extends LogEventPatternConverter {
     /**
      * Singleton.
@@ -45,9 +45,10 @@ public final class LevelPatternConverter
 
     /**
      * Obtains an instance of pattern converter.
-     *
-     * @param options options, may be null. May contain a list of level names and
-     * The value that should be displayed for the Level.
+     * 
+     * @param options
+     *            options, may be null. May contain a list of level names and The value that
should be displayed for the
+     *            Level.
      * @return instance of pattern converter.
      */
     public static LevelPatternConverter newInstance(final String[] options) {
@@ -55,6 +56,7 @@ public final class LevelPatternConverter
             return INSTANCE;
         }
         final EnumMap<Level, String> levelMap = new EnumMap<Level, String>(Level.class);
+        int length = Integer.MAX_VALUE; // More than the longest level name.
         final String[] definitions = options[0].split(",");
         for (final String def : definitions) {
             final String[] pair = def.split("=");
@@ -62,25 +64,49 @@ public final class LevelPatternConverter
                 LOGGER.error("Invalid option {}", def);
                 continue;
             }
-            final Level level = Level.toLevel(pair[0].trim(), null);
-            if (level == null) {
-                LOGGER.error("Invalid Level {}", pair[0].trim());
+            final String key = pair[0].trim();
+            final String value = pair[1].trim();
+            if ("length".equalsIgnoreCase(key)) {
+                length = Integer.parseInt(value);
             } else {
-                levelMap.put(level, pair[1].trim());
+                final Level level = Level.toLevel(key, null);
+                if (level == null) {
+                    LOGGER.error("Invalid Level {}", key);
+                } else {
+                    levelMap.put(level, value);
+                }
             }
         }
-        if (levelMap.size() == 0) {
+        if (levelMap.size() == 0 && length == Integer.MAX_VALUE) {
             return INSTANCE;
         }
         for (final Level level : Level.values()) {
             if (!levelMap.containsKey(level)) {
-                levelMap.put(level, level.toString());
+                levelMap.put(level, left(level, length));
             }
         }
         return new LevelPatternConverter(levelMap);
     }
 
     /**
+     * Returns the leftmost chars of the level name for the given level.
+     * 
+     * @param level
+     *            The level
+     * @param length
+     *            How many chars to return
+     * @return The abbreviated level name, or the whole level name if the {@code length}
is greater than the level name
+     *         length,
+     */
+    private static String left(final Level level, int length) {
+        final String string = level.toString();
+        if (length >= string.length()) {
+            return string;
+        }
+        return string.substring(0, length);
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -97,26 +123,26 @@ public final class LevelPatternConverter
             final Level level = ((LogEvent) e).getLevel();
 
             switch (level) {
-                case TRACE:
-                    return "level trace";
+            case TRACE:
+                return "level trace";
 
-                case DEBUG:
-                    return "level debug";
+            case DEBUG:
+                return "level debug";
 
-                case INFO:
-                    return "level info";
+            case INFO:
+                return "level info";
 
-                case WARN:
-                    return "level warn";
+            case WARN:
+                return "level warn";
 
-                case ERROR:
-                    return "level error";
+            case ERROR:
+                return "level error";
 
-                case FATAL:
-                    return "level fatal";
+            case FATAL:
+                return "level fatal";
 
-                default:
-                    return "level " + ((LogEvent) e).getLevel().toString();
+            default:
+                return "level " + ((LogEvent) e).getLevel().toString();
             }
         }
 

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=1519760&r1=1519759&r2=1519760&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 17:14:05 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j.core.pattern;
 
+import static org.junit.Assert.assertEquals;
+
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
@@ -23,23 +25,58 @@ import org.apache.logging.log4j.message.
 import org.apache.logging.log4j.message.SimpleMessage;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-
 /**
  *
  */
 public class LevelPatternConverterTest {
 
+    private void testLevelLength(int length, String debug, String warn) {
+        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[] { "length=" + length };
+        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 testLevelLength1() {
+        testLevelLength(1, "D", "W");
+    }
+
+    @Test
+    public void testLevelLength10() {
+        testLevelLength(10, "DEBUG", "WARN");
+    }
+
+    @Test
+    public void testLevelLength2() {
+        testLevelLength(2, "DE", "WA");
+    }
+
+    @Test
+    public void testLevelLength5() {
+        testLevelLength(5, "DEBUG", "WARN");
+    }
 
     @Test
-    public void testLevel() {
+    public void testLevelMap() {
         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, DEBUG=Debug, ERROR=Error, TRACE=Trace,
INFO=Info"};
+        final String[] opts = new String[] { "WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace,
INFO=Info" };
         converter = LevelPatternConverter.newInstance(opts);
         sb.setLength(0);
         converter.format(event, sb);
@@ -49,4 +86,24 @@ public class LevelPatternConverterTest {
         converter.format(event, sb);
         assertEquals("Warning", sb.toString());
     }
+
+    @Test
+    public void testLevelMapWithLength() {
+        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" };
+        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=1519760&r1=1519759&r2=1519760&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Sep  3 17:14:05 2013
@@ -65,6 +65,9 @@
       <action issue="LOG4J2-374" dev="ggregory" type="add" due-to="Tibor Benke">
         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.
+      </action>
       <action dev="nickwilliams" type="update">
         Cleaned up tests and cleared up documentation for the JPA appender following the
resolution of EclipseLink
         issue #412454.

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=1519760&r1=1519759&r2=1519760&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 17:14:05 2013
@@ -678,12 +678,12 @@ WARN  [main]: Message 2</pre>
             </tr>
             <tr>
               <td align="center">
-                <b>p{level name mapping}</b><br />
-                <b>level{level name mapping}</b>
+                <b>p</b>|<b>level</b>{level=label, level=label, ...}
+                <b>p</b>|<b>level</b>{length=<em>n</em>}
               </td>
               <td>
                 <p>
-                  Outputs the level of the logging event. A level name mapping map be provided
in the form
+                  Outputs the level of the logging event. You provide a level name map in
the form
                   "level=value, level=value" where level is the name of the Level and value
is the value that
                   should be displayed instead of the name of the Level.
                 </p>
@@ -695,6 +695,16 @@ WARN  [main]: Message 2</pre>
                   Alternatively, for the compact-minded:
                   <pre>%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}</pre>
                 </p>
+                <p>
+                  More succinctly, for the same result as above, you can define the length
of the level label:
+                  <pre>%level{length=1}</pre>
+                  If the length is greater than a level name length, the layout uses the
normal level name.
+                </p>
+                <p>
+                  You can combine the two kinds of options:
+                  <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>
               </td>
             </tr>
             <tr>



Mime
View raw message