[
https://issues.apache.org/jira/browse/LOG4J2-2102?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16240695#comment-16240695
]
ASF GitHub Bot commented on LOG4J2-2102:
----------------------------------------
Github user cakofony commented on a diff in the pull request:
https://github.com/apache/logging-log4j2/pull/125#discussion_r149168416
--- Diff:
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EncodingPatternConverter.java
---
@@ -86,100 +87,4 @@ public void format(final LogEvent event, final
StringBuilder toAppendTo) {
}
escapeFormat.escape(toAppendTo, start);
}
-
- private enum EscapeFormat {
- HTML {
- @Override
- void escape(final StringBuilder toAppendTo, final int start) {
- for (int i = toAppendTo.length() - 1; i >= start; i--) {
// backwards: length may change
- final char c = toAppendTo.charAt(i);
- switch (c) {
- case '\r':
- toAppendTo.setCharAt(i, '\\');
- toAppendTo.insert(i + 1, 'r');
- break;
- case '\n':
- toAppendTo.setCharAt(i, '\\');
- toAppendTo.insert(i + 1, 'n');
- break;
- case '&':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "amp;");
- break;
- case '<':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "lt;");
- break;
- case '>':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "gt;");
- break;
- case '"':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "quot;");
- break;
- case '\'':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "apos;");
- break;
- case '/':
- toAppendTo.setCharAt(i, '&');
- toAppendTo.insert(i + 1, "#x2F;");
- break;
- }
- }
- }
- },
-
- /**
- * JSON string escaping as defined in RFC 4627.
- *
- * @see <a href="https://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>
- */
- JSON {
- @Override
- void escape(final StringBuilder toAppendTo, final int start) {
- for (int i = toAppendTo.length() - 1; i >= start; i--) {
// backwards: length may change
- final char c = toAppendTo.charAt(i);
- if (Character.isISOControl(c)) {
- // all iso control characters are in U+00xx
- toAppendTo.setCharAt(i, '\\');
- toAppendTo.insert(i + 1, "u0000");
- toAppendTo.setCharAt(i + 4,
Chars.getUpperCaseHex((c & 0xF0) >> 4));
- toAppendTo.setCharAt(i + 5,
Chars.getUpperCaseHex(c & 0xF));
- } else if (c == '"' || c == '\\') {
- // only " and \ need to be escaped; other escapes
are optional
- toAppendTo.insert(i, '\\');
- }
- }
- }
- },
-
- CRLF {
- @Override
- void escape(final StringBuilder toAppendTo, final int start) {
- for (int i = toAppendTo.length() - 1; i >= start; i--) {
// backwards: length may change
- final char c = toAppendTo.charAt(i);
- switch (c) {
- case '\r':
- toAppendTo.setCharAt(i, '\\');
- toAppendTo.insert(i + 1, 'r');
- break;
- case '\n':
- toAppendTo.setCharAt(i, '\\');
- toAppendTo.insert(i + 1, 'n');
- break;
- }
- }
- }
- };
-
- /**
- * Escapes text using a standardized format from a given starting
point to the end of the string.
- *
- * @param toAppendTo string buffer to escape
- * @param start where to start escaping from
- */
- abstract void escape(final StringBuilder toAppendTo, final int
start);
- }
--- End diff --
Alternatively I can pull the json implementation into a shared utility
class elsewhere.
> MapMessage JSON encoding should escape keys and values
> ------------------------------------------------------
>
> Key: LOG4J2-2102
> URL: https://issues.apache.org/jira/browse/LOG4J2-2102
> Project: Log4j 2
> Issue Type: Bug
> Components: Layouts
> Affects Versions: 2.9.1
> Reporter: Carter Douglas Kozak
>
> Writing a map message as json does not currently escape quotes, resulting in
> invalid json
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)