logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1382402 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/layout/ core/src/main/java/org/apache/logging/log4j/core/pattern/ core/src/test/java/org/apache/logging/log4j/core/pattern/ src/changes/
Date Sun, 09 Sep 2012 05:42:24 GMT
Author: rgoers
Date: Sun Sep  9 05:42:23 2012
New Revision: 1382402

URL: http://svn.apache.org/viewvc?rev=1382402&view=rev
Log:
Fix LOG4J2-81 - PatternLayout not honoring format modifiers

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternFormatter.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java?rev=1382402&r1=1382401&r2=1382402&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
Sun Sep  9 05:42:23 2012
@@ -26,6 +26,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.helpers.OptionConverter;
 import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
 import org.apache.logging.log4j.core.pattern.PatternConverter;
+import org.apache.logging.log4j.core.pattern.PatternFormatter;
 import org.apache.logging.log4j.core.pattern.PatternParser;
 import org.apache.logging.log4j.core.pattern.RegexReplacement;
 
@@ -73,7 +74,7 @@ public final class PatternLayout extends
     /**
      * Initial converter for pattern.
      */
-    private List<PatternConverter> converters;
+    private List<PatternFormatter> formatters;
 
     /**
      * Conversion pattern.
@@ -107,7 +108,7 @@ public final class PatternLayout extends
         this.conversionPattern = pattern;
         this.config = config;
         PatternParser parser = createPatternParser(config);
-        converters = parser.parse((pattern == null) ? DEFAULT_CONVERSION_PATTERN : pattern);
+        formatters = parser.parse((pattern == null) ? DEFAULT_CONVERSION_PATTERN : pattern);
         handlesExceptions = parser.handlesExceptions();
 
     }
@@ -125,7 +126,7 @@ public final class PatternLayout extends
             return;
         }
         PatternParser parser = createPatternParser(this.config);
-        converters = parser.parse(pattern);
+        formatters = parser.parse(pattern);
         handlesExceptions = parser.handlesExceptions();
     }
 
@@ -137,8 +138,8 @@ public final class PatternLayout extends
      */
     public String formatAs(final LogEvent event) {
         StringBuilder buf = new StringBuilder();
-        for (PatternConverter c : converters) {
-            c.format(event, buf);
+        for (PatternFormatter formatter : formatters) {
+            formatter.format(event, buf);
         }
         String str = buf.toString();
         if (replace != null) {

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternFormatter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternFormatter.java?rev=1382402&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternFormatter.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternFormatter.java
Sun Sep  9 05:42:23 2012
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.pattern;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+
+/**
+ *
+ */
+public class PatternFormatter {
+    private LogEventPatternConverter converter;
+    private FormattingInfo field;
+
+    public PatternFormatter(LogEventPatternConverter converter, FormattingInfo field) {
+        this.converter = converter;
+        this.field = field;
+    }
+
+    public void format(LogEvent event, StringBuilder buf) {
+        int startField = buf.length();
+        converter.format(event, buf);
+        field.format(startField, buf);
+    }
+
+    public LogEventPatternConverter getConverter() {
+        return converter;
+    }
+
+    public FormattingInfo getFormattingInfo() {
+        return field;
+    }
+}

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java?rev=1382402&r1=1382401&r2=1382402&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
Sun Sep  9 05:42:23 2012
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.core.pattern;
 
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.PluginManager;
 import org.apache.logging.log4j.core.config.plugins.PluginType;
@@ -125,35 +126,33 @@ public final class PatternParser {
     }
 
 
-    public List<PatternConverter> parse(String pattern) {
+    public List<PatternFormatter> parse(String pattern) {
+        List<PatternFormatter> list = new ArrayList<PatternFormatter>();
         List<PatternConverter> converters = new ArrayList<PatternConverter>();
         List<FormattingInfo> fields = new ArrayList<FormattingInfo>();
 
         parse(pattern, converters, fields);
 
-        LogEventPatternConverter[] patternConverters = new LogEventPatternConverter[converters.size()];
-        FormattingInfo[] patternFields = new FormattingInfo[converters.size()];
-
-        int i = 0;
         Iterator fieldIter = fields.iterator();
 
         for (PatternConverter converter : converters) {
+            LogEventPatternConverter pc;
             if (converter instanceof LogEventPatternConverter) {
-                patternConverters[i] = (LogEventPatternConverter) converter;
-                handlesExceptions |= patternConverters[i].handlesThrowable();
+                pc = (LogEventPatternConverter) converter;
+                handlesExceptions |= pc.handlesThrowable();
             } else {
-                patternConverters[i] = new LiteralPatternConverter("");
+                pc = new LiteralPatternConverter("");
             }
 
+            FormattingInfo field;
             if (fieldIter.hasNext()) {
-                patternFields[i] = (FormattingInfo) fieldIter.next();
+                field = (FormattingInfo) fieldIter.next();
             } else {
-                patternFields[i] = FormattingInfo.getDefault();
+                field = FormattingInfo.getDefault();
             }
-
-            i++;
+            list.add(new PatternFormatter(pc, field));
         }
-        return converters;
+        return list;
     }
 
     public boolean handlesExceptions() {

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java?rev=1382402&r1=1382401&r2=1382402&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
Sun Sep  9 05:42:23 2012
@@ -35,20 +35,20 @@ public final class RegexReplacementConve
 
     private final String substitution;
 
-    private List<PatternConverter> converters;
+    private List<PatternFormatter> formatters;
 
     /**
      * Construct the converter.
-     * @param converters The PatternConverters to generate the text to manipulate.
+     * @param formatters The PatternFormatters to generate the text to manipulate.
      * @param pattern The regular expression Pattern.
      * @param substitution The substitution string.
      */
-    private RegexReplacementConverter(List<PatternConverter> converters,
+    private RegexReplacementConverter(List<PatternFormatter> formatters,
                                       Pattern pattern, String substitution) {
         super("replace", "replace");
         this.pattern = pattern;
         this.substitution = substitution;
-        this.converters = converters;
+        this.formatters = formatters;
     }
 
     /**
@@ -78,8 +78,8 @@ public final class RegexReplacementConve
         }
         Pattern p = Pattern.compile(options[1]);
         PatternParser parser = PatternLayout.createPatternParser(config);
-        List<PatternConverter> converters = parser.parse(options[0]);
-        return new RegexReplacementConverter(converters, p, options[2]);
+        List<PatternFormatter> formatters = parser.parse(options[0]);
+        return new RegexReplacementConverter(formatters, p, options[2]);
     }
 
 
@@ -88,8 +88,8 @@ public final class RegexReplacementConve
      */
     public void format(final LogEvent event, final StringBuilder toAppendTo) {
         StringBuilder buf = new StringBuilder();
-        for (PatternConverter c : converters) {
-            c.format(event, buf);
+        for (PatternFormatter formatter : formatters) {
+            formatter.format(event, buf);
         }
         toAppendTo.append(pattern.matcher(buf.toString()).replaceAll(substitution));
     }

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java?rev=1382402&r1=1382401&r2=1382402&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
Sun Sep  9 05:42:23 2012
@@ -16,10 +16,15 @@
  */
 package org.apache.logging.log4j.core.pattern;
 
+import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.Logger;
 
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -28,7 +33,9 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  *
@@ -47,6 +54,8 @@ public class PatternParserTest {
     private String mdcMsgPattern4 = "%m : %X{key3}%n";
     private String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
 
+    private static String customPattern = "[%d{yyyyMMdd HH:mm:ss,SSS}] %-5p [%-25.25c{1}:%-4L]
- %m%n";
+
 
     private static final String KEY = "Converter";
     private PatternParser parser;
@@ -56,8 +65,8 @@ public class PatternParserTest {
         parser = new PatternParser(KEY);
     }
 
-    private void validateConverter(List<PatternConverter> converters, int index, String
name) {
-        PatternConverter pc = converters.get(index);
+    private void validateConverter(List<PatternFormatter> formatter, int index, String
name) {
+        PatternConverter pc = formatter.get(index).getConverter();
         assertEquals("Incorrect converter " + pc.getName() + " at index " + index + " expected
" + name,
             pc.getName(), name);
     }
@@ -67,11 +76,35 @@ public class PatternParserTest {
      */
     @Test
     public void defaultPattern() {
-        List<PatternConverter> converters = parser.parse(msgPattern);
-        assertNotNull(converters);
-        assertTrue(converters.size() == 2);
-        validateConverter(converters, 0, "Message");
-        validateConverter(converters, 1, "Line Sep");
+        List<PatternFormatter> formatters = parser.parse(msgPattern);
+        assertNotNull(formatters);
+        assertTrue(formatters.size() == 2);
+        validateConverter(formatters, 0, "Message");
+        validateConverter(formatters, 1, "Line Sep");
     }
 
+    /**
+     * Test the custome pattern
+     */
+    @Test
+    public void testCustomPattern() {
+        List<PatternFormatter> formatters = parser.parse(customPattern);
+        assertNotNull(formatters);
+        Map<String, String> mdc = new HashMap<String, String>();
+        mdc.put("loginId", "Fred");
+        Throwable t = new Throwable();
+        StackTraceElement[] elements = t.getStackTrace();
+        LogEvent event = new Log4jLogEvent("org.apache.logging.log4j.PatternParserTest",
MarkerManager.getMarker("TEST"),
+            Logger.class.getName(), Level.INFO, new SimpleMessage("Hello, world"), null,
+            mdc, null, "Thread1", elements[0], System.currentTimeMillis());
+        StringBuilder buf = new StringBuilder();
+        for (PatternFormatter formatter : formatters) {
+            formatter.format(event, buf);
+        }
+        String str = buf.toString();
+        String expected = "INFO  [PatternParserTest        :95  ] - Hello, world\n";
+        assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
+    }
+
+
 }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1382402&r1=1382401&r2=1382402&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Sep  9 05:42:23 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-alpha3" date="TBD" description="">
+      <action issue="LOG4J-81" dev="rgoers" type="fix">
+        PatternLayout was not honoring format modifiers.
+      </action>
       <action dev="rgoers" type="fix">
         Created web module to allow web applications to include the Log4j context listener
in WEB-INF/lib even if
         Log4j is in the container's class path. Allow locating the LoggerContext to include
the ClassLoader. Updated



Mime
View raw message