You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2014/03/01 20:32:14 UTC
svn commit: r1573211 - 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/
Author: rgoers
Date: Sat Mar 1 19:32:13 2014
New Revision: 1573211
URL: http://svn.apache.org/r1573211
Log:
LOG4J2-439 - Add EncodingPatternConverter to escape newlines and HTML special characters.
Added:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverterTest.java
Modified:
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/layouts.xml.vm
Added: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java?rev=1573211&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java Sat Mar 1 19:32:13 2014
@@ -0,0 +1,108 @@
+/*
+ * 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 java.util.List;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+
+/**
+ * Returns the event's rendered message in a StringBuilder.
+ */
+@Plugin(name = "encode", category = "Converter")
+@ConverterKeys({"enc", "encode"})
+public final class EncodingPatternConverter extends LogEventPatternConverter {
+
+ private final List<PatternFormatter> formatters;
+
+ /**
+ * Private constructor.
+ *
+ * @param formatters The PatternFormatters to generate the text to manipulate.
+ */
+ private EncodingPatternConverter(final List<PatternFormatter> formatters) {
+ super("encode", "encode");
+ this.formatters = formatters;
+ }
+
+ /**
+ * Obtains an instance of pattern converter.
+ *
+ * @param config The Configuration.
+ * @param options options, may be null.
+ * @return instance of pattern converter.
+ */
+ public static EncodingPatternConverter newInstance(final Configuration config, final String[] options) {
+ if (options.length != 1) {
+ LOGGER.error("Incorrect number of options on escape. Expected 1, received " + options.length);
+ return null;
+ }
+ if (options[0] == null) {
+ LOGGER.error("No pattern supplied on escape");
+ return null;
+ }
+ final PatternParser parser = PatternLayout.createPatternParser(config);
+ final List<PatternFormatter> formatters = parser.parse(options[0]);
+ return new EncodingPatternConverter(formatters);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void format(final LogEvent event, final StringBuilder toAppendTo) {
+ final StringBuilder buf = new StringBuilder();
+ for (final PatternFormatter formatter : formatters) {
+ formatter.format(event, buf);
+ }
+ for (int i = 0; i < buf.length(); i++) {
+ final char c = buf.charAt(i);
+ switch (c) {
+ case '\r':
+ toAppendTo.append("\\r");
+ break;
+ case '\n':
+ toAppendTo.append("\\n");
+ break;
+ case '&':
+ toAppendTo.append("&");
+ break;
+ case '<':
+ toAppendTo.append("<");
+ break;
+ case '>':
+ toAppendTo.append(">");
+ break;
+ case '"':
+ toAppendTo.append(""");
+ break;
+ case '\'':
+ toAppendTo.append("'");
+ break;
+ case '/':
+ toAppendTo.append("/");
+ break;
+ default:
+ toAppendTo.append(c);
+ break;
+ }
+ }
+ }
+}
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverterTest.java?rev=1573211&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverterTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverterTest.java Sat Mar 1 19:32:13 2014
@@ -0,0 +1,48 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class EncodingPatternConverterTest {
+
+ @Test
+ public void testReplacement() {
+ final LogEvent event = new Log4jLogEvent(EncodingPatternConverterTest.class.getName(), null, null, Level.DEBUG,
+ new SimpleMessage("Test \r\n<div class=\"test\">this</div> & <div class='test'>that</div>"), null);
+ final StringBuilder sb = new StringBuilder();
+ final LoggerContext ctx = (LoggerContext) LogManager.getContext();
+ final String[] options = new String[]{"%msg"};
+ final EncodingPatternConverter converter = EncodingPatternConverter
+ .newInstance(ctx.getConfiguration(), options);
+ converter.format(event, sb);
+ assertEquals(
+ "Test \\r\\n<div class="test">this</div> & <div class='test'>that</div>",
+ 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=1573211&r1=1573210&r2=1573211&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Mar 1 19:32:13 2014
@@ -21,6 +21,9 @@
</properties>
<body>
<release version="2.0-rc2?" date="2014-MM-DD" description="Bug fixes and enhancements">
+ <action issue="LOG4J2-439" dev="rgoers" type="add" due-to="Bruce Brouwer">
+ Add EncodingPatternConverter to escape newlines and HTML special characters.
+ </action>
<action issue="LOG4J2-496" dev="rgoers" type="update">
Allow header and footer to be specified as lookup patterns in PatternLayout.
</action>
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=1573211&r1=1573210&r2=1573211&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 Sat Mar 1 19:32:13 2014
@@ -47,8 +47,8 @@
</p>
<h4>Complete well-formed JSON vs. fragment JSON</h4>
<p>
- If you configure <code>complete="true"</code>, the appender outputs a well-formed JSON document. By default,
- with <code>complete="false"</code>, you should include the output as an <em>external file</em> in a
+ If you configure <code>complete="true"</code>, the appender outputs a well-formed JSON document. By default,
+ with <code>complete="false"</code>, you should include the output as an <em>external file</em> in a
separate file to form a well-formed JSON document.
</p>
<p>
@@ -73,7 +73,7 @@
]
</pre>
<p>
- If <code>complete="false"</code>, the appender does not write the JSON open array character "[" at the start
+ If <code>complete="false"</code>, the appender does not write the JSON open array character "[" at the start
of the document. and "]" and the end.
</p>
<p>
@@ -81,14 +81,14 @@
</p>
<h4>Encoding</h4>
<p>
- Appenders using this layout should have their <code>charset</code> set to <code>UTF-8</code> or
+ Appenders using this layout should have their <code>charset</code> set to <code>UTF-8</code> or
<code>UTF-16</code>, otherwise events containing non ASCII characters could result in corrupted log files.
</p>
<h4>Pretty vs. compact XML</h4>
<p>
- By default, the JSON layout is not compact (a.k.a. not "pretty") with <code>compact="false"</code>, which
- means the appender uses end-of-line characters and indents lines to format the text. If
- <code>compact="true"</code>, then no end-of-line or indentation is used. Message content may contain,
+ By default, the JSON layout is not compact (a.k.a. not "pretty") with <code>compact="false"</code>, which
+ means the appender uses end-of-line characters and indents lines to format the text. If
+ <code>compact="true"</code>, then no end-of-line or indentation is used. Message content may contain,
of course, escaped end-of-lines.
</p>
</subsection>
@@ -370,18 +370,29 @@ WARN [main]: Message 2</pre>
</table>
</p>
<p>
- %d{UNIX} outputs the UNIX time in seconds. %d{UNIX_MILLIS} outputs the UNIX time in milliseconds.
+ %d{UNIX} outputs the UNIX time in seconds. %d{UNIX_MILLIS} outputs the UNIX time in milliseconds.
The UNIX time is the difference, in seconds for UNIX and in milliseconds for UNIX_MILLIS, between
- the current time and midnight, January 1, 1970 UTC. While the time unit is milliseconds, the
- granularity depends on the operating system
- (<a href="http://msdn.microsoft.com/en-us/windows/hardware/gg463266.aspx">Windows</a>).
- This is an efficient way to output the event time because only a conversion from long to String
+ the current time and midnight, January 1, 1970 UTC. While the time unit is milliseconds, the
+ granularity depends on the operating system
+ (<a href="http://msdn.microsoft.com/en-us/windows/hardware/gg463266.aspx">Windows</a>).
+ This is an efficient way to output the event time because only a conversion from long to String
takes place, there is no Date formatting involved.
</p>
</td>
</tr>
<tr>
<td align="center">
+ <b>enc</b>{pattern}<br />
+ <b>encode</b>{pattern>
+ </td>
+ <td>
+ <p>Escape newlines and HTML special characters in the specified pattern.
+ </p>
+ <p>Allows HTML to be safely logged.</p>
+ </td>
+ </tr>
+ <tr>
+ <td align="center">
<b>ex</b>|<b>exception</b>|<b>throwable</b><br />
{["none"<br />
|"full"<br />
@@ -396,37 +407,37 @@ WARN [main]: Message 2</pre>
</td>
<td>
<p>
- Outputs the Throwable trace bound to the LoggingEvent, by default this will output the full trace
+ Outputs the Throwable trace bound to the LoggingEvent, by default this will output the full trace
as one would normally find with a call to Throwable.printStackTrace().
</p>
<p>
You can follow the throwable conversion word with an option in the form <b>%throwable{option}</b>.
</p>
- <p>
+ <p>
<b>%throwable{short}</b> outputs the first line of the Throwable.
</p>
- <p>
+ <p>
<b>%throwable{short.className}</b> outputs the name of the class where the exception occurred.
</p>
- <p>
+ <p>
<b>%throwable{short.methodName}</b> outputs the method name where the exception occurred.
</p>
- <p>
+ <p>
<b>%throwable{short.fileName}</b> outputs the name of the class where the exception occurred.
</p>
- <p>
+ <p>
<b>%throwable{short.lineNumber}</b> outputs the line number where the exception occurred.
</p>
- <p>
+ <p>
<b>%throwable{short.message}</b> outputs the message.
</p>
- <p>
+ <p>
<b>%throwable{short.localizedMessage}</b> outputs the localized message.
</p>
- <p>
- <b>%throwable{n}</b> outputs the first n lines of the stack trace.
- </p>
- <p>
+ <p>
+ <b>%throwable{n}</b> outputs the first n lines of the stack trace.
+ </p>
+ <p>
Specifying <b>%throwable{none}</b> or <b>%throwable{0}</b> suppresses output of the exception.
</p>
</td>
@@ -1313,10 +1324,10 @@ at org.apache.logging.log4j.core.pattern
</p>
<h4>Complete well-formed XML vs. fragment XML</h4>
<p>
- If you configure <code>complete="true"</code>, the appender outputs a well-formed XML document where the
- default namespace is the Log4j namespace <code>"http://logging.apache.org/log4j/2.0/events"</code>. By default,
- with <code>complete="false"</code>, you should include the output as an <em>external entity</em> in a
- separate file to form a well-formed XML document, in which case the appender uses
+ If you configure <code>complete="true"</code>, the appender outputs a well-formed XML document where the
+ default namespace is the Log4j namespace <code>"http://logging.apache.org/log4j/2.0/events"</code>. By default,
+ with <code>complete="false"</code>, you should include the output as an <em>external entity</em> in a
+ separate file to form a well-formed XML document, in which case the appender uses
<code>namespacePrefix</code> with a default of <code>"log4j"</code>.
</p>
<p>
@@ -1334,7 +1345,7 @@ at org.apache.logging.log4j.core.pattern
</Event>
</Events></pre>
<p>
- If <code>complete="false"</code>, the appender does not write the XML processing instruction and the root
+ If <code>complete="false"</code>, the appender does not write the XML processing instruction and the root
element.
</p>
<p>
@@ -1348,14 +1359,14 @@ at org.apache.logging.log4j.core.pattern
</p>
<h4>Encoding</h4>
<p>
- Appenders using this layout should have their <code>charset</code> set to <code>UTF-8</code> or
+ Appenders using this layout should have their <code>charset</code> set to <code>UTF-8</code> or
<code>UTF-16</code>, otherwise events containing non ASCII characters could result in corrupted log files.
</p>
<h4>Pretty vs. compact XML</h4>
<p>
- By default, the XML layout is not compact (a.k.a. not "pretty") with <code>compact="false"</code>, which
- means the appender uses end-of-line characters and indents lines to format the XML. If
- <code>compact="true"</code>, then no end-of-line or indentation is used. Message content may contain,
+ By default, the XML layout is not compact (a.k.a. not "pretty") with <code>compact="false"</code>, which
+ means the appender uses end-of-line characters and indents lines to format the XML. If
+ <code>compact="true"</code>, then no end-of-line or indentation is used. Message content may contain,
of course, end-of-lines.
</p>
</subsection>