logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1553750 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/ log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ src/cha...
Date Fri, 27 Dec 2013 22:13:03 GMT
Author: ggregory
Date: Fri Dec 27 22:13:02 2013
New Revision: 1553750

URL: http://svn.apache.org/r1553750
Log:
[LOG4J2-478] The message and ndc fields are not JavaScript escaped in JSONLayout.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Transform.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Transform.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Transform.java?rev=1553750&r1=1553749&r2=1553750&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Transform.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Transform.java
Fri Dec 27 22:13:02 2013
@@ -139,7 +139,7 @@ public final class Transform {
         final int len = input.length();
         for (int i = 0; i < len; i++) {
             final char ch = input.charAt(i);
-            final String escBs = "\\\\";
+            final String escBs = "\\";
             switch (ch) {
             case '"':
                 buf.append(escBs);

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java?rev=1553750&r1=1553749&r2=1553750&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
Fri Dec 27 22:13:02 2013
@@ -189,7 +189,7 @@ public class JSONLayout extends Abstract
             if (jsonSupported) {
                 buf.append(((MultiformatMessage) msg).getFormattedMessage(FORMATS));
             } else {
-                Transform.appendEscapingCDATA(buf, event.getMessage().getFormattedMessage());
+                buf.append(Transform.escapeJsonControlCharacters(event.getMessage().getFormattedMessage()));
             }
             buf.append('\"');
         }
@@ -198,7 +198,7 @@ public class JSONLayout extends Abstract
             buf.append(",");
             buf.append(this.eol);
             buf.append("\"ndc\":");
-            Transform.appendEscapingCDATA(buf, event.getContextStack().toString());
+            buf.append(Transform.escapeJsonControlCharacters(event.getContextStack().toString()));
             buf.append("\"");
         }
 

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java?rev=1553750&r1=1553749&r2=1553750&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java
Fri Dec 27 22:13:02 2013
@@ -126,6 +126,32 @@ public class JSONLayoutTest {
         this.checkAt("\"message\":\"starting mdc pattern test\",", 6, list);
     }
 
+    @Test
+    public void testEscapeLayout() throws Exception {
+
+        // set up appender
+        final JSONLayout layout = JSONLayout.createLayout("true", "true", "true", "false",
null);
+        final ListAppender appender = new ListAppender("List", null, layout, true, false);
+        appender.start();
+
+        // set appender on root and set level to debug
+        this.root.addAppender(appender);
+        this.root.setLevel(Level.DEBUG);
+
+        // output starting message
+        this.root.debug("Here is a quote ' and then a double quote \"");
+
+        appender.stop();
+
+        final List<String> list = appender.getMessages();
+
+        this.checkAt("[", 0, list);
+        this.checkAt("{", 1, list);
+        this.checkAt("\"logger\":\"root\",", 2, list);
+        this.checkAt("\"level\":\"DEBUG\",", 4, list);
+        this.checkAt("\"message\":\"Here is a quote ' and then a double quote \\\"\",", 6,
list);
+    }
+
     private void checkAt(String expected, int lineIndex, List<String> list) {
         final String trimedLine = list.get(lineIndex).trim();
         assertTrue("Incorrect line index " + lineIndex + ": \"" + trimedLine + "\"", trimedLine.equals(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=1553750&r1=1553749&r2=1553750&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Dec 27 22:13:02 2013
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-478" dev="ggregory" type="fix" due-to="Michael Friedmann.">
+        The message and ndc fields are not JavaScript escaped in JSONLayout.
+      </action>
       <action issue="LOG4J2-455" dev="rpopma" type="fix" due-to="Robin Zhang Tao">
         RingBufferLogEvent should return Message timestamp for TimestampMessage messages.
       </action>



Mime
View raw message