logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1405405 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/pattern/ core/src/test/java/org/apache/logging/log4j/core/pattern/ src/changes/ src/site/xdoc/manual/
Date Sat, 03 Nov 2012 19:15:39 GMT
Author: rgoers
Date: Sat Nov  3 19:15:39 2012
New Revision: 1405405

URL: http://svn.apache.org/viewvc?rev=1405405&view=rev
Log:
LOG4J2-105 - Add ability to customize the names of the Levels in the LevelPatternConverter.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
      - copied, changed from r1405298, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java?rev=1405405&r1=1405404&r2=1405405&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/LevelPatternConverter.java
Sat Nov  3 19:15:39 2012
@@ -20,6 +20,8 @@ import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 
+import java.util.EnumMap;
+
 
 /**
  * Returns the event's level in a StringBuffer.
@@ -30,25 +32,52 @@ public final class LevelPatternConverter
     /**
      * Singleton.
      */
-    private static final LevelPatternConverter INSTANCE =
-        new LevelPatternConverter();
+    private static final LevelPatternConverter INSTANCE = new LevelPatternConverter(null);
+
+    private final EnumMap<Level, String> levelMap;
 
     /**
      * Private constructor.
      */
-    private LevelPatternConverter() {
+    private LevelPatternConverter(EnumMap<Level, String> map) {
         super("Level", "level");
+        this.levelMap = map;
     }
 
     /**
      * Obtains an instance of pattern converter.
      *
-     * @param options options, may be null.
+     * @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) {
-        return INSTANCE;
+    public static LevelPatternConverter newInstance(final String[] options) {
+        if (options == null || options.length == 0) {
+            return INSTANCE;
+        }
+        EnumMap<Level, String> levelMap = new EnumMap<Level, String>(Level.class);
+        String[] definitions = options[0].split(",");
+        for (String def : definitions) {
+            String[] pair = def.split("=");
+            if (pair == null || pair.length != 2) {
+                LOGGER.error("Invalid option {}", def);
+                continue;
+            }
+            Level level = Level.toLevel(pair[0].trim().toUpperCase(), null);
+            if (level == null) {
+                LOGGER.error("Invalid Level {}", pair[0].trim());
+            }
+            levelMap.put(level, pair[1].trim());
+        }
+        if (levelMap.size() == 0) {
+            return INSTANCE;
+        }
+        for (Level level : Level.values()) {
+            if (!levelMap.containsKey(level)) {
+                levelMap.put(level, level.toString());
+            }
+        }
+        return new LevelPatternConverter(levelMap);
     }
 
     /**
@@ -56,7 +85,7 @@ public final class LevelPatternConverter
      */
     @Override
     public void format(final LogEvent event, final StringBuilder output) {
-        output.append(event.getLevel().toString());
+        output.append(levelMap == null ? event.getLevel().toString() : levelMap.get(event.getLevel()));
     }
 
     /**

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
(from r1405298, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java&r1=1405298&r2=1405405&rev=1405405&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/LevelPatternConverterTest.java
Sat Nov  3 19:15:39 2012
@@ -22,27 +22,36 @@ import org.apache.logging.log4j.MarkerMa
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.message.StructuredDataMessage;
 import org.junit.Test;
 
+import javax.xml.transform.TransformerConfigurationException;
+
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 
 /**
  *
  */
-public class MarkerPatternConverterTest {
+public class LevelPatternConverterTest {
 
 
     @Test
-    public void testLookup() {
-        Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
-        Marker eventMarker = MarkerManager.getMarker("EVENT");
-        Marker auditMarker = MarkerManager.getMarker("AUDIT", eventMarker);
-        LogEvent event = new Log4jLogEvent("MyLogger", auditMarker, null, Level.DEBUG, msg,
null);
+    public void testLevel() {
+        Message msg = new SimpleMessage("Hello");
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
         StringBuilder sb = new StringBuilder();
-        MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null);
+        LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
+        converter.format(event, sb);
+        assertEquals(Level.DEBUG.toString(), sb.toString());
+        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);
+        assertEquals("Debug", sb.toString());
+        event = new Log4jLogEvent("MyLogger", null, null, Level.WARN, msg, null);
+        sb.setLength(0);
         converter.format(event, sb);
-        assertEquals(auditMarker.toString(), sb.toString());
+        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=1405405&r1=1405404&r2=1405405&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Nov  3 19:15:39 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta3" date="TBD" description= "Bug fixes and enhancements">
+      <action issue="LOG4J2-105" dev="rgoers" type="update">
+        Add ability to customize the names of the Levels in the LevelPatternConverter.
+      </action>
       <action issue="LOG4J2-107" dev="rgoers" type="fix">
         PatternParser was not properly handling adjacent nested options
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml?rev=1405405&r1=1405404&r2=1405405&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml Sat Nov  3 19:15:39 2012
@@ -257,8 +257,8 @@ WARN  [main]: Message 2</pre>
                     <tr>
                       <td>%d{HH:mm:ss}{GMT+0}</td>
                       <td>18:34:02,781</td>
-                    </tr>                    
-                  </table>                 
+                    </tr>
+                  </table>
                 </p>
               </td>
             </tr>
@@ -511,10 +511,12 @@ WARN  [main]: Message 2</pre>
             </tr>
             <tr>
               <td align="center">
-                <b>p</b><br />
-                <b>level</b>
+                <b>p{level name mapping}</b><br />
+                <b>level{level name mapping}</b>
               </td>
-              <td>Outputs the level of the logging event.</td>
+              <td>Outputs the level of the logging event. A level name mapping map
be provided 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.</td>
             </tr>
             <tr>
               <td align="center">



Mime
View raw message