You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2017/07/22 20:48:32 UTC

logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1986 [created] c4814a873


LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c4814a87
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87

Branch: refs/heads/LOG4J2-1986
Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
Parents: eea873e
Author: Mikael Ståldal <mi...@staldal.nu>
Authored: Sat Jul 22 22:48:24 2017 +0200
Committer: Mikael Ståldal <mi...@staldal.nu>
Committed: Sat Jul 22 22:48:24 2017 +0200

----------------------------------------------------------------------
 .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
 .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
 .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
 .../log4j/core/parser/ParseException.java       |  34 ++++++
 .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
 .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
 .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
 .../logging/log4j/core/parser/package-info.java |  20 ++++
 .../core/parser/JsonLogEventParserTest.java     | 103 +++++++++++++++++++
 src/changes/changes.xml                         |   3 +
 10 files changed, 456 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
new file mode 100644
index 0000000..8b00e57
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
@@ -0,0 +1,85 @@
+/*
+ * 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.parser;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectReader;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+class AbstractJacksonLogEventParser implements TextLogEventParser {
+    final ObjectReader objectReader;
+
+    AbstractJacksonLogEventParser(ObjectReader objectReader) {
+        this.objectReader = objectReader;
+    }
+
+    @Override
+    public LogEvent parseFrom(InputStream input) throws IOException, ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(Reader input) throws IOException, ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(String input) throws ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(byte[] input) throws ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(byte[] input, int offset, int length) throws ParseException {
+        try {
+            return objectReader.readValue(input, offset, length);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
new file mode 100644
index 0000000..218de0b
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
@@ -0,0 +1,32 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+
+/**
+ * Parse the output from JsonLayout layout into instances of {@link LogEvent}.
+ */
+public class JsonLogEventParser extends AbstractJacksonLogEventParser {
+
+    public JsonLogEventParser() {
+        super(new Log4jJsonObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
new file mode 100644
index 0000000..88e84fb
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
@@ -0,0 +1,64 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Parse the output from a layout into instances of {@link LogEvent}.
+ */
+public interface LogEventParser {
+
+    /**
+     * Parse from an InputStream. The input stream may contain multiple log events.
+     * This method will read one log event and leave the stream open and positioned to read the
+     * next log event.
+     *
+     * @param input  the input stream
+     *
+     * @return the parsed LogEvent, or {@literal null} of end of input is reached.
+     * @throws IOException if unable to read from the input
+     * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent
+     */
+    LogEvent parseFrom(InputStream input) throws IOException, ParseException;
+
+    /**
+     * Parse from a byte array, which is expected to contain exactly one log event.
+     *
+     * @param input  the byte array
+     *
+     * @return the parsed LogEvent, never {@literal null}.
+     * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent
+     */
+    LogEvent parseFrom(byte[] input) throws ParseException;
+
+    /**
+     * Parse from a specified range of a byte array. The specified range is expected to contain
+     * exactly one log event.
+     *
+     * @param input  the byte array
+     * @param offset  the initial offset
+     * @param length  the length
+     *
+     * @return the parsed LogEvent, never {@literal null}.
+     * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent
+     */
+    LogEvent parseFrom(byte[] input, int offset, int length) throws ParseException;
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
new file mode 100644
index 0000000..8f5fa54
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
@@ -0,0 +1,34 @@
+/*
+ * 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.parser;
+
+/**
+ * Thrown when the input cannot be parsed.
+ */
+public class ParseException extends Exception {
+    public ParseException(String message) {
+        super(message);
+    }
+
+    public ParseException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public ParseException(Throwable cause) {
+        super(cause);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
new file mode 100644
index 0000000..3428024
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
@@ -0,0 +1,50 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Parse the output from a text based layout into instances of {@link LogEvent}.
+ */
+public interface TextLogEventParser extends LogEventParser {
+    /**
+     * Parse from a Reader. The reader may contain multiple log events.
+     * This method will read one log event and leave the reader open and positioned to read the
+     * next log event.
+     *
+     * @param input  the reader
+     *
+     * @return the parsed LogEvent, or {@literal null} of end of input is reached.
+     * @throws IOException if unable to read from the input
+     * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent
+     */
+    LogEvent parseFrom(Reader input) throws IOException, ParseException;
+
+    /**
+     * Parse from a String, which is expected to contain exactly one log event.
+     *
+     * @param input  the string
+     *
+     * @return the parsed LogEvent
+     * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent
+     */
+    LogEvent parseFrom(String input) throws ParseException;
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
new file mode 100644
index 0000000..597e70b
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
@@ -0,0 +1,32 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
+
+/**
+ * Parse the output from XmlLayout layout into instances of {@link LogEvent}.
+ */
+public class XmlLogEventParser extends AbstractJacksonLogEventParser {
+
+    public XmlLogEventParser() {
+        super(new Log4jXmlObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
new file mode 100644
index 0000000..59b3463
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
@@ -0,0 +1,33 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
+import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
+
+/**
+ * Parse the output from YamlLayout layout into instances of {@link LogEvent}.
+ */
+public class YamlLogEventParser extends AbstractJacksonLogEventParser {
+
+    public YamlLogEventParser() {
+        super(new Log4jYamlObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
new file mode 100644
index 0000000..3506570
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * Parsers for the output of various layouts.
+ */
+package org.apache.logging.log4j.core.parser;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
new file mode 100644
index 0000000..5684602
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class JsonLogEventParserTest {
+
+    private JsonLogEventParser parser;
+
+    private static final String JSON = "{\n" +
+            "  \"timeMillis\" : 1493121664118,\n" +
+            "  \"thread\" : \"main\",\n" +
+            "  \"level\" : \"INFO\",\n" +
+            "  \"loggerName\" : \"HelloWorld\",\n" +
+            "  \"marker\" : {\n" +
+            "    \"name\" : \"child\",\n" +
+            "    \"parents\" : [ {\n" +
+            "      \"name\" : \"parent\",\n" +
+            "      \"parents\" : [ {\n" +
+            "        \"name\" : \"grandparent\"\n" +
+            "      } ]\n" +
+            "    } ]\n" +
+            "  },\n" +
+            "  \"message\" : \"Hello, world!\",\n" +
+            "  \"thrown\" : {\n" +
+            "    \"commonElementCount\" : 0,\n" +
+            "    \"name\" : \"java.lang.RuntimeException\",\n" +
+            "    \"extendedStackTrace\" : [ {\n" +
+            "      \"class\" : \"logtest.Main\",\n" +
+            "      \"method\" : \"main\",\n" +
+            "      \"file\" : \"Main.java\",\n" +
+            "      \"line\" : 29,\n" +
+            "      \"exact\" : true,\n" +
+            "      \"location\" : \"classes/\",\n" +
+            "      \"version\" : \"?\"\n" +
+            "    } ]\n" +
+            "  },\n" +
+            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
+            "  \"endOfBatch\" : false,\n" +
+            "  \"loggerFqcn\" : \"org.apache.logging.log4j.spi.AbstractLogger\",\n" +
+            "  \"contextMap\" : {\n" +
+            "    \"bar\" : \"BAR\",\n" +
+            "    \"foo\" : \"FOO\"\n" +
+            "  },\n" +
+            "  \"threadId\" : 1,\n" +
+            "  \"threadPriority\" : 5,\n" +
+            "  \"source\" : {\n" +
+            "    \"class\" : \"logtest.Main\",\n" +
+            "    \"method\" : \"main\",\n" +
+            "    \"file\" : \"Main.java\",\n" +
+            "    \"line\" : 29\n" +
+            "  }\n" +
+            "}";
+
+    @Before
+    public void setup() {
+        parser = new JsonLogEventParser();
+    }
+
+    @Test
+    public void testString() throws ParseException {
+        LogEvent logEvent = parser.parseFrom(JSON);
+        assertNotNull(logEvent);
+        assertEquals("HelloWorld", logEvent.getLoggerName());
+        // TODO assert more here
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringEmpty() throws ParseException {
+        parser.parseFrom("");
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringInvalidJson() throws ParseException {
+        parser.parseFrom("foobar");
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringInvalidProperty() throws ParseException {
+        parser.parseFrom("{\"foo\":\"bar\"}");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4814a87/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 074e31c..522f49b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
          - "remove" - Removed
     -->
     <release version="2.9.0" date="2017-MM-DD" description="GA Release 2.9.0">
+      <action issue="LOG4J2-1986" dev="mikes" type="add">
+        Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent.
+      </action>
       <action issue="LOG4J2-1984" dev="rgoers" type="update">
         Allow maxLength of StructuredData to be specified by the user.
       </action>


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
Hi All,

I see two paths:

(1) All these APIs (if they are indeed needed, are they?) could throw both
IOException and ParseException (assuming you want to keep ParseException a
direct subclass of ParseException).

My reasoning here is that the underlying code like a Jackson Object Mapper
may have a genuine IOException even when parsing a String or byte[]. How?
Because the underlying gadget that does the parsing, might need to do some
IO to configure itself or who knows what.

(2) Another route is for all APIs to only throw ParseException and have the
internals catch everything and rethrow a ParseException. That might be
simpler and more homogeneous.

Gary


On Mon, Jul 24, 2017 at 1:34 PM, Mikael Ståldal <mi...@apache.org> wrote:

> I could use "Throwables.rethrow(e)", but is that really better than "throw
> new RuntimeException(e)"?
>
>
>
> On 2017-07-24 22:06, Matt Sicker wrote:
>
>> Java 8 added an UncheckedIOException class which is handy for rethrowing
>> IOException. Since we're on 7, that class isn't available. However, if you
>> just want to throw it, you can use the rethrow() method in Throwables
>> which
>> won't wrap the exception (abuses type erasure to allow checked exceptions
>> to be thrown).
>>
>> On 24 July 2017 at 14:56, Mikael Ståldal <mi...@apache.org> wrote:
>>
>> But that won't work, I need to at least translate JsonProcessingException
>>> (Jackson specific, which we don't want to expose) to our own
>>> ParseException.
>>>
>>>
>>>
>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>
>>>       @Override
>>>>       public LogEvent parseFrom(InputStream input) throws IOException,
>>>> ParseException {
>>>>           return objectReader.readValue(input);
>>>>       }
>>>>
>>>>
>>>
>>
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
True, but the caller will get an unexpected checked exception in the face.


On 2017-07-25 00:19, Matt Sicker wrote:
> rethrow(e) doesn't wrap the exception, so there's no useless exception.
> 
> On 24 July 2017 at 15:34, Mikael Ståldal <mi...@apache.org> wrote:
> 
>> I could use "Throwables.rethrow(e)", but is that really better than "throw
>> new RuntimeException(e)"?
>>
>>
>>
>> On 2017-07-24 22:06, Matt Sicker wrote:
>>
>>> Java 8 added an UncheckedIOException class which is handy for rethrowing
>>> IOException. Since we're on 7, that class isn't available. However, if you
>>> just want to throw it, you can use the rethrow() method in Throwables
>>> which
>>> won't wrap the exception (abuses type erasure to allow checked exceptions
>>> to be thrown).
>>>
>>> On 24 July 2017 at 14:56, Mikael Ståldal <mi...@apache.org> wrote:
>>>
>>> But that won't work, I need to at least translate JsonProcessingException
>>>> (Jackson specific, which we don't want to expose) to our own
>>>> ParseException.
>>>>
>>>>
>>>>
>>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>>
>>>>        @Override
>>>>>        public LogEvent parseFrom(InputStream input) throws IOException,
>>>>> ParseException {
>>>>>            return objectReader.readValue(input);
>>>>>        }
>>>>>
>>>>>
>>>>
>>>
>>>
>>
> 
> 

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Matt Sicker <bo...@gmail.com>.
rethrow(e) doesn't wrap the exception, so there's no useless exception.

On 24 July 2017 at 15:34, Mikael Ståldal <mi...@apache.org> wrote:

> I could use "Throwables.rethrow(e)", but is that really better than "throw
> new RuntimeException(e)"?
>
>
>
> On 2017-07-24 22:06, Matt Sicker wrote:
>
>> Java 8 added an UncheckedIOException class which is handy for rethrowing
>> IOException. Since we're on 7, that class isn't available. However, if you
>> just want to throw it, you can use the rethrow() method in Throwables
>> which
>> won't wrap the exception (abuses type erasure to allow checked exceptions
>> to be thrown).
>>
>> On 24 July 2017 at 14:56, Mikael Ståldal <mi...@apache.org> wrote:
>>
>> But that won't work, I need to at least translate JsonProcessingException
>>> (Jackson specific, which we don't want to expose) to our own
>>> ParseException.
>>>
>>>
>>>
>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>
>>>       @Override
>>>>       public LogEvent parseFrom(InputStream input) throws IOException,
>>>> ParseException {
>>>>           return objectReader.readValue(input);
>>>>       }
>>>>
>>>>
>>>
>>
>>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
I could use "Throwables.rethrow(e)", but is that really better than 
"throw new RuntimeException(e)"?


On 2017-07-24 22:06, Matt Sicker wrote:
> Java 8 added an UncheckedIOException class which is handy for rethrowing
> IOException. Since we're on 7, that class isn't available. However, if you
> just want to throw it, you can use the rethrow() method in Throwables which
> won't wrap the exception (abuses type erasure to allow checked exceptions
> to be thrown).
> 
> On 24 July 2017 at 14:56, Mikael Ståldal <mi...@apache.org> wrote:
> 
>> But that won't work, I need to at least translate JsonProcessingException
>> (Jackson specific, which we don't want to expose) to our own ParseException.
>>
>>
>>
>> On 2017-07-23 22:01, Gary Gregory wrote:
>>
>>>       @Override
>>>       public LogEvent parseFrom(InputStream input) throws IOException,
>>> ParseException {
>>>           return objectReader.readValue(input);
>>>       }
>>>
>>
> 
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Matt Sicker <bo...@gmail.com>.
Java 8 added an UncheckedIOException class which is handy for rethrowing
IOException. Since we're on 7, that class isn't available. However, if you
just want to throw it, you can use the rethrow() method in Throwables which
won't wrap the exception (abuses type erasure to allow checked exceptions
to be thrown).

On 24 July 2017 at 14:56, Mikael Ståldal <mi...@apache.org> wrote:

> But that won't work, I need to at least translate JsonProcessingException
> (Jackson specific, which we don't want to expose) to our own ParseException.
>
>
>
> On 2017-07-23 22:01, Gary Gregory wrote:
>
>>      @Override
>>      public LogEvent parseFrom(InputStream input) throws IOException,
>> ParseException {
>>          return objectReader.readValue(input);
>>      }
>>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
I created a branch called LOG4J2-1986-b with what I think is a cleaner
implementation.

Gary

On Tue, Jul 25, 2017 at 7:40 AM, Matt Sicker <bo...@gmail.com> wrote:

> The caller will get an unexpected unchecked exception in the alternative
> scenario as well.
>
> Anyways, it might not be a terrible idea to use checked exceptions deriving
> from IOException in this API.
>
> On 25 July 2017 at 02:49, Mikael Ståldal <mi...@staldal.nu> wrote:
>
> > Right, but this one will not:
> >
> >     public LogEvent parseFrom(InputStream input) throws IOException,
> >                 ParseException {
> >         return objectReader.readValue(input);
> >     }
> >
> >
> > In any case I need to translate from Jackson's JsonProcessingException to
> > our ParseException.
> >
> >
> >
> > On 2017-07-24 22:44, Gary Gregory wrote:
> >
> >> On Mon, Jul 24, 2017 at 1:31 PM, Mikael Ståldal <mi...@apache.org>
> wrote:
> >>
> >> The code you posted will never throw ParseException.
> >>>
> >>>
> >> It sure does! See:
> >>
> >>      @Override
> >>      public LogEvent parseFrom(byte[] input) throws ParseException {
> >>          try {
> >>              return objectReader.readValue(input);
> >>          } catch (IOException e) {
> >>              throw new ParseException(e);
> >>          }
> >>      }
> >>
> >>      @Override
> >>      public LogEvent parseFrom(byte[] input, int offset, int length)
> >> throws
> >> ParseException {
> >>          try {
> >>              return objectReader.readValue(input, offset, length);
> >>          } catch (IOException e) {
> >>              throw new ParseException(e);
> >>          }
> >>      }
> >>
> >>
> >>
> >>> Yes, ParseException could extend IOException, but I would still have to
> >>> do
> >>> translation.
> >>>
> >>
> >>
> >> Translate from what to what? From one of kind of IOException to another
> >> kind of IOException?
> >>
> >> Gary
> >>
> >>
> >>
> >>>
> >>> On 2017-07-24 22:20, Gary Gregory wrote:
> >>>
> >>> Whaaaat? Note that the code I posted here compiles just fine. A
> >>>> JsonProcessingException
> >>>>    is already a subclass of IOException. Wrapping
> >>>> JsonProcessingException
> >>>> in
> >>>> a ParseException is just adding a level of indirection for which I see
> >>>> no
> >>>> point, especially when the methods already throw IOException.
> >>>>
> >>>> As a side note, why can't ParseException extend IOException? You parse
> >>>> some
> >>>> kind of _input_, parsing does not happen in a void.
> >>>>
> >>>> Gary
> >>>>
> >>>> On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org>
> >>>> wrote:
> >>>>
> >>>> But that won't work, I need to at least translate
> >>>> JsonProcessingException
> >>>>
> >>>>> (Jackson specific, which we don't want to expose) to our own
> >>>>> ParseException.
> >>>>>
> >>>>>
> >>>>>
> >>>>> On 2017-07-23 22:01, Gary Gregory wrote:
> >>>>>
> >>>>>        @Override
> >>>>>
> >>>>>>        public LogEvent parseFrom(InputStream input) throws
> >>>>>> IOException,
> >>>>>> ParseException {
> >>>>>>            return objectReader.readValue(input);
> >>>>>>        }
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>
> >>>>
> >>>
> >>
> >
>
>
> --
> Matt Sicker <bo...@gmail.com>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Matt Sicker <bo...@gmail.com>.
The caller will get an unexpected unchecked exception in the alternative
scenario as well.

Anyways, it might not be a terrible idea to use checked exceptions deriving
from IOException in this API.

On 25 July 2017 at 02:49, Mikael Ståldal <mi...@staldal.nu> wrote:

> Right, but this one will not:
>
>     public LogEvent parseFrom(InputStream input) throws IOException,
>                 ParseException {
>         return objectReader.readValue(input);
>     }
>
>
> In any case I need to translate from Jackson's JsonProcessingException to
> our ParseException.
>
>
>
> On 2017-07-24 22:44, Gary Gregory wrote:
>
>> On Mon, Jul 24, 2017 at 1:31 PM, Mikael Ståldal <mi...@apache.org> wrote:
>>
>> The code you posted will never throw ParseException.
>>>
>>>
>> It sure does! See:
>>
>>      @Override
>>      public LogEvent parseFrom(byte[] input) throws ParseException {
>>          try {
>>              return objectReader.readValue(input);
>>          } catch (IOException e) {
>>              throw new ParseException(e);
>>          }
>>      }
>>
>>      @Override
>>      public LogEvent parseFrom(byte[] input, int offset, int length)
>> throws
>> ParseException {
>>          try {
>>              return objectReader.readValue(input, offset, length);
>>          } catch (IOException e) {
>>              throw new ParseException(e);
>>          }
>>      }
>>
>>
>>
>>> Yes, ParseException could extend IOException, but I would still have to
>>> do
>>> translation.
>>>
>>
>>
>> Translate from what to what? From one of kind of IOException to another
>> kind of IOException?
>>
>> Gary
>>
>>
>>
>>>
>>> On 2017-07-24 22:20, Gary Gregory wrote:
>>>
>>> Whaaaat? Note that the code I posted here compiles just fine. A
>>>> JsonProcessingException
>>>>    is already a subclass of IOException. Wrapping
>>>> JsonProcessingException
>>>> in
>>>> a ParseException is just adding a level of indirection for which I see
>>>> no
>>>> point, especially when the methods already throw IOException.
>>>>
>>>> As a side note, why can't ParseException extend IOException? You parse
>>>> some
>>>> kind of _input_, parsing does not happen in a void.
>>>>
>>>> Gary
>>>>
>>>> On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org>
>>>> wrote:
>>>>
>>>> But that won't work, I need to at least translate
>>>> JsonProcessingException
>>>>
>>>>> (Jackson specific, which we don't want to expose) to our own
>>>>> ParseException.
>>>>>
>>>>>
>>>>>
>>>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>>>
>>>>>        @Override
>>>>>
>>>>>>        public LogEvent parseFrom(InputStream input) throws
>>>>>> IOException,
>>>>>> ParseException {
>>>>>>            return objectReader.readValue(input);
>>>>>>        }
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@staldal.nu>.
Right, but this one will not:

     public LogEvent parseFrom(InputStream input) throws IOException,
		ParseException {
         return objectReader.readValue(input);
     }


In any case I need to translate from Jackson's JsonProcessingException 
to our ParseException.


On 2017-07-24 22:44, Gary Gregory wrote:
> On Mon, Jul 24, 2017 at 1:31 PM, Mikael Ståldal <mi...@apache.org> wrote:
> 
>> The code you posted will never throw ParseException.
>>
> 
> It sure does! See:
> 
>      @Override
>      public LogEvent parseFrom(byte[] input) throws ParseException {
>          try {
>              return objectReader.readValue(input);
>          } catch (IOException e) {
>              throw new ParseException(e);
>          }
>      }
> 
>      @Override
>      public LogEvent parseFrom(byte[] input, int offset, int length) throws
> ParseException {
>          try {
>              return objectReader.readValue(input, offset, length);
>          } catch (IOException e) {
>              throw new ParseException(e);
>          }
>      }
> 
> 
>>
>> Yes, ParseException could extend IOException, but I would still have to do
>> translation.
> 
> 
> Translate from what to what? From one of kind of IOException to another
> kind of IOException?
> 
> Gary
> 
> 
>>
>>
>> On 2017-07-24 22:20, Gary Gregory wrote:
>>
>>> Whaaaat? Note that the code I posted here compiles just fine. A
>>> JsonProcessingException
>>>    is already a subclass of IOException. Wrapping JsonProcessingException
>>> in
>>> a ParseException is just adding a level of indirection for which I see no
>>> point, especially when the methods already throw IOException.
>>>
>>> As a side note, why can't ParseException extend IOException? You parse
>>> some
>>> kind of _input_, parsing does not happen in a void.
>>>
>>> Gary
>>>
>>> On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org>
>>> wrote:
>>>
>>> But that won't work, I need to at least translate JsonProcessingException
>>>> (Jackson specific, which we don't want to expose) to our own
>>>> ParseException.
>>>>
>>>>
>>>>
>>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>>
>>>>        @Override
>>>>>        public LogEvent parseFrom(InputStream input) throws IOException,
>>>>> ParseException {
>>>>>            return objectReader.readValue(input);
>>>>>        }
>>>>>
>>>>>
>>>>
>>>
>>
> 

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
On Mon, Jul 24, 2017 at 1:31 PM, Mikael Ståldal <mi...@apache.org> wrote:

> The code you posted will never throw ParseException.
>

It sure does! See:

    @Override
    public LogEvent parseFrom(byte[] input) throws ParseException {
        try {
            return objectReader.readValue(input);
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }

    @Override
    public LogEvent parseFrom(byte[] input, int offset, int length) throws
ParseException {
        try {
            return objectReader.readValue(input, offset, length);
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }


>
> Yes, ParseException could extend IOException, but I would still have to do
> translation.


Translate from what to what? From one of kind of IOException to another
kind of IOException?

Gary


>
>
> On 2017-07-24 22:20, Gary Gregory wrote:
>
>> Whaaaat? Note that the code I posted here compiles just fine. A
>> JsonProcessingException
>>   is already a subclass of IOException. Wrapping JsonProcessingException
>> in
>> a ParseException is just adding a level of indirection for which I see no
>> point, especially when the methods already throw IOException.
>>
>> As a side note, why can't ParseException extend IOException? You parse
>> some
>> kind of _input_, parsing does not happen in a void.
>>
>> Gary
>>
>> On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org>
>> wrote:
>>
>> But that won't work, I need to at least translate JsonProcessingException
>>> (Jackson specific, which we don't want to expose) to our own
>>> ParseException.
>>>
>>>
>>>
>>> On 2017-07-23 22:01, Gary Gregory wrote:
>>>
>>>       @Override
>>>>       public LogEvent parseFrom(InputStream input) throws IOException,
>>>> ParseException {
>>>>           return objectReader.readValue(input);
>>>>       }
>>>>
>>>>
>>>
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
The code you posted will never throw ParseException.

Yes, ParseException could extend IOException, but I would still have to 
do translation.


On 2017-07-24 22:20, Gary Gregory wrote:
> Whaaaat? Note that the code I posted here compiles just fine. A
> JsonProcessingException
>   is already a subclass of IOException. Wrapping JsonProcessingException in
> a ParseException is just adding a level of indirection for which I see no
> point, especially when the methods already throw IOException.
> 
> As a side note, why can't ParseException extend IOException? You parse some
> kind of _input_, parsing does not happen in a void.
> 
> Gary
> 
> On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org> wrote:
> 
>> But that won't work, I need to at least translate JsonProcessingException
>> (Jackson specific, which we don't want to expose) to our own ParseException.
>>
>>
>>
>> On 2017-07-23 22:01, Gary Gregory wrote:
>>
>>>       @Override
>>>       public LogEvent parseFrom(InputStream input) throws IOException,
>>> ParseException {
>>>           return objectReader.readValue(input);
>>>       }
>>>
>>
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
Whaaaat? Note that the code I posted here compiles just fine. A
JsonProcessingException
 is already a subclass of IOException. Wrapping JsonProcessingException in
a ParseException is just adding a level of indirection for which I see no
point, especially when the methods already throw IOException.

As a side note, why can't ParseException extend IOException? You parse some
kind of _input_, parsing does not happen in a void.

Gary

On Mon, Jul 24, 2017 at 12:56 PM, Mikael Ståldal <mi...@apache.org> wrote:

> But that won't work, I need to at least translate JsonProcessingException
> (Jackson specific, which we don't want to expose) to our own ParseException.
>
>
>
> On 2017-07-23 22:01, Gary Gregory wrote:
>
>>      @Override
>>      public LogEvent parseFrom(InputStream input) throws IOException,
>> ParseException {
>>          return objectReader.readValue(input);
>>      }
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
But that won't work, I need to at least translate 
JsonProcessingException (Jackson specific, which we don't want to 
expose) to our own ParseException.


On 2017-07-23 22:01, Gary Gregory wrote:
>      @Override
>      public LogEvent parseFrom(InputStream input) throws IOException,
> ParseException {
>          return objectReader.readValue(input);
>      }

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
For my money, I also do not see the point of declaring IOException in the
API and then changing one kind of IOException to a ParseException.

I would just rewrite the class like this:

class AbstractJacksonLogEventParser implements TextLogEventParser {
    final ObjectReader objectReader;

    AbstractJacksonLogEventParser(ObjectReader objectReader) {
        this.objectReader = objectReader;
    }

    @Override
    public LogEvent parseFrom(InputStream input) throws IOException,
ParseException {
        return objectReader.readValue(input);
    }

    @Override
    public LogEvent parseFrom(Reader input) throws IOException,
ParseException {
        return objectReader.readValue(input);
    }

    @Override
    public LogEvent parseFrom(String input) throws ParseException {
        try {
            return objectReader.readValue(input);
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }

    @Override
    public LogEvent parseFrom(byte[] input) throws ParseException {
        try {
            return objectReader.readValue(input);
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }

    @Override
    public LogEvent parseFrom(byte[] input, int offset, int length) throws
ParseException {
        try {
            return objectReader.readValue(input, offset, length);
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }
}

The other other question is whether all APIs should declare IOException and
ParseException.

Gary

On Sat, Jul 22, 2017 at 1:48 PM, <mi...@apache.org> wrote:

> Repository: logging-log4j2
> Updated Branches:
>   refs/heads/LOG4J2-1986 [created] c4814a873
>
>
> LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
> into a LogEvent
>
>
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> commit/c4814a87
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87
>
> Branch: refs/heads/LOG4J2-1986
> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> Parents: eea873e
> Author: Mikael Ståldal <mi...@staldal.nu>
> Authored: Sat Jul 22 22:48:24 2017 +0200
> Committer: Mikael Ståldal <mi...@staldal.nu>
> Committed: Sat Jul 22 22:48:24 2017 +0200
>
> ----------------------------------------------------------------------
>  .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
>  .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>  .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>  .../log4j/core/parser/ParseException.java       |  34 ++++++
>  .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>  .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>  .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>  .../logging/log4j/core/parser/package-info.java |  20 ++++
>  .../core/parser/JsonLogEventParserTest.java     | 103 +++++++++++++++++++
>  src/changes/changes.xml                         |   3 +
>  10 files changed, 456 insertions(+)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
> new file mode 100644
> index 0000000..8b00e57
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> @@ -0,0 +1,85 @@
> +/*
> + * 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.parser;
> +
> +import com.fasterxml.jackson.core.JsonProcessingException;
> +import com.fasterxml.jackson.databind.ObjectReader;
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +import java.io.Reader;
> +
> +class AbstractJacksonLogEventParser implements TextLogEventParser {
> +    final ObjectReader objectReader;
> +
> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> +        this.objectReader = objectReader;
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(InputStream input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(Reader input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(String input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input, int offset, int length)
> throws ParseException {
> +        try {
> +            return objectReader.readValue(input, offset, length);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> new file mode 100644
> index 0000000..218de0b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +/**
> + * Parse the output from JsonLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public JsonLogEventParser() {
> +        super(new Log4jJsonObjectMapper().readerFor(Log4jLogEvent.class)
> );
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/LogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> new file mode 100644
> index 0000000..88e84fb
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> @@ -0,0 +1,64 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +
> +/**
> + * Parse the output from a layout into instances of {@link LogEvent}.
> + */
> +public interface LogEventParser {
> +
> +    /**
> +     * Parse from an InputStream. The input stream may contain multiple
> log events.
> +     * This method will read one log event and leave the stream open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the input stream
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(InputStream input) throws IOException,
> ParseException;
> +
> +    /**
> +     * Parse from a byte array, which is expected to contain exactly one
> log event.
> +     *
> +     * @param input  the byte array
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input) throws ParseException;
> +
> +    /**
> +     * Parse from a specified range of a byte array. The specified range
> is expected to contain
> +     * exactly one log event.
> +     *
> +     * @param input  the byte array
> +     * @param offset  the initial offset
> +     * @param length  the length
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
> ParseException;
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/ParseException.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> new file mode 100644
> index 0000000..8f5fa54
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> @@ -0,0 +1,34 @@
> +/*
> + * 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.parser;
> +
> +/**
> + * Thrown when the input cannot be parsed.
> + */
> +public class ParseException extends Exception {
> +    public ParseException(String message) {
> +        super(message);
> +    }
> +
> +    public ParseException(String message, Throwable cause) {
> +        super(message, cause);
> +    }
> +
> +    public ParseException(Throwable cause) {
> +        super(cause);
> +    }
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> TextLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> new file mode 100644
> index 0000000..3428024
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> @@ -0,0 +1,50 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.Reader;
> +
> +/**
> + * Parse the output from a text based layout into instances of {@link
> LogEvent}.
> + */
> +public interface TextLogEventParser extends LogEventParser {
> +    /**
> +     * Parse from a Reader. The reader may contain multiple log events.
> +     * This method will read one log event and leave the reader open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the reader
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(Reader input) throws IOException, ParseException;
> +
> +    /**
> +     * Parse from a String, which is expected to contain exactly one log
> event.
> +     *
> +     * @param input  the string
> +     *
> +     * @return the parsed LogEvent
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(String input) throws ParseException;
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/XmlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> new file mode 100644
> index 0000000..597e70b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +
> +/**
> + * Parse the output from XmlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public XmlLogEventParser() {
> +        super(new Log4jXmlObjectMapper().readerFor(Log4jLogEvent.class));
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> YamlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> new file mode 100644
> index 0000000..59b3463
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> @@ -0,0 +1,33 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
> +
> +/**
> + * Parse the output from YamlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public YamlLogEventParser() {
> +        super(new Log4jYamlObjectMapper().readerFor(Log4jLogEvent.class)
> );
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/package-info.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> new file mode 100644
> index 0000000..3506570
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> @@ -0,0 +1,20 @@
> +/*
> + * 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.
> + */
> +/**
> + * Parsers for the output of various layouts.
> + */
> +package org.apache.logging.log4j.core.parser;
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParserTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> new file mode 100644
> index 0000000..5684602
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java
> @@ -0,0 +1,103 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import static org.junit.Assert.assertEquals;
> +import static org.junit.Assert.assertNotNull;
> +
> +public class JsonLogEventParserTest {
> +
> +    private JsonLogEventParser parser;
> +
> +    private static final String JSON = "{\n" +
> +            "  \"timeMillis\" : 1493121664118,\n" +
> +            "  \"thread\" : \"main\",\n" +
> +            "  \"level\" : \"INFO\",\n" +
> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> +            "  \"marker\" : {\n" +
> +            "    \"name\" : \"child\",\n" +
> +            "    \"parents\" : [ {\n" +
> +            "      \"name\" : \"parent\",\n" +
> +            "      \"parents\" : [ {\n" +
> +            "        \"name\" : \"grandparent\"\n" +
> +            "      } ]\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"message\" : \"Hello, world!\",\n" +
> +            "  \"thrown\" : {\n" +
> +            "    \"commonElementCount\" : 0,\n" +
> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> +            "    \"extendedStackTrace\" : [ {\n" +
> +            "      \"class\" : \"logtest.Main\",\n" +
> +            "      \"method\" : \"main\",\n" +
> +            "      \"file\" : \"Main.java\",\n" +
> +            "      \"line\" : 29,\n" +
> +            "      \"exact\" : true,\n" +
> +            "      \"location\" : \"classes/\",\n" +
> +            "      \"version\" : \"?\"\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> +            "  \"endOfBatch\" : false,\n" +
> +            "  \"loggerFqcn\" : \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> +
> +            "  \"contextMap\" : {\n" +
> +            "    \"bar\" : \"BAR\",\n" +
> +            "    \"foo\" : \"FOO\"\n" +
> +            "  },\n" +
> +            "  \"threadId\" : 1,\n" +
> +            "  \"threadPriority\" : 5,\n" +
> +            "  \"source\" : {\n" +
> +            "    \"class\" : \"logtest.Main\",\n" +
> +            "    \"method\" : \"main\",\n" +
> +            "    \"file\" : \"Main.java\",\n" +
> +            "    \"line\" : 29\n" +
> +            "  }\n" +
> +            "}";
> +
> +    @Before
> +    public void setup() {
> +        parser = new JsonLogEventParser();
> +    }
> +
> +    @Test
> +    public void testString() throws ParseException {
> +        LogEvent logEvent = parser.parseFrom(JSON);
> +        assertNotNull(logEvent);
> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> +        // TODO assert more here
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringEmpty() throws ParseException {
> +        parser.parseFrom("");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidJson() throws ParseException {
> +        parser.parseFrom("foobar");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidProperty() throws ParseException {
> +        parser.parseFrom("{\"foo\":\"bar\"}");
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index 074e31c..522f49b 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -31,6 +31,9 @@
>           - "remove" - Removed
>      -->
>      <release version="2.9.0" date="2017-MM-DD" description="GA Release
> 2.9.0">
> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> +        Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
> into a LogEvent.
> +      </action>
>        <action issue="LOG4J2-1984" dev="rgoers" type="update">
>          Allow maxLength of StructuredData to be specified by the user.
>        </action>
>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
Hi,

Parse exception is missing its serialVersionUID

Gary

On Sat, Jul 22, 2017 at 1:48 PM, <mi...@apache.org> wrote:

> Repository: logging-log4j2
> Updated Branches:
>   refs/heads/LOG4J2-1986 [created] c4814a873
>
>
> LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
> into a LogEvent
>
>
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> commit/c4814a87
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87
>
> Branch: refs/heads/LOG4J2-1986
> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> Parents: eea873e
> Author: Mikael Ståldal <mi...@staldal.nu>
> Authored: Sat Jul 22 22:48:24 2017 +0200
> Committer: Mikael Ståldal <mi...@staldal.nu>
> Committed: Sat Jul 22 22:48:24 2017 +0200
>
> ----------------------------------------------------------------------
>  .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
>  .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>  .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>  .../log4j/core/parser/ParseException.java       |  34 ++++++
>  .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>  .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>  .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>  .../logging/log4j/core/parser/package-info.java |  20 ++++
>  .../core/parser/JsonLogEventParserTest.java     | 103 +++++++++++++++++++
>  src/changes/changes.xml                         |   3 +
>  10 files changed, 456 insertions(+)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
> new file mode 100644
> index 0000000..8b00e57
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> @@ -0,0 +1,85 @@
> +/*
> + * 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.parser;
> +
> +import com.fasterxml.jackson.core.JsonProcessingException;
> +import com.fasterxml.jackson.databind.ObjectReader;
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +import java.io.Reader;
> +
> +class AbstractJacksonLogEventParser implements TextLogEventParser {
> +    final ObjectReader objectReader;
> +
> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> +        this.objectReader = objectReader;
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(InputStream input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(Reader input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(String input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input, int offset, int length)
> throws ParseException {
> +        try {
> +            return objectReader.readValue(input, offset, length);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> new file mode 100644
> index 0000000..218de0b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +/**
> + * Parse the output from JsonLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public JsonLogEventParser() {
> +        super(new Log4jJsonObjectMapper().readerFor(Log4jLogEvent.class)
> );
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/LogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> new file mode 100644
> index 0000000..88e84fb
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> @@ -0,0 +1,64 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +
> +/**
> + * Parse the output from a layout into instances of {@link LogEvent}.
> + */
> +public interface LogEventParser {
> +
> +    /**
> +     * Parse from an InputStream. The input stream may contain multiple
> log events.
> +     * This method will read one log event and leave the stream open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the input stream
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(InputStream input) throws IOException,
> ParseException;
> +
> +    /**
> +     * Parse from a byte array, which is expected to contain exactly one
> log event.
> +     *
> +     * @param input  the byte array
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input) throws ParseException;
> +
> +    /**
> +     * Parse from a specified range of a byte array. The specified range
> is expected to contain
> +     * exactly one log event.
> +     *
> +     * @param input  the byte array
> +     * @param offset  the initial offset
> +     * @param length  the length
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
> ParseException;
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/ParseException.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> new file mode 100644
> index 0000000..8f5fa54
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> @@ -0,0 +1,34 @@
> +/*
> + * 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.parser;
> +
> +/**
> + * Thrown when the input cannot be parsed.
> + */
> +public class ParseException extends Exception {
> +    public ParseException(String message) {
> +        super(message);
> +    }
> +
> +    public ParseException(String message, Throwable cause) {
> +        super(message, cause);
> +    }
> +
> +    public ParseException(Throwable cause) {
> +        super(cause);
> +    }
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> TextLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> new file mode 100644
> index 0000000..3428024
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> @@ -0,0 +1,50 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.Reader;
> +
> +/**
> + * Parse the output from a text based layout into instances of {@link
> LogEvent}.
> + */
> +public interface TextLogEventParser extends LogEventParser {
> +    /**
> +     * Parse from a Reader. The reader may contain multiple log events.
> +     * This method will read one log event and leave the reader open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the reader
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(Reader input) throws IOException, ParseException;
> +
> +    /**
> +     * Parse from a String, which is expected to contain exactly one log
> event.
> +     *
> +     * @param input  the string
> +     *
> +     * @return the parsed LogEvent
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(String input) throws ParseException;
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/XmlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> new file mode 100644
> index 0000000..597e70b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +
> +/**
> + * Parse the output from XmlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public XmlLogEventParser() {
> +        super(new Log4jXmlObjectMapper().readerFor(Log4jLogEvent.class));
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> YamlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> new file mode 100644
> index 0000000..59b3463
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> @@ -0,0 +1,33 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
> +
> +/**
> + * Parse the output from YamlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public YamlLogEventParser() {
> +        super(new Log4jYamlObjectMapper().readerFor(Log4jLogEvent.class)
> );
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/package-info.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> new file mode 100644
> index 0000000..3506570
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> @@ -0,0 +1,20 @@
> +/*
> + * 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.
> + */
> +/**
> + * Parsers for the output of various layouts.
> + */
> +package org.apache.logging.log4j.core.parser;
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParserTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> new file mode 100644
> index 0000000..5684602
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java
> @@ -0,0 +1,103 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import static org.junit.Assert.assertEquals;
> +import static org.junit.Assert.assertNotNull;
> +
> +public class JsonLogEventParserTest {
> +
> +    private JsonLogEventParser parser;
> +
> +    private static final String JSON = "{\n" +
> +            "  \"timeMillis\" : 1493121664118,\n" +
> +            "  \"thread\" : \"main\",\n" +
> +            "  \"level\" : \"INFO\",\n" +
> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> +            "  \"marker\" : {\n" +
> +            "    \"name\" : \"child\",\n" +
> +            "    \"parents\" : [ {\n" +
> +            "      \"name\" : \"parent\",\n" +
> +            "      \"parents\" : [ {\n" +
> +            "        \"name\" : \"grandparent\"\n" +
> +            "      } ]\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"message\" : \"Hello, world!\",\n" +
> +            "  \"thrown\" : {\n" +
> +            "    \"commonElementCount\" : 0,\n" +
> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> +            "    \"extendedStackTrace\" : [ {\n" +
> +            "      \"class\" : \"logtest.Main\",\n" +
> +            "      \"method\" : \"main\",\n" +
> +            "      \"file\" : \"Main.java\",\n" +
> +            "      \"line\" : 29,\n" +
> +            "      \"exact\" : true,\n" +
> +            "      \"location\" : \"classes/\",\n" +
> +            "      \"version\" : \"?\"\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> +            "  \"endOfBatch\" : false,\n" +
> +            "  \"loggerFqcn\" : \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> +
> +            "  \"contextMap\" : {\n" +
> +            "    \"bar\" : \"BAR\",\n" +
> +            "    \"foo\" : \"FOO\"\n" +
> +            "  },\n" +
> +            "  \"threadId\" : 1,\n" +
> +            "  \"threadPriority\" : 5,\n" +
> +            "  \"source\" : {\n" +
> +            "    \"class\" : \"logtest.Main\",\n" +
> +            "    \"method\" : \"main\",\n" +
> +            "    \"file\" : \"Main.java\",\n" +
> +            "    \"line\" : 29\n" +
> +            "  }\n" +
> +            "}";
> +
> +    @Before
> +    public void setup() {
> +        parser = new JsonLogEventParser();
> +    }
> +
> +    @Test
> +    public void testString() throws ParseException {
> +        LogEvent logEvent = parser.parseFrom(JSON);
> +        assertNotNull(logEvent);
> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> +        // TODO assert more here
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringEmpty() throws ParseException {
> +        parser.parseFrom("");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidJson() throws ParseException {
> +        parser.parseFrom("foobar");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidProperty() throws ParseException {
> +        parser.parseFrom("{\"foo\":\"bar\"}");
> +    }
> +
> +}
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index 074e31c..522f49b 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -31,6 +31,9 @@
>           - "remove" - Removed
>      -->
>      <release version="2.9.0" date="2017-MM-DD" description="GA Release
> 2.9.0">
> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> +        Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
> into a LogEvent.
> +      </action>
>        <action issue="LOG4J2-1984" dev="rgoers" type="update">
>          Allow maxLength of StructuredData to be specified by the user.
>        </action>
>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
Yes, I'll do that.


On 2017-07-23 23:46, Gary Gregory wrote:
> Also, I'd like to see this used from our socket server classes to make sure
> it fits nicely with that use case.
> 
> Gary
> 
> On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
> wrote:
> 
>> OK, but RuntimeException should still never be thrown here...
>>
>> Gary
>>
>> On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org> wrote:
>>
>>> But that's not quite correct. I want to distinguish between unable to
>>> parse due to malformed input (ParseException), and unable to read from
>>> input (IOException). When reading from String or byte[], unable to read
>>> from input shouldn't happen.
>>>
>>>
>>> On 2017-07-23 21:50, Gary Gregory wrote:
>>>
>>>> You already declare and throw ParseException, so I would use
>>>> ParseException.
>>>>
>>>> Gary
>>>>
>>>>
>>>>
>>>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
>>>> wrote:
>>>>
>>>> Do you have another suggestion?
>>>>>
>>>>>
>>>>> On 2017-07-23 05:01, Gary Gregory wrote:
>>>>>
>>>>> -1 to throw new RuntimeException(e)
>>>>>>
>>>>>> Gary
>>>>>>
>>>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>>>>
>>>>>> Repository: logging-log4j2
>>>>>> Updated Branches:
>>>>>>      refs/heads/LOG4J2-1986 [created] c4814a873
>>>>>>
>>>>>>
>>>>>> LOG4J2-1986 Public API for parsing the output from
>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>> into a LogEvent
>>>>>>
>>>>>>
>>>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>>>>> commit/c4814a87
>>>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
>>>>>> 4814a87
>>>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
>>>>>> 4814a87
>>>>>>
>>>>>> Branch: refs/heads/LOG4J2-1986
>>>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>>>>> Parents: eea873e
>>>>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>>>>
>>>>>> ----------------------------------------------------------------------
>>>>>>     .../parser/AbstractJacksonLogEventParser.java   |  85
>>>>>> +++++++++++++++
>>>>>>     .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>>>>     .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>>>>>     .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>>>>     .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>>>>     .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>>>>     .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>>>>     .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>>>>     .../core/parser/JsonLogEventParserTest.java     | 103
>>>>>> +++++++++++++++++++
>>>>>>     src/changes/changes.xml                         |   3 +
>>>>>>     10 files changed, 456 insertions(+)
>>>>>> ----------------------------------------------------------------------
>>>>>>
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> AbstractJacksonLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/
>>>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
>>>>>> Parser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..8b00e57
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>>>> AbstractJacksonLogEventParser.java
>>>>>> @@ -0,0 +1,85 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>>>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.InputStream;
>>>>>> +import java.io.Reader;
>>>>>> +
>>>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>>>>>> +    final ObjectReader objectReader;
>>>>>> +
>>>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>>>>> +        this.objectReader = objectReader;
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(String input) throws ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>>>>>> throws
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input, offset, length);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> JsonLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/JsonLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..218de0b
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParser.java
>>>>>> @@ -0,0 +1,32 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from JsonLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser
>>>>>> {
>>>>>> +
>>>>>> +    public JsonLogEventParser() {
>>>>>> +        super(new Log4jJsonObjectMapper().reader
>>>>>> For(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/LogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/LogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/LogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..88e84fb
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/LogEventParser.java
>>>>>> @@ -0,0 +1,64 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.InputStream;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from a layout into instances of {@link LogEvent}.
>>>>>> + */
>>>>>> +public interface LogEventParser {
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from an InputStream. The input stream may contain
>>>>>> multiple
>>>>>> log events.
>>>>>> +     * This method will read one log event and leave the stream open
>>>>>> and
>>>>>> positioned to read the
>>>>>> +     * next log event.
>>>>>> +     *
>>>>>> +     * @param input  the input stream
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>> input is
>>>>>> reached.
>>>>>> +     * @throws IOException if unable to read from the input
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>>>>> ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a byte array, which is expected to contain exactly
>>>>>> one
>>>>>> log event.
>>>>>> +     *
>>>>>> +     * @param input  the byte array
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a specified range of a byte array. The specified
>>>>>> range
>>>>>> is expected to contain
>>>>>> +     * exactly one log event.
>>>>>> +     *
>>>>>> +     * @param input  the byte array
>>>>>> +     * @param offset  the initial offset
>>>>>> +     * @param length  the length
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>>>>>> ParseException;
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/ParseException.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/ParseException.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/ParseException.java
>>>>>> new file mode 100644
>>>>>> index 0000000..8f5fa54
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/ParseException.java
>>>>>> @@ -0,0 +1,34 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +/**
>>>>>> + * Thrown when the input cannot be parsed.
>>>>>> + */
>>>>>> +public class ParseException extends Exception {
>>>>>> +    public ParseException(String message) {
>>>>>> +        super(message);
>>>>>> +    }
>>>>>> +
>>>>>> +    public ParseException(String message, Throwable cause) {
>>>>>> +        super(message, cause);
>>>>>> +    }
>>>>>> +
>>>>>> +    public ParseException(Throwable cause) {
>>>>>> +        super(cause);
>>>>>> +    }
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> TextLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/TextLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/TextLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..3428024
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/TextLogEventParser.java
>>>>>> @@ -0,0 +1,50 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.Reader;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from a text based layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public interface TextLogEventParser extends LogEventParser {
>>>>>> +    /**
>>>>>> +     * Parse from a Reader. The reader may contain multiple log
>>>>>> events.
>>>>>> +     * This method will read one log event and leave the reader open
>>>>>> and
>>>>>> positioned to read the
>>>>>> +     * next log event.
>>>>>> +     *
>>>>>> +     * @param input  the reader
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>> input is
>>>>>> reached.
>>>>>> +     * @throws IOException if unable to read from the input
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(Reader input) throws IOException,
>>>>>> ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a String, which is expected to contain exactly one
>>>>>> log
>>>>>> event.
>>>>>> +     *
>>>>>> +     * @param input  the string
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(String input) throws ParseException;
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/XmlLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/XmlLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/XmlLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..597e70b
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/XmlLogEventParser.java
>>>>>> @@ -0,0 +1,32 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from XmlLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>>>>>> +
>>>>>> +    public XmlLogEventParser() {
>>>>>> +        super(new Log4jXmlObjectMapper().readerF
>>>>>> or(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> YamlLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/YamlLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/YamlLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..59b3463
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/YamlLogEventParser.java
>>>>>> @@ -0,0 +1,33 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from YamlLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser
>>>>>> {
>>>>>> +
>>>>>> +    public YamlLogEventParser() {
>>>>>> +        super(new Log4jYamlObjectMapper().reader
>>>>>> For(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/package-info.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/package-info.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/package-info.java
>>>>>> new file mode 100644
>>>>>> index 0000000..3506570
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/package-info.java
>>>>>> @@ -0,0 +1,20 @@
>>>>>> +/*
>>>>>> + * 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.
>>>>>> + */
>>>>>> +/**
>>>>>> + * Parsers for the output of various layouts.
>>>>>> + */
>>>>>> +package org.apache.logging.log4j.core.parser;
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> JsonLogEventParserTest.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>>>>> new file mode 100644
>>>>>> index 0000000..5684602
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParserTest.java
>>>>>> @@ -0,0 +1,103 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.junit.Before;
>>>>>> +import org.junit.Test;
>>>>>> +
>>>>>> +import static org.junit.Assert.assertEquals;
>>>>>> +import static org.junit.Assert.assertNotNull;
>>>>>> +
>>>>>> +public class JsonLogEventParserTest {
>>>>>> +
>>>>>> +    private JsonLogEventParser parser;
>>>>>> +
>>>>>> +    private static final String JSON = "{\n" +
>>>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>>>>> +            "  \"thread\" : \"main\",\n" +
>>>>>> +            "  \"level\" : \"INFO\",\n" +
>>>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>>>>> +            "  \"marker\" : {\n" +
>>>>>> +            "    \"name\" : \"child\",\n" +
>>>>>> +            "    \"parents\" : [ {\n" +
>>>>>> +            "      \"name\" : \"parent\",\n" +
>>>>>> +            "      \"parents\" : [ {\n" +
>>>>>> +            "        \"name\" : \"grandparent\"\n" +
>>>>>> +            "      } ]\n" +
>>>>>> +            "    } ]\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>>>>> +            "  \"thrown\" : {\n" +
>>>>>> +            "    \"commonElementCount\" : 0,\n" +
>>>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>>>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>>>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>>>>> +            "      \"method\" : \"main\",\n" +
>>>>>> +            "      \"file\" : \"Main.java\",\n" +
>>>>>> +            "      \"line\" : 29,\n" +
>>>>>> +            "      \"exact\" : true,\n" +
>>>>>> +            "      \"location\" : \"classes/\",\n" +
>>>>>> +            "      \"version\" : \"?\"\n" +
>>>>>> +            "    } ]\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>>>>> +            "  \"endOfBatch\" : false,\n" +
>>>>>> +            "  \"loggerFqcn\" :
>>>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>>>>> +
>>>>>> +            "  \"contextMap\" : {\n" +
>>>>>> +            "    \"bar\" : \"BAR\",\n" +
>>>>>> +            "    \"foo\" : \"FOO\"\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"threadId\" : 1,\n" +
>>>>>> +            "  \"threadPriority\" : 5,\n" +
>>>>>> +            "  \"source\" : {\n" +
>>>>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>>>>> +            "    \"method\" : \"main\",\n" +
>>>>>> +            "    \"file\" : \"Main.java\",\n" +
>>>>>> +            "    \"line\" : 29\n" +
>>>>>> +            "  }\n" +
>>>>>> +            "}";
>>>>>> +
>>>>>> +    @Before
>>>>>> +    public void setup() {
>>>>>> +        parser = new JsonLogEventParser();
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test
>>>>>> +    public void testString() throws ParseException {
>>>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>>>>> +        assertNotNull(logEvent);
>>>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>>>>> +        // TODO assert more here
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringEmpty() throws ParseException {
>>>>>> +        parser.parseFrom("");
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringInvalidJson() throws ParseException {
>>>>>> +        parser.parseFrom("foobar");
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringInvalidProperty() throws ParseException {
>>>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/src/changes/changes.xml
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>>> index 074e31c..522f49b 100644
>>>>>> --- a/src/changes/changes.xml
>>>>>> +++ b/src/changes/changes.xml
>>>>>> @@ -31,6 +31,9 @@
>>>>>>              - "remove" - Removed
>>>>>>         -->
>>>>>>         <release version="2.9.0" date="2017-MM-DD" description="GA
>>>>>> Release
>>>>>> 2.9.0">
>>>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>>>>> +        Public API for parsing the output from
>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>> into a LogEvent.
>>>>>> +      </action>
>>>>>>           <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>>>>             Allow maxLength of StructuredData to be specified by the
>>>>>> user.
>>>>>>           </action>
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
But what would that be? I think that java.io.IOException is fine when 
unable to read from underlaying input.

But that shouldn't happen when reading from String or byte[], should it?

I don't feel like defining "BogusIOException".


On 2017-07-24 05:28, Matt Sicker wrote:
> But some of the methods threw a runtime exception which could just be
> another type of parse exception if differentiation is necessary.
> 
> On 23 July 2017 at 21:16, Gary Gregory <ga...@gmail.com> wrote:
> 
>> On Jul 23, 2017 19:10, "Matt Sicker" <bo...@gmail.com> wrote:
>>
>> Nothing wrong with making more custom exceptions.
>>
>>
>> This new package already defines a new exception ParseException.
>>
>> Gary
>>
>>
>> On 23 July 2017 at 16:46, Gary Gregory <ga...@gmail.com> wrote:
>>
>>> Also, I'd like to see this used from our socket server classes to make
>> sure
>>> it fits nicely with that use case.
>>>
>>> Gary
>>>
>>> On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
>>> wrote:
>>>
>>>> OK, but RuntimeException should still never be thrown here...
>>>>
>>>> Gary
>>>>
>>>> On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org>
>>> wrote:
>>>>
>>>>> But that's not quite correct. I want to distinguish between unable to
>>>>> parse due to malformed input (ParseException), and unable to read from
>>>>> input (IOException). When reading from String or byte[], unable to
>> read
>>>>> from input shouldn't happen.
>>>>>
>>>>>
>>>>> On 2017-07-23 21:50, Gary Gregory wrote:
>>>>>
>>>>>> You already declare and throw ParseException, so I would use
>>>>>> ParseException.
>>>>>>
>>>>>> Gary
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
>>>>>> wrote:
>>>>>>
>>>>>> Do you have another suggestion?
>>>>>>>
>>>>>>>
>>>>>>> On 2017-07-23 05:01, Gary Gregory wrote:
>>>>>>>
>>>>>>> -1 to throw new RuntimeException(e)
>>>>>>>>
>>>>>>>> Gary
>>>>>>>>
>>>>>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>>>>>>
>>>>>>>> Repository: logging-log4j2
>>>>>>>> Updated Branches:
>>>>>>>>      refs/heads/LOG4J2-1986 [created] c4814a873
>>>>>>>>
>>>>>>>>
>>>>>>>> LOG4J2-1986 Public API for parsing the output from
>>>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>>>> into a LogEvent
>>>>>>>>
>>>>>>>>
>>>>>>>> Project: http://git-wip-us.apache.org/
>> repos/asf/logging-log4j2/repo
>>>>>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>>>>>>> commit/c4814a87
>>>>>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
>>>>>>>> 4814a87
>>>>>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
>>>>>>>> 4814a87
>>>>>>>>
>>>>>>>> Branch: refs/heads/LOG4J2-1986
>>>>>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>>>>>>> Parents: eea873e
>>>>>>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>>>>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>>>>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>>>>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>>>>>>
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>>     .../parser/AbstractJacksonLogEventParser.java   |  85
>>>>>>>> +++++++++++++++
>>>>>>>>     .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>>>>>>     .../log4j/core/parser/LogEventParser.java       |  64
>>> ++++++++++++
>>>>>>>>     .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>>>>>>     .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>>>>>>     .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>>>>>>     .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>>>>>>     .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>>>>>>     .../core/parser/JsonLogEventParserTest.java     | 103
>>>>>>>> +++++++++++++++++++
>>>>>>>>     src/changes/changes.xml                         |   3 +
>>>>>>>>     10 files changed, 456 insertions(+)
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>>
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>>>> ore/parser/
>>>>>>>> AbstractJacksonLogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/
>>>>>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>>>>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
>>>>>>>> Parser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..8b00e57
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/
>>>>>>>> AbstractJacksonLogEventParser.java
>>>>>>>> @@ -0,0 +1,85 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>>>>>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.
>> jackson.Log4jJsonObjectMapper;
>>>>>>>> +
>>>>>>>> +import java.io.IOException;
>>>>>>>> +import java.io.InputStream;
>>>>>>>> +import java.io.Reader;
>>>>>>>> +
>>>>>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser
>> {
>>>>>>>> +    final ObjectReader objectReader;
>>>>>>>> +
>>>>>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>>>>>>> +        this.objectReader = objectReader;
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Override
>>>>>>>> +    public LogEvent parseFrom(InputStream input) throws
>> IOException,
>>>>>>>> ParseException {
>>>>>>>> +        try {
>>>>>>>> +            return objectReader.readValue(input);
>>>>>>>> +        } catch (JsonProcessingException e) {
>>>>>>>> +            throw new ParseException(e);
>>>>>>>> +        }
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Override
>>>>>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>>>>>>> ParseException {
>>>>>>>> +        try {
>>>>>>>> +            return objectReader.readValue(input);
>>>>>>>> +        } catch (JsonProcessingException e) {
>>>>>>>> +            throw new ParseException(e);
>>>>>>>> +        }
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Override
>>>>>>>> +    public LogEvent parseFrom(String input) throws ParseException
>> {
>>>>>>>> +        try {
>>>>>>>> +            return objectReader.readValue(input);
>>>>>>>> +        } catch (JsonProcessingException e) {
>>>>>>>> +            throw new ParseException(e);
>>>>>>>> +        } catch (IOException e) {
>>>>>>>> +            throw new RuntimeException(e);
>>>>>>>> +        }
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Override
>>>>>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException
>> {
>>>>>>>> +        try {
>>>>>>>> +            return objectReader.readValue(input);
>>>>>>>> +        } catch (JsonProcessingException e) {
>>>>>>>> +            throw new ParseException(e);
>>>>>>>> +        } catch (IOException e) {
>>>>>>>> +            throw new RuntimeException(e);
>>>>>>>> +        }
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Override
>>>>>>>> +    public LogEvent parseFrom(byte[] input, int offset, int
>> length)
>>>>>>>> throws
>>>>>>>> ParseException {
>>>>>>>> +        try {
>>>>>>>> +            return objectReader.readValue(input, offset, length);
>>>>>>>> +        } catch (JsonProcessingException e) {
>>>>>>>> +            throw new ParseException(e);
>>>>>>>> +        } catch (IOException e) {
>>>>>>>> +            throw new RuntimeException(e);
>>>>>>>> +        }
>>>>>>>> +    }
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>>>> ore/parser/
>>>>>>>> JsonLogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/JsonLogEventParser.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/JsonLogEventParser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..218de0b
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/JsonLogEventParser.java
>>>>>>>> @@ -0,0 +1,32 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.
>> jackson.Log4jJsonObjectMapper;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Parse the output from JsonLayout layout into instances of
>> {@link
>>>>>>>> LogEvent}.
>>>>>>>> + */
>>>>>>>> +public class JsonLogEventParser extends
>>> AbstractJacksonLogEventParser
>>>>>>>> {
>>>>>>>> +
>>>>>>>> +    public JsonLogEventParser() {
>>>>>>>> +        super(new Log4jJsonObjectMapper().reader
>>>>>>>> For(Log4jLogEvent.class));
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>>>> core/parser/LogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/LogEventParser.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/LogEventParser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..88e84fb
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/LogEventParser.java
>>>>>>>> @@ -0,0 +1,64 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +
>>>>>>>> +import java.io.IOException;
>>>>>>>> +import java.io.InputStream;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Parse the output from a layout into instances of {@link
>>> LogEvent}.
>>>>>>>> + */
>>>>>>>> +public interface LogEventParser {
>>>>>>>> +
>>>>>>>> +    /**
>>>>>>>> +     * Parse from an InputStream. The input stream may contain
>>>>>>>> multiple
>>>>>>>> log events.
>>>>>>>> +     * This method will read one log event and leave the stream
>> open
>>>>>>>> and
>>>>>>>> positioned to read the
>>>>>>>> +     * next log event.
>>>>>>>> +     *
>>>>>>>> +     * @param input  the input stream
>>>>>>>> +     *
>>>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>>>> input is
>>>>>>>> reached.
>>>>>>>> +     * @throws IOException if unable to read from the input
>>>>>>>> +     * @throws ParseException if the input is malformed and cannot
>>> be
>>>>>>>> parsed as a LogEvent
>>>>>>>> +     */
>>>>>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>>>>>>> ParseException;
>>>>>>>> +
>>>>>>>> +    /**
>>>>>>>> +     * Parse from a byte array, which is expected to contain
>> exactly
>>>>>>>> one
>>>>>>>> log event.
>>>>>>>> +     *
>>>>>>>> +     * @param input  the byte array
>>>>>>>> +     *
>>>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>>>> +     * @throws ParseException if the input is malformed and cannot
>>> be
>>>>>>>> parsed as a LogEvent
>>>>>>>> +     */
>>>>>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>>>>>>> +
>>>>>>>> +    /**
>>>>>>>> +     * Parse from a specified range of a byte array. The specified
>>>>>>>> range
>>>>>>>> is expected to contain
>>>>>>>> +     * exactly one log event.
>>>>>>>> +     *
>>>>>>>> +     * @param input  the byte array
>>>>>>>> +     * @param offset  the initial offset
>>>>>>>> +     * @param length  the length
>>>>>>>> +     *
>>>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>>>> +     * @throws ParseException if the input is malformed and cannot
>>> be
>>>>>>>> parsed as a LogEvent
>>>>>>>> +     */
>>>>>>>> +    LogEvent parseFrom(byte[] input, int offset, int length)
>> throws
>>>>>>>> ParseException;
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>>>> core/parser/ParseException.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/ParseException.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/ParseException.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..8f5fa54
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/ParseException.java
>>>>>>>> @@ -0,0 +1,34 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Thrown when the input cannot be parsed.
>>>>>>>> + */
>>>>>>>> +public class ParseException extends Exception {
>>>>>>>> +    public ParseException(String message) {
>>>>>>>> +        super(message);
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    public ParseException(String message, Throwable cause) {
>>>>>>>> +        super(message, cause);
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    public ParseException(Throwable cause) {
>>>>>>>> +        super(cause);
>>>>>>>> +    }
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>>>> ore/parser/
>>>>>>>> TextLogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/TextLogEventParser.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/TextLogEventParser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..3428024
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/TextLogEventParser.java
>>>>>>>> @@ -0,0 +1,50 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +
>>>>>>>> +import java.io.IOException;
>>>>>>>> +import java.io.Reader;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Parse the output from a text based layout into instances of
>>> {@link
>>>>>>>> LogEvent}.
>>>>>>>> + */
>>>>>>>> +public interface TextLogEventParser extends LogEventParser {
>>>>>>>> +    /**
>>>>>>>> +     * Parse from a Reader. The reader may contain multiple log
>>>>>>>> events.
>>>>>>>> +     * This method will read one log event and leave the reader
>> open
>>>>>>>> and
>>>>>>>> positioned to read the
>>>>>>>> +     * next log event.
>>>>>>>> +     *
>>>>>>>> +     * @param input  the reader
>>>>>>>> +     *
>>>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>>>> input is
>>>>>>>> reached.
>>>>>>>> +     * @throws IOException if unable to read from the input
>>>>>>>> +     * @throws ParseException if the input is malformed and cannot
>>> be
>>>>>>>> parsed as a LogEvent
>>>>>>>> +     */
>>>>>>>> +    LogEvent parseFrom(Reader input) throws IOException,
>>>>>>>> ParseException;
>>>>>>>> +
>>>>>>>> +    /**
>>>>>>>> +     * Parse from a String, which is expected to contain exactly
>> one
>>>>>>>> log
>>>>>>>> event.
>>>>>>>> +     *
>>>>>>>> +     * @param input  the string
>>>>>>>> +     *
>>>>>>>> +     * @return the parsed LogEvent
>>>>>>>> +     * @throws ParseException if the input is malformed and cannot
>>> be
>>>>>>>> parsed as a LogEvent
>>>>>>>> +     */
>>>>>>>> +    LogEvent parseFrom(String input) throws ParseException;
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>>>> core/parser/XmlLogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/XmlLogEventParser.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/XmlLogEventParser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..597e70b
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/XmlLogEventParser.java
>>>>>>>> @@ -0,0 +1,32 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.
>> jackson.Log4jXmlObjectMapper;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Parse the output from XmlLayout layout into instances of {@link
>>>>>>>> LogEvent}.
>>>>>>>> + */
>>>>>>>> +public class XmlLogEventParser extends
>>> AbstractJacksonLogEventParser {
>>>>>>>> +
>>>>>>>> +    public XmlLogEventParser() {
>>>>>>>> +        super(new Log4jXmlObjectMapper().readerF
>>>>>>>> or(Log4jLogEvent.class));
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>>>> ore/parser/
>>>>>>>> YamlLogEventParser.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/YamlLogEventParser.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/YamlLogEventParser.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..59b3463
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/YamlLogEventParser.java
>>>>>>>> @@ -0,0 +1,33 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>>>> +import org.apache.logging.log4j.core.
>> jackson.Log4jXmlObjectMapper;
>>>>>>>> +import org.apache.logging.log4j.core.
>> jackson.Log4jYamlObjectMapper;
>>>>>>>> +
>>>>>>>> +/**
>>>>>>>> + * Parse the output from YamlLayout layout into instances of
>> {@link
>>>>>>>> LogEvent}.
>>>>>>>> + */
>>>>>>>> +public class YamlLogEventParser extends
>>> AbstractJacksonLogEventParser
>>>>>>>> {
>>>>>>>> +
>>>>>>>> +    public YamlLogEventParser() {
>>>>>>>> +        super(new Log4jYamlObjectMapper().reader
>>>>>>>> For(Log4jLogEvent.class));
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>>>> core/parser/package-info.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>>>> /apache/logging/log4j/core/par
>>>>>>>> ser/package-info.java
>>>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/package-info.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..3506570
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>>>> parser/package-info.java
>>>>>>>> @@ -0,0 +1,20 @@
>>>>>>>> +/*
>>>>>>>> + * 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.
>>>>>>>> + */
>>>>>>>> +/**
>>>>>>>> + * Parsers for the output of various layouts.
>>>>>>>> + */
>>>>>>>> +package org.apache.logging.log4j.core.parser;
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
>>>>>>>> ore/parser/
>>>>>>>> JsonLogEventParserTest.java
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/log4j-core/src/test/java/
>> org/apache/logging/log4j/core/
>>>>>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>>>>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..5684602
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>>>>> parser/JsonLogEventParserTest.java
>>>>>>>> @@ -0,0 +1,103 @@
>>>>>>>> +/*
>>>>>>>> + * 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.parser;
>>>>>>>> +
>>>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>>>> +import org.junit.Before;
>>>>>>>> +import org.junit.Test;
>>>>>>>> +
>>>>>>>> +import static org.junit.Assert.assertEquals;
>>>>>>>> +import static org.junit.Assert.assertNotNull;
>>>>>>>> +
>>>>>>>> +public class JsonLogEventParserTest {
>>>>>>>> +
>>>>>>>> +    private JsonLogEventParser parser;
>>>>>>>> +
>>>>>>>> +    private static final String JSON = "{\n" +
>>>>>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>>>>>>> +            "  \"thread\" : \"main\",\n" +
>>>>>>>> +            "  \"level\" : \"INFO\",\n" +
>>>>>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>>>>>>> +            "  \"marker\" : {\n" +
>>>>>>>> +            "    \"name\" : \"child\",\n" +
>>>>>>>> +            "    \"parents\" : [ {\n" +
>>>>>>>> +            "      \"name\" : \"parent\",\n" +
>>>>>>>> +            "      \"parents\" : [ {\n" +
>>>>>>>> +            "        \"name\" : \"grandparent\"\n" +
>>>>>>>> +            "      } ]\n" +
>>>>>>>> +            "    } ]\n" +
>>>>>>>> +            "  },\n" +
>>>>>>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>>>>>>> +            "  \"thrown\" : {\n" +
>>>>>>>> +            "    \"commonElementCount\" : 0,\n" +
>>>>>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>>>>>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>>>>>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>>>>>>> +            "      \"method\" : \"main\",\n" +
>>>>>>>> +            "      \"file\" : \"Main.java\",\n" +
>>>>>>>> +            "      \"line\" : 29,\n" +
>>>>>>>> +            "      \"exact\" : true,\n" +
>>>>>>>> +            "      \"location\" : \"classes/\",\n" +
>>>>>>>> +            "      \"version\" : \"?\"\n" +
>>>>>>>> +            "    } ]\n" +
>>>>>>>> +            "  },\n" +
>>>>>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>>>>>>> +            "  \"endOfBatch\" : false,\n" +
>>>>>>>> +            "  \"loggerFqcn\" :
>>>>>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>>>>>>> +
>>>>>>>> +            "  \"contextMap\" : {\n" +
>>>>>>>> +            "    \"bar\" : \"BAR\",\n" +
>>>>>>>> +            "    \"foo\" : \"FOO\"\n" +
>>>>>>>> +            "  },\n" +
>>>>>>>> +            "  \"threadId\" : 1,\n" +
>>>>>>>> +            "  \"threadPriority\" : 5,\n" +
>>>>>>>> +            "  \"source\" : {\n" +
>>>>>>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>>>>>>> +            "    \"method\" : \"main\",\n" +
>>>>>>>> +            "    \"file\" : \"Main.java\",\n" +
>>>>>>>> +            "    \"line\" : 29\n" +
>>>>>>>> +            "  }\n" +
>>>>>>>> +            "}";
>>>>>>>> +
>>>>>>>> +    @Before
>>>>>>>> +    public void setup() {
>>>>>>>> +        parser = new JsonLogEventParser();
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Test
>>>>>>>> +    public void testString() throws ParseException {
>>>>>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>>>>>>> +        assertNotNull(logEvent);
>>>>>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>>>>>>> +        // TODO assert more here
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Test(expected = ParseException.class)
>>>>>>>> +    public void testStringEmpty() throws ParseException {
>>>>>>>> +        parser.parseFrom("");
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Test(expected = ParseException.class)
>>>>>>>> +    public void testStringInvalidJson() throws ParseException {
>>>>>>>> +        parser.parseFrom("foobar");
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +    @Test(expected = ParseException.class)
>>>>>>>> +    public void testStringInvalidProperty() throws ParseException
>> {
>>>>>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>>>>>>> +    }
>>>>>>>> +
>>>>>>>> +}
>>>>>>>>
>>>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>>>> c4814a87/src/changes/changes.xml
>>>>>>>> ------------------------------------------------------------
>>> ----------
>>>>>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>>>>> index 074e31c..522f49b 100644
>>>>>>>> --- a/src/changes/changes.xml
>>>>>>>> +++ b/src/changes/changes.xml
>>>>>>>> @@ -31,6 +31,9 @@
>>>>>>>>              - "remove" - Removed
>>>>>>>>         -->
>>>>>>>>         <release version="2.9.0" date="2017-MM-DD" description="GA
>>>>>>>> Release
>>>>>>>> 2.9.0">
>>>>>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>>>>>>> +        Public API for parsing the output from
>>>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>>>> into a LogEvent.
>>>>>>>> +      </action>
>>>>>>>>           <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>>>>>>             Allow maxLength of StructuredData to be specified by the
>>>>>>>> user.
>>>>>>>>           </action>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>>
>>
>> --
>> Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Matt Sicker <bo...@gmail.com>.
But some of the methods threw a runtime exception which could just be
another type of parse exception if differentiation is necessary.

On 23 July 2017 at 21:16, Gary Gregory <ga...@gmail.com> wrote:

> On Jul 23, 2017 19:10, "Matt Sicker" <bo...@gmail.com> wrote:
>
> Nothing wrong with making more custom exceptions.
>
>
> This new package already defines a new exception ParseException.
>
> Gary
>
>
> On 23 July 2017 at 16:46, Gary Gregory <ga...@gmail.com> wrote:
>
> > Also, I'd like to see this used from our socket server classes to make
> sure
> > it fits nicely with that use case.
> >
> > Gary
> >
> > On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
> > wrote:
> >
> > > OK, but RuntimeException should still never be thrown here...
> > >
> > > Gary
> > >
> > > On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org>
> > wrote:
> > >
> > >> But that's not quite correct. I want to distinguish between unable to
> > >> parse due to malformed input (ParseException), and unable to read from
> > >> input (IOException). When reading from String or byte[], unable to
> read
> > >> from input shouldn't happen.
> > >>
> > >>
> > >> On 2017-07-23 21:50, Gary Gregory wrote:
> > >>
> > >>> You already declare and throw ParseException, so I would use
> > >>> ParseException.
> > >>>
> > >>> Gary
> > >>>
> > >>>
> > >>>
> > >>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
> > >>> wrote:
> > >>>
> > >>> Do you have another suggestion?
> > >>>>
> > >>>>
> > >>>> On 2017-07-23 05:01, Gary Gregory wrote:
> > >>>>
> > >>>> -1 to throw new RuntimeException(e)
> > >>>>>
> > >>>>> Gary
> > >>>>>
> > >>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
> > >>>>>
> > >>>>> Repository: logging-log4j2
> > >>>>> Updated Branches:
> > >>>>>     refs/heads/LOG4J2-1986 [created] c4814a873
> > >>>>>
> > >>>>>
> > >>>>> LOG4J2-1986 Public API for parsing the output from
> > >>>>> JsonLayout/XmlLayout/YamlLayout
> > >>>>> into a LogEvent
> > >>>>>
> > >>>>>
> > >>>>> Project: http://git-wip-us.apache.org/
> repos/asf/logging-log4j2/repo
> > >>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> > >>>>> commit/c4814a87
> > >>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
> > >>>>> 4814a87
> > >>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
> > >>>>> 4814a87
> > >>>>>
> > >>>>> Branch: refs/heads/LOG4J2-1986
> > >>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> > >>>>> Parents: eea873e
> > >>>>> Author: Mikael Ståldal <mi...@staldal.nu>
> > >>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
> > >>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
> > >>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
> > >>>>>
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>>    .../parser/AbstractJacksonLogEventParser.java   |  85
> > >>>>> +++++++++++++++
> > >>>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
> > >>>>>    .../log4j/core/parser/LogEventParser.java       |  64
> > ++++++++++++
> > >>>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
> > >>>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
> > >>>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
> > >>>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
> > >>>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
> > >>>>>    .../core/parser/JsonLogEventParserTest.java     | 103
> > >>>>> +++++++++++++++++++
> > >>>>>    src/changes/changes.xml                         |   3 +
> > >>>>>    10 files changed, 456 insertions(+)
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>>
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> > >>>>> ore/parser/
> > >>>>> AbstractJacksonLogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/
> > >>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> > >>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
> > >>>>> Parser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..8b00e57
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/
> > >>>>> AbstractJacksonLogEventParser.java
> > >>>>> @@ -0,0 +1,85 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
> > >>>>> +import com.fasterxml.jackson.databind.ObjectReader;
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> > >>>>> +import org.apache.logging.log4j.core.
> jackson.Log4jJsonObjectMapper;
> > >>>>> +
> > >>>>> +import java.io.IOException;
> > >>>>> +import java.io.InputStream;
> > >>>>> +import java.io.Reader;
> > >>>>> +
> > >>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser
> {
> > >>>>> +    final ObjectReader objectReader;
> > >>>>> +
> > >>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> > >>>>> +        this.objectReader = objectReader;
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Override
> > >>>>> +    public LogEvent parseFrom(InputStream input) throws
> IOException,
> > >>>>> ParseException {
> > >>>>> +        try {
> > >>>>> +            return objectReader.readValue(input);
> > >>>>> +        } catch (JsonProcessingException e) {
> > >>>>> +            throw new ParseException(e);
> > >>>>> +        }
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Override
> > >>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
> > >>>>> ParseException {
> > >>>>> +        try {
> > >>>>> +            return objectReader.readValue(input);
> > >>>>> +        } catch (JsonProcessingException e) {
> > >>>>> +            throw new ParseException(e);
> > >>>>> +        }
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Override
> > >>>>> +    public LogEvent parseFrom(String input) throws ParseException
> {
> > >>>>> +        try {
> > >>>>> +            return objectReader.readValue(input);
> > >>>>> +        } catch (JsonProcessingException e) {
> > >>>>> +            throw new ParseException(e);
> > >>>>> +        } catch (IOException e) {
> > >>>>> +            throw new RuntimeException(e);
> > >>>>> +        }
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Override
> > >>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException
> {
> > >>>>> +        try {
> > >>>>> +            return objectReader.readValue(input);
> > >>>>> +        } catch (JsonProcessingException e) {
> > >>>>> +            throw new ParseException(e);
> > >>>>> +        } catch (IOException e) {
> > >>>>> +            throw new RuntimeException(e);
> > >>>>> +        }
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Override
> > >>>>> +    public LogEvent parseFrom(byte[] input, int offset, int
> length)
> > >>>>> throws
> > >>>>> ParseException {
> > >>>>> +        try {
> > >>>>> +            return objectReader.readValue(input, offset, length);
> > >>>>> +        } catch (JsonProcessingException e) {
> > >>>>> +            throw new ParseException(e);
> > >>>>> +        } catch (IOException e) {
> > >>>>> +            throw new RuntimeException(e);
> > >>>>> +        }
> > >>>>> +    }
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> > >>>>> ore/parser/
> > >>>>> JsonLogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/JsonLogEventParser.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/JsonLogEventParser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..218de0b
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/JsonLogEventParser.java
> > >>>>> @@ -0,0 +1,32 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> > >>>>> +import org.apache.logging.log4j.core.
> jackson.Log4jJsonObjectMapper;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Parse the output from JsonLayout layout into instances of
> {@link
> > >>>>> LogEvent}.
> > >>>>> + */
> > >>>>> +public class JsonLogEventParser extends
> > AbstractJacksonLogEventParser
> > >>>>> {
> > >>>>> +
> > >>>>> +    public JsonLogEventParser() {
> > >>>>> +        super(new Log4jJsonObjectMapper().reader
> > >>>>> For(Log4jLogEvent.class));
> > >>>>> +    }
> > >>>>> +
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> > >>>>> core/parser/LogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/LogEventParser.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/LogEventParser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..88e84fb
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/LogEventParser.java
> > >>>>> @@ -0,0 +1,64 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +
> > >>>>> +import java.io.IOException;
> > >>>>> +import java.io.InputStream;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Parse the output from a layout into instances of {@link
> > LogEvent}.
> > >>>>> + */
> > >>>>> +public interface LogEventParser {
> > >>>>> +
> > >>>>> +    /**
> > >>>>> +     * Parse from an InputStream. The input stream may contain
> > >>>>> multiple
> > >>>>> log events.
> > >>>>> +     * This method will read one log event and leave the stream
> open
> > >>>>> and
> > >>>>> positioned to read the
> > >>>>> +     * next log event.
> > >>>>> +     *
> > >>>>> +     * @param input  the input stream
> > >>>>> +     *
> > >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> > >>>>> input is
> > >>>>> reached.
> > >>>>> +     * @throws IOException if unable to read from the input
> > >>>>> +     * @throws ParseException if the input is malformed and cannot
> > be
> > >>>>> parsed as a LogEvent
> > >>>>> +     */
> > >>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
> > >>>>> ParseException;
> > >>>>> +
> > >>>>> +    /**
> > >>>>> +     * Parse from a byte array, which is expected to contain
> exactly
> > >>>>> one
> > >>>>> log event.
> > >>>>> +     *
> > >>>>> +     * @param input  the byte array
> > >>>>> +     *
> > >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> > >>>>> +     * @throws ParseException if the input is malformed and cannot
> > be
> > >>>>> parsed as a LogEvent
> > >>>>> +     */
> > >>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
> > >>>>> +
> > >>>>> +    /**
> > >>>>> +     * Parse from a specified range of a byte array. The specified
> > >>>>> range
> > >>>>> is expected to contain
> > >>>>> +     * exactly one log event.
> > >>>>> +     *
> > >>>>> +     * @param input  the byte array
> > >>>>> +     * @param offset  the initial offset
> > >>>>> +     * @param length  the length
> > >>>>> +     *
> > >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> > >>>>> +     * @throws ParseException if the input is malformed and cannot
> > be
> > >>>>> parsed as a LogEvent
> > >>>>> +     */
> > >>>>> +    LogEvent parseFrom(byte[] input, int offset, int length)
> throws
> > >>>>> ParseException;
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> > >>>>> core/parser/ParseException.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/ParseException.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/ParseException.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..8f5fa54
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/ParseException.java
> > >>>>> @@ -0,0 +1,34 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Thrown when the input cannot be parsed.
> > >>>>> + */
> > >>>>> +public class ParseException extends Exception {
> > >>>>> +    public ParseException(String message) {
> > >>>>> +        super(message);
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    public ParseException(String message, Throwable cause) {
> > >>>>> +        super(message, cause);
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    public ParseException(Throwable cause) {
> > >>>>> +        super(cause);
> > >>>>> +    }
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> > >>>>> ore/parser/
> > >>>>> TextLogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/TextLogEventParser.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/TextLogEventParser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..3428024
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/TextLogEventParser.java
> > >>>>> @@ -0,0 +1,50 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +
> > >>>>> +import java.io.IOException;
> > >>>>> +import java.io.Reader;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Parse the output from a text based layout into instances of
> > {@link
> > >>>>> LogEvent}.
> > >>>>> + */
> > >>>>> +public interface TextLogEventParser extends LogEventParser {
> > >>>>> +    /**
> > >>>>> +     * Parse from a Reader. The reader may contain multiple log
> > >>>>> events.
> > >>>>> +     * This method will read one log event and leave the reader
> open
> > >>>>> and
> > >>>>> positioned to read the
> > >>>>> +     * next log event.
> > >>>>> +     *
> > >>>>> +     * @param input  the reader
> > >>>>> +     *
> > >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> > >>>>> input is
> > >>>>> reached.
> > >>>>> +     * @throws IOException if unable to read from the input
> > >>>>> +     * @throws ParseException if the input is malformed and cannot
> > be
> > >>>>> parsed as a LogEvent
> > >>>>> +     */
> > >>>>> +    LogEvent parseFrom(Reader input) throws IOException,
> > >>>>> ParseException;
> > >>>>> +
> > >>>>> +    /**
> > >>>>> +     * Parse from a String, which is expected to contain exactly
> one
> > >>>>> log
> > >>>>> event.
> > >>>>> +     *
> > >>>>> +     * @param input  the string
> > >>>>> +     *
> > >>>>> +     * @return the parsed LogEvent
> > >>>>> +     * @throws ParseException if the input is malformed and cannot
> > be
> > >>>>> parsed as a LogEvent
> > >>>>> +     */
> > >>>>> +    LogEvent parseFrom(String input) throws ParseException;
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> > >>>>> core/parser/XmlLogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/XmlLogEventParser.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/XmlLogEventParser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..597e70b
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/XmlLogEventParser.java
> > >>>>> @@ -0,0 +1,32 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> > >>>>> +import org.apache.logging.log4j.core.
> jackson.Log4jXmlObjectMapper;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Parse the output from XmlLayout layout into instances of {@link
> > >>>>> LogEvent}.
> > >>>>> + */
> > >>>>> +public class XmlLogEventParser extends
> > AbstractJacksonLogEventParser {
> > >>>>> +
> > >>>>> +    public XmlLogEventParser() {
> > >>>>> +        super(new Log4jXmlObjectMapper().readerF
> > >>>>> or(Log4jLogEvent.class));
> > >>>>> +    }
> > >>>>> +
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> > >>>>> ore/parser/
> > >>>>> YamlLogEventParser.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/YamlLogEventParser.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/YamlLogEventParser.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..59b3463
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/YamlLogEventParser.java
> > >>>>> @@ -0,0 +1,33 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> > >>>>> +import org.apache.logging.log4j.core.
> jackson.Log4jXmlObjectMapper;
> > >>>>> +import org.apache.logging.log4j.core.
> jackson.Log4jYamlObjectMapper;
> > >>>>> +
> > >>>>> +/**
> > >>>>> + * Parse the output from YamlLayout layout into instances of
> {@link
> > >>>>> LogEvent}.
> > >>>>> + */
> > >>>>> +public class YamlLogEventParser extends
> > AbstractJacksonLogEventParser
> > >>>>> {
> > >>>>> +
> > >>>>> +    public YamlLogEventParser() {
> > >>>>> +        super(new Log4jYamlObjectMapper().reader
> > >>>>> For(Log4jLogEvent.class));
> > >>>>> +    }
> > >>>>> +
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> > >>>>> core/parser/package-info.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/main/java/org
> > >>>>> /apache/logging/log4j/core/par
> > >>>>> ser/package-info.java
> > >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/package-info.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..3506570
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> > >>>>> parser/package-info.java
> > >>>>> @@ -0,0 +1,20 @@
> > >>>>> +/*
> > >>>>> + * 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.
> > >>>>> + */
> > >>>>> +/**
> > >>>>> + * Parsers for the output of various layouts.
> > >>>>> + */
> > >>>>> +package org.apache.logging.log4j.core.parser;
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
> > >>>>> ore/parser/
> > >>>>> JsonLogEventParserTest.java
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/log4j-core/src/test/java/
> org/apache/logging/log4j/core/
> > >>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> > >>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> > >>>>> new file mode 100644
> > >>>>> index 0000000..5684602
> > >>>>> --- /dev/null
> > >>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> > >>>>> parser/JsonLogEventParserTest.java
> > >>>>> @@ -0,0 +1,103 @@
> > >>>>> +/*
> > >>>>> + * 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.parser;
> > >>>>> +
> > >>>>> +import org.apache.logging.log4j.core.LogEvent;
> > >>>>> +import org.junit.Before;
> > >>>>> +import org.junit.Test;
> > >>>>> +
> > >>>>> +import static org.junit.Assert.assertEquals;
> > >>>>> +import static org.junit.Assert.assertNotNull;
> > >>>>> +
> > >>>>> +public class JsonLogEventParserTest {
> > >>>>> +
> > >>>>> +    private JsonLogEventParser parser;
> > >>>>> +
> > >>>>> +    private static final String JSON = "{\n" +
> > >>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
> > >>>>> +            "  \"thread\" : \"main\",\n" +
> > >>>>> +            "  \"level\" : \"INFO\",\n" +
> > >>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> > >>>>> +            "  \"marker\" : {\n" +
> > >>>>> +            "    \"name\" : \"child\",\n" +
> > >>>>> +            "    \"parents\" : [ {\n" +
> > >>>>> +            "      \"name\" : \"parent\",\n" +
> > >>>>> +            "      \"parents\" : [ {\n" +
> > >>>>> +            "        \"name\" : \"grandparent\"\n" +
> > >>>>> +            "      } ]\n" +
> > >>>>> +            "    } ]\n" +
> > >>>>> +            "  },\n" +
> > >>>>> +            "  \"message\" : \"Hello, world!\",\n" +
> > >>>>> +            "  \"thrown\" : {\n" +
> > >>>>> +            "    \"commonElementCount\" : 0,\n" +
> > >>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> > >>>>> +            "    \"extendedStackTrace\" : [ {\n" +
> > >>>>> +            "      \"class\" : \"logtest.Main\",\n" +
> > >>>>> +            "      \"method\" : \"main\",\n" +
> > >>>>> +            "      \"file\" : \"Main.java\",\n" +
> > >>>>> +            "      \"line\" : 29,\n" +
> > >>>>> +            "      \"exact\" : true,\n" +
> > >>>>> +            "      \"location\" : \"classes/\",\n" +
> > >>>>> +            "      \"version\" : \"?\"\n" +
> > >>>>> +            "    } ]\n" +
> > >>>>> +            "  },\n" +
> > >>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> > >>>>> +            "  \"endOfBatch\" : false,\n" +
> > >>>>> +            "  \"loggerFqcn\" :
> > >>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> > >>>>> +
> > >>>>> +            "  \"contextMap\" : {\n" +
> > >>>>> +            "    \"bar\" : \"BAR\",\n" +
> > >>>>> +            "    \"foo\" : \"FOO\"\n" +
> > >>>>> +            "  },\n" +
> > >>>>> +            "  \"threadId\" : 1,\n" +
> > >>>>> +            "  \"threadPriority\" : 5,\n" +
> > >>>>> +            "  \"source\" : {\n" +
> > >>>>> +            "    \"class\" : \"logtest.Main\",\n" +
> > >>>>> +            "    \"method\" : \"main\",\n" +
> > >>>>> +            "    \"file\" : \"Main.java\",\n" +
> > >>>>> +            "    \"line\" : 29\n" +
> > >>>>> +            "  }\n" +
> > >>>>> +            "}";
> > >>>>> +
> > >>>>> +    @Before
> > >>>>> +    public void setup() {
> > >>>>> +        parser = new JsonLogEventParser();
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Test
> > >>>>> +    public void testString() throws ParseException {
> > >>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
> > >>>>> +        assertNotNull(logEvent);
> > >>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> > >>>>> +        // TODO assert more here
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Test(expected = ParseException.class)
> > >>>>> +    public void testStringEmpty() throws ParseException {
> > >>>>> +        parser.parseFrom("");
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Test(expected = ParseException.class)
> > >>>>> +    public void testStringInvalidJson() throws ParseException {
> > >>>>> +        parser.parseFrom("foobar");
> > >>>>> +    }
> > >>>>> +
> > >>>>> +    @Test(expected = ParseException.class)
> > >>>>> +    public void testStringInvalidProperty() throws ParseException
> {
> > >>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
> > >>>>> +    }
> > >>>>> +
> > >>>>> +}
> > >>>>>
> > >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> > >>>>> c4814a87/src/changes/changes.xml
> > >>>>> ------------------------------------------------------------
> > ----------
> > >>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> > >>>>> index 074e31c..522f49b 100644
> > >>>>> --- a/src/changes/changes.xml
> > >>>>> +++ b/src/changes/changes.xml
> > >>>>> @@ -31,6 +31,9 @@
> > >>>>>             - "remove" - Removed
> > >>>>>        -->
> > >>>>>        <release version="2.9.0" date="2017-MM-DD" description="GA
> > >>>>> Release
> > >>>>> 2.9.0">
> > >>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> > >>>>> +        Public API for parsing the output from
> > >>>>> JsonLayout/XmlLayout/YamlLayout
> > >>>>> into a LogEvent.
> > >>>>> +      </action>
> > >>>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
> > >>>>>            Allow maxLength of StructuredData to be specified by the
> > >>>>> user.
> > >>>>>          </action>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>
> > >>>
> > >>
> > >
> >
>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>



-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
On Jul 23, 2017 19:10, "Matt Sicker" <bo...@gmail.com> wrote:

Nothing wrong with making more custom exceptions.


This new package already defines a new exception ParseException.

Gary


On 23 July 2017 at 16:46, Gary Gregory <ga...@gmail.com> wrote:

> Also, I'd like to see this used from our socket server classes to make
sure
> it fits nicely with that use case.
>
> Gary
>
> On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
> wrote:
>
> > OK, but RuntimeException should still never be thrown here...
> >
> > Gary
> >
> > On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org>
> wrote:
> >
> >> But that's not quite correct. I want to distinguish between unable to
> >> parse due to malformed input (ParseException), and unable to read from
> >> input (IOException). When reading from String or byte[], unable to read
> >> from input shouldn't happen.
> >>
> >>
> >> On 2017-07-23 21:50, Gary Gregory wrote:
> >>
> >>> You already declare and throw ParseException, so I would use
> >>> ParseException.
> >>>
> >>> Gary
> >>>
> >>>
> >>>
> >>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
> >>> wrote:
> >>>
> >>> Do you have another suggestion?
> >>>>
> >>>>
> >>>> On 2017-07-23 05:01, Gary Gregory wrote:
> >>>>
> >>>> -1 to throw new RuntimeException(e)
> >>>>>
> >>>>> Gary
> >>>>>
> >>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
> >>>>>
> >>>>> Repository: logging-log4j2
> >>>>> Updated Branches:
> >>>>>     refs/heads/LOG4J2-1986 [created] c4814a873
> >>>>>
> >>>>>
> >>>>> LOG4J2-1986 Public API for parsing the output from
> >>>>> JsonLayout/XmlLayout/YamlLayout
> >>>>> into a LogEvent
> >>>>>
> >>>>>
> >>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> >>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> >>>>> commit/c4814a87
> >>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
> >>>>> 4814a87
> >>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
> >>>>> 4814a87
> >>>>>
> >>>>> Branch: refs/heads/LOG4J2-1986
> >>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> >>>>> Parents: eea873e
> >>>>> Author: Mikael Ståldal <mi...@staldal.nu>
> >>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
> >>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
> >>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
> >>>>>
> >>>>> ------------------------------------------------------------
> ----------
> >>>>>    .../parser/AbstractJacksonLogEventParser.java   |  85
> >>>>> +++++++++++++++
> >>>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
> >>>>>    .../log4j/core/parser/LogEventParser.java       |  64
> ++++++++++++
> >>>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
> >>>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
> >>>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
> >>>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
> >>>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
> >>>>>    .../core/parser/JsonLogEventParserTest.java     | 103
> >>>>> +++++++++++++++++++
> >>>>>    src/changes/changes.xml                         |   3 +
> >>>>>    10 files changed, 456 insertions(+)
> >>>>> ------------------------------------------------------------
> ----------
> >>>>>
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> AbstractJacksonLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/
> >>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> >>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
> >>>>> Parser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..8b00e57
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> >>>>> AbstractJacksonLogEventParser.java
> >>>>> @@ -0,0 +1,85 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
> >>>>> +import com.fasterxml.jackson.databind.ObjectReader;
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.InputStream;
> >>>>> +import java.io.Reader;
> >>>>> +
> >>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
> >>>>> +    final ObjectReader objectReader;
> >>>>> +
> >>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> >>>>> +        this.objectReader = objectReader;
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(InputStream input) throws
IOException,
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(String input) throws ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
> >>>>> throws
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input, offset, length);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> JsonLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/JsonLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..218de0b
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParser.java
> >>>>> @@ -0,0 +1,32 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from JsonLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class JsonLogEventParser extends
> AbstractJacksonLogEventParser
> >>>>> {
> >>>>> +
> >>>>> +    public JsonLogEventParser() {
> >>>>> +        super(new Log4jJsonObjectMapper().reader
> >>>>> For(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/LogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/LogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/LogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..88e84fb
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/LogEventParser.java
> >>>>> @@ -0,0 +1,64 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.InputStream;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from a layout into instances of {@link
> LogEvent}.
> >>>>> + */
> >>>>> +public interface LogEventParser {
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from an InputStream. The input stream may contain
> >>>>> multiple
> >>>>> log events.
> >>>>> +     * This method will read one log event and leave the stream
open
> >>>>> and
> >>>>> positioned to read the
> >>>>> +     * next log event.
> >>>>> +     *
> >>>>> +     * @param input  the input stream
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> >>>>> input is
> >>>>> reached.
> >>>>> +     * @throws IOException if unable to read from the input
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
> >>>>> ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a byte array, which is expected to contain
exactly
> >>>>> one
> >>>>> log event.
> >>>>> +     *
> >>>>> +     * @param input  the byte array
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a specified range of a byte array. The specified
> >>>>> range
> >>>>> is expected to contain
> >>>>> +     * exactly one log event.
> >>>>> +     *
> >>>>> +     * @param input  the byte array
> >>>>> +     * @param offset  the initial offset
> >>>>> +     * @param length  the length
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
> >>>>> ParseException;
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/ParseException.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/ParseException.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/ParseException.java
> >>>>> new file mode 100644
> >>>>> index 0000000..8f5fa54
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/ParseException.java
> >>>>> @@ -0,0 +1,34 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +/**
> >>>>> + * Thrown when the input cannot be parsed.
> >>>>> + */
> >>>>> +public class ParseException extends Exception {
> >>>>> +    public ParseException(String message) {
> >>>>> +        super(message);
> >>>>> +    }
> >>>>> +
> >>>>> +    public ParseException(String message, Throwable cause) {
> >>>>> +        super(message, cause);
> >>>>> +    }
> >>>>> +
> >>>>> +    public ParseException(Throwable cause) {
> >>>>> +        super(cause);
> >>>>> +    }
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> TextLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/TextLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/TextLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..3428024
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/TextLogEventParser.java
> >>>>> @@ -0,0 +1,50 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.Reader;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from a text based layout into instances of
> {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public interface TextLogEventParser extends LogEventParser {
> >>>>> +    /**
> >>>>> +     * Parse from a Reader. The reader may contain multiple log
> >>>>> events.
> >>>>> +     * This method will read one log event and leave the reader
open
> >>>>> and
> >>>>> positioned to read the
> >>>>> +     * next log event.
> >>>>> +     *
> >>>>> +     * @param input  the reader
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> >>>>> input is
> >>>>> reached.
> >>>>> +     * @throws IOException if unable to read from the input
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(Reader input) throws IOException,
> >>>>> ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a String, which is expected to contain exactly
one
> >>>>> log
> >>>>> event.
> >>>>> +     *
> >>>>> +     * @param input  the string
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(String input) throws ParseException;
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/XmlLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/XmlLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/XmlLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..597e70b
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/XmlLogEventParser.java
> >>>>> @@ -0,0 +1,32 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from XmlLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class XmlLogEventParser extends
> AbstractJacksonLogEventParser {
> >>>>> +
> >>>>> +    public XmlLogEventParser() {
> >>>>> +        super(new Log4jXmlObjectMapper().readerF
> >>>>> or(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> YamlLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/YamlLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/YamlLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..59b3463
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/YamlLogEventParser.java
> >>>>> @@ -0,0 +1,33 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from YamlLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class YamlLogEventParser extends
> AbstractJacksonLogEventParser
> >>>>> {
> >>>>> +
> >>>>> +    public YamlLogEventParser() {
> >>>>> +        super(new Log4jYamlObjectMapper().reader
> >>>>> For(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/package-info.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/package-info.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/package-info.java
> >>>>> new file mode 100644
> >>>>> index 0000000..3506570
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/package-info.java
> >>>>> @@ -0,0 +1,20 @@
> >>>>> +/*
> >>>>> + * 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.
> >>>>> + */
> >>>>> +/**
> >>>>> + * Parsers for the output of various layouts.
> >>>>> + */
> >>>>> +package org.apache.logging.log4j.core.parser;
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> JsonLogEventParserTest.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> >>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> >>>>> new file mode 100644
> >>>>> index 0000000..5684602
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParserTest.java
> >>>>> @@ -0,0 +1,103 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.junit.Before;
> >>>>> +import org.junit.Test;
> >>>>> +
> >>>>> +import static org.junit.Assert.assertEquals;
> >>>>> +import static org.junit.Assert.assertNotNull;
> >>>>> +
> >>>>> +public class JsonLogEventParserTest {
> >>>>> +
> >>>>> +    private JsonLogEventParser parser;
> >>>>> +
> >>>>> +    private static final String JSON = "{\n" +
> >>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
> >>>>> +            "  \"thread\" : \"main\",\n" +
> >>>>> +            "  \"level\" : \"INFO\",\n" +
> >>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> >>>>> +            "  \"marker\" : {\n" +
> >>>>> +            "    \"name\" : \"child\",\n" +
> >>>>> +            "    \"parents\" : [ {\n" +
> >>>>> +            "      \"name\" : \"parent\",\n" +
> >>>>> +            "      \"parents\" : [ {\n" +
> >>>>> +            "        \"name\" : \"grandparent\"\n" +
> >>>>> +            "      } ]\n" +
> >>>>> +            "    } ]\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"message\" : \"Hello, world!\",\n" +
> >>>>> +            "  \"thrown\" : {\n" +
> >>>>> +            "    \"commonElementCount\" : 0,\n" +
> >>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> >>>>> +            "    \"extendedStackTrace\" : [ {\n" +
> >>>>> +            "      \"class\" : \"logtest.Main\",\n" +
> >>>>> +            "      \"method\" : \"main\",\n" +
> >>>>> +            "      \"file\" : \"Main.java\",\n" +
> >>>>> +            "      \"line\" : 29,\n" +
> >>>>> +            "      \"exact\" : true,\n" +
> >>>>> +            "      \"location\" : \"classes/\",\n" +
> >>>>> +            "      \"version\" : \"?\"\n" +
> >>>>> +            "    } ]\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> >>>>> +            "  \"endOfBatch\" : false,\n" +
> >>>>> +            "  \"loggerFqcn\" :
> >>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> >>>>> +
> >>>>> +            "  \"contextMap\" : {\n" +
> >>>>> +            "    \"bar\" : \"BAR\",\n" +
> >>>>> +            "    \"foo\" : \"FOO\"\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"threadId\" : 1,\n" +
> >>>>> +            "  \"threadPriority\" : 5,\n" +
> >>>>> +            "  \"source\" : {\n" +
> >>>>> +            "    \"class\" : \"logtest.Main\",\n" +
> >>>>> +            "    \"method\" : \"main\",\n" +
> >>>>> +            "    \"file\" : \"Main.java\",\n" +
> >>>>> +            "    \"line\" : 29\n" +
> >>>>> +            "  }\n" +
> >>>>> +            "}";
> >>>>> +
> >>>>> +    @Before
> >>>>> +    public void setup() {
> >>>>> +        parser = new JsonLogEventParser();
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test
> >>>>> +    public void testString() throws ParseException {
> >>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
> >>>>> +        assertNotNull(logEvent);
> >>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> >>>>> +        // TODO assert more here
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringEmpty() throws ParseException {
> >>>>> +        parser.parseFrom("");
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringInvalidJson() throws ParseException {
> >>>>> +        parser.parseFrom("foobar");
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringInvalidProperty() throws ParseException {
> >>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/src/changes/changes.xml
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> >>>>> index 074e31c..522f49b 100644
> >>>>> --- a/src/changes/changes.xml
> >>>>> +++ b/src/changes/changes.xml
> >>>>> @@ -31,6 +31,9 @@
> >>>>>             - "remove" - Removed
> >>>>>        -->
> >>>>>        <release version="2.9.0" date="2017-MM-DD" description="GA
> >>>>> Release
> >>>>> 2.9.0">
> >>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> >>>>> +        Public API for parsing the output from
> >>>>> JsonLayout/XmlLayout/YamlLayout
> >>>>> into a LogEvent.
> >>>>> +      </action>
> >>>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
> >>>>>            Allow maxLength of StructuredData to be specified by the
> >>>>> user.
> >>>>>          </action>
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>
> >>
> >
>



--
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Matt Sicker <bo...@gmail.com>.
Nothing wrong with making more custom exceptions.

On 23 July 2017 at 16:46, Gary Gregory <ga...@gmail.com> wrote:

> Also, I'd like to see this used from our socket server classes to make sure
> it fits nicely with that use case.
>
> Gary
>
> On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
> wrote:
>
> > OK, but RuntimeException should still never be thrown here...
> >
> > Gary
> >
> > On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org>
> wrote:
> >
> >> But that's not quite correct. I want to distinguish between unable to
> >> parse due to malformed input (ParseException), and unable to read from
> >> input (IOException). When reading from String or byte[], unable to read
> >> from input shouldn't happen.
> >>
> >>
> >> On 2017-07-23 21:50, Gary Gregory wrote:
> >>
> >>> You already declare and throw ParseException, so I would use
> >>> ParseException.
> >>>
> >>> Gary
> >>>
> >>>
> >>>
> >>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
> >>> wrote:
> >>>
> >>> Do you have another suggestion?
> >>>>
> >>>>
> >>>> On 2017-07-23 05:01, Gary Gregory wrote:
> >>>>
> >>>> -1 to throw new RuntimeException(e)
> >>>>>
> >>>>> Gary
> >>>>>
> >>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
> >>>>>
> >>>>> Repository: logging-log4j2
> >>>>> Updated Branches:
> >>>>>     refs/heads/LOG4J2-1986 [created] c4814a873
> >>>>>
> >>>>>
> >>>>> LOG4J2-1986 Public API for parsing the output from
> >>>>> JsonLayout/XmlLayout/YamlLayout
> >>>>> into a LogEvent
> >>>>>
> >>>>>
> >>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> >>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> >>>>> commit/c4814a87
> >>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
> >>>>> 4814a87
> >>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
> >>>>> 4814a87
> >>>>>
> >>>>> Branch: refs/heads/LOG4J2-1986
> >>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> >>>>> Parents: eea873e
> >>>>> Author: Mikael Ståldal <mi...@staldal.nu>
> >>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
> >>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
> >>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
> >>>>>
> >>>>> ------------------------------------------------------------
> ----------
> >>>>>    .../parser/AbstractJacksonLogEventParser.java   |  85
> >>>>> +++++++++++++++
> >>>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
> >>>>>    .../log4j/core/parser/LogEventParser.java       |  64
> ++++++++++++
> >>>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
> >>>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
> >>>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
> >>>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
> >>>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
> >>>>>    .../core/parser/JsonLogEventParserTest.java     | 103
> >>>>> +++++++++++++++++++
> >>>>>    src/changes/changes.xml                         |   3 +
> >>>>>    10 files changed, 456 insertions(+)
> >>>>> ------------------------------------------------------------
> ----------
> >>>>>
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> AbstractJacksonLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/
> >>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> >>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
> >>>>> Parser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..8b00e57
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> >>>>> AbstractJacksonLogEventParser.java
> >>>>> @@ -0,0 +1,85 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
> >>>>> +import com.fasterxml.jackson.databind.ObjectReader;
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.InputStream;
> >>>>> +import java.io.Reader;
> >>>>> +
> >>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
> >>>>> +    final ObjectReader objectReader;
> >>>>> +
> >>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> >>>>> +        this.objectReader = objectReader;
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(String input) throws ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +
> >>>>> +    @Override
> >>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
> >>>>> throws
> >>>>> ParseException {
> >>>>> +        try {
> >>>>> +            return objectReader.readValue(input, offset, length);
> >>>>> +        } catch (JsonProcessingException e) {
> >>>>> +            throw new ParseException(e);
> >>>>> +        } catch (IOException e) {
> >>>>> +            throw new RuntimeException(e);
> >>>>> +        }
> >>>>> +    }
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> JsonLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/JsonLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..218de0b
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParser.java
> >>>>> @@ -0,0 +1,32 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from JsonLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class JsonLogEventParser extends
> AbstractJacksonLogEventParser
> >>>>> {
> >>>>> +
> >>>>> +    public JsonLogEventParser() {
> >>>>> +        super(new Log4jJsonObjectMapper().reader
> >>>>> For(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/LogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/LogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/LogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..88e84fb
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/LogEventParser.java
> >>>>> @@ -0,0 +1,64 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.InputStream;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from a layout into instances of {@link
> LogEvent}.
> >>>>> + */
> >>>>> +public interface LogEventParser {
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from an InputStream. The input stream may contain
> >>>>> multiple
> >>>>> log events.
> >>>>> +     * This method will read one log event and leave the stream open
> >>>>> and
> >>>>> positioned to read the
> >>>>> +     * next log event.
> >>>>> +     *
> >>>>> +     * @param input  the input stream
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> >>>>> input is
> >>>>> reached.
> >>>>> +     * @throws IOException if unable to read from the input
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
> >>>>> ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a byte array, which is expected to contain exactly
> >>>>> one
> >>>>> log event.
> >>>>> +     *
> >>>>> +     * @param input  the byte array
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a specified range of a byte array. The specified
> >>>>> range
> >>>>> is expected to contain
> >>>>> +     * exactly one log event.
> >>>>> +     *
> >>>>> +     * @param input  the byte array
> >>>>> +     * @param offset  the initial offset
> >>>>> +     * @param length  the length
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, never {@literal null}.
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
> >>>>> ParseException;
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/ParseException.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/ParseException.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/ParseException.java
> >>>>> new file mode 100644
> >>>>> index 0000000..8f5fa54
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/ParseException.java
> >>>>> @@ -0,0 +1,34 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +/**
> >>>>> + * Thrown when the input cannot be parsed.
> >>>>> + */
> >>>>> +public class ParseException extends Exception {
> >>>>> +    public ParseException(String message) {
> >>>>> +        super(message);
> >>>>> +    }
> >>>>> +
> >>>>> +    public ParseException(String message, Throwable cause) {
> >>>>> +        super(message, cause);
> >>>>> +    }
> >>>>> +
> >>>>> +    public ParseException(Throwable cause) {
> >>>>> +        super(cause);
> >>>>> +    }
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> TextLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/TextLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/TextLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..3428024
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/TextLogEventParser.java
> >>>>> @@ -0,0 +1,50 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +
> >>>>> +import java.io.IOException;
> >>>>> +import java.io.Reader;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from a text based layout into instances of
> {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public interface TextLogEventParser extends LogEventParser {
> >>>>> +    /**
> >>>>> +     * Parse from a Reader. The reader may contain multiple log
> >>>>> events.
> >>>>> +     * This method will read one log event and leave the reader open
> >>>>> and
> >>>>> positioned to read the
> >>>>> +     * next log event.
> >>>>> +     *
> >>>>> +     * @param input  the reader
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
> >>>>> input is
> >>>>> reached.
> >>>>> +     * @throws IOException if unable to read from the input
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(Reader input) throws IOException,
> >>>>> ParseException;
> >>>>> +
> >>>>> +    /**
> >>>>> +     * Parse from a String, which is expected to contain exactly one
> >>>>> log
> >>>>> event.
> >>>>> +     *
> >>>>> +     * @param input  the string
> >>>>> +     *
> >>>>> +     * @return the parsed LogEvent
> >>>>> +     * @throws ParseException if the input is malformed and cannot
> be
> >>>>> parsed as a LogEvent
> >>>>> +     */
> >>>>> +    LogEvent parseFrom(String input) throws ParseException;
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/XmlLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/XmlLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/XmlLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..597e70b
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/XmlLogEventParser.java
> >>>>> @@ -0,0 +1,32 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from XmlLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class XmlLogEventParser extends
> AbstractJacksonLogEventParser {
> >>>>> +
> >>>>> +    public XmlLogEventParser() {
> >>>>> +        super(new Log4jXmlObjectMapper().readerF
> >>>>> or(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> YamlLogEventParser.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/YamlLogEventParser.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/YamlLogEventParser.java
> >>>>> new file mode 100644
> >>>>> index 0000000..59b3463
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/YamlLogEventParser.java
> >>>>> @@ -0,0 +1,33 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> >>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
> >>>>> +
> >>>>> +/**
> >>>>> + * Parse the output from YamlLayout layout into instances of {@link
> >>>>> LogEvent}.
> >>>>> + */
> >>>>> +public class YamlLogEventParser extends
> AbstractJacksonLogEventParser
> >>>>> {
> >>>>> +
> >>>>> +    public YamlLogEventParser() {
> >>>>> +        super(new Log4jYamlObjectMapper().reader
> >>>>> For(Log4jLogEvent.class));
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> >>>>> core/parser/package-info.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/main/java/org
> >>>>> /apache/logging/log4j/core/par
> >>>>> ser/package-info.java
> >>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/package-info.java
> >>>>> new file mode 100644
> >>>>> index 0000000..3506570
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> >>>>> parser/package-info.java
> >>>>> @@ -0,0 +1,20 @@
> >>>>> +/*
> >>>>> + * 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.
> >>>>> + */
> >>>>> +/**
> >>>>> + * Parsers for the output of various layouts.
> >>>>> + */
> >>>>> +package org.apache.logging.log4j.core.parser;
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
> >>>>> ore/parser/
> >>>>> JsonLogEventParserTest.java
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> >>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> >>>>> new file mode 100644
> >>>>> index 0000000..5684602
> >>>>> --- /dev/null
> >>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> >>>>> parser/JsonLogEventParserTest.java
> >>>>> @@ -0,0 +1,103 @@
> >>>>> +/*
> >>>>> + * 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.parser;
> >>>>> +
> >>>>> +import org.apache.logging.log4j.core.LogEvent;
> >>>>> +import org.junit.Before;
> >>>>> +import org.junit.Test;
> >>>>> +
> >>>>> +import static org.junit.Assert.assertEquals;
> >>>>> +import static org.junit.Assert.assertNotNull;
> >>>>> +
> >>>>> +public class JsonLogEventParserTest {
> >>>>> +
> >>>>> +    private JsonLogEventParser parser;
> >>>>> +
> >>>>> +    private static final String JSON = "{\n" +
> >>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
> >>>>> +            "  \"thread\" : \"main\",\n" +
> >>>>> +            "  \"level\" : \"INFO\",\n" +
> >>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> >>>>> +            "  \"marker\" : {\n" +
> >>>>> +            "    \"name\" : \"child\",\n" +
> >>>>> +            "    \"parents\" : [ {\n" +
> >>>>> +            "      \"name\" : \"parent\",\n" +
> >>>>> +            "      \"parents\" : [ {\n" +
> >>>>> +            "        \"name\" : \"grandparent\"\n" +
> >>>>> +            "      } ]\n" +
> >>>>> +            "    } ]\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"message\" : \"Hello, world!\",\n" +
> >>>>> +            "  \"thrown\" : {\n" +
> >>>>> +            "    \"commonElementCount\" : 0,\n" +
> >>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> >>>>> +            "    \"extendedStackTrace\" : [ {\n" +
> >>>>> +            "      \"class\" : \"logtest.Main\",\n" +
> >>>>> +            "      \"method\" : \"main\",\n" +
> >>>>> +            "      \"file\" : \"Main.java\",\n" +
> >>>>> +            "      \"line\" : 29,\n" +
> >>>>> +            "      \"exact\" : true,\n" +
> >>>>> +            "      \"location\" : \"classes/\",\n" +
> >>>>> +            "      \"version\" : \"?\"\n" +
> >>>>> +            "    } ]\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> >>>>> +            "  \"endOfBatch\" : false,\n" +
> >>>>> +            "  \"loggerFqcn\" :
> >>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> >>>>> +
> >>>>> +            "  \"contextMap\" : {\n" +
> >>>>> +            "    \"bar\" : \"BAR\",\n" +
> >>>>> +            "    \"foo\" : \"FOO\"\n" +
> >>>>> +            "  },\n" +
> >>>>> +            "  \"threadId\" : 1,\n" +
> >>>>> +            "  \"threadPriority\" : 5,\n" +
> >>>>> +            "  \"source\" : {\n" +
> >>>>> +            "    \"class\" : \"logtest.Main\",\n" +
> >>>>> +            "    \"method\" : \"main\",\n" +
> >>>>> +            "    \"file\" : \"Main.java\",\n" +
> >>>>> +            "    \"line\" : 29\n" +
> >>>>> +            "  }\n" +
> >>>>> +            "}";
> >>>>> +
> >>>>> +    @Before
> >>>>> +    public void setup() {
> >>>>> +        parser = new JsonLogEventParser();
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test
> >>>>> +    public void testString() throws ParseException {
> >>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
> >>>>> +        assertNotNull(logEvent);
> >>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> >>>>> +        // TODO assert more here
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringEmpty() throws ParseException {
> >>>>> +        parser.parseFrom("");
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringInvalidJson() throws ParseException {
> >>>>> +        parser.parseFrom("foobar");
> >>>>> +    }
> >>>>> +
> >>>>> +    @Test(expected = ParseException.class)
> >>>>> +    public void testStringInvalidProperty() throws ParseException {
> >>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
> >>>>> +    }
> >>>>> +
> >>>>> +}
> >>>>>
> >>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> >>>>> c4814a87/src/changes/changes.xml
> >>>>> ------------------------------------------------------------
> ----------
> >>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> >>>>> index 074e31c..522f49b 100644
> >>>>> --- a/src/changes/changes.xml
> >>>>> +++ b/src/changes/changes.xml
> >>>>> @@ -31,6 +31,9 @@
> >>>>>             - "remove" - Removed
> >>>>>        -->
> >>>>>        <release version="2.9.0" date="2017-MM-DD" description="GA
> >>>>> Release
> >>>>> 2.9.0">
> >>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> >>>>> +        Public API for parsing the output from
> >>>>> JsonLayout/XmlLayout/YamlLayout
> >>>>> into a LogEvent.
> >>>>> +      </action>
> >>>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
> >>>>>            Allow maxLength of StructuredData to be specified by the
> >>>>> user.
> >>>>>          </action>
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>
> >>
> >
>



-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
I have updated the socket server in the log4-server module in the 
log4j-tools repo to use 2.9-SNAPSHOT, and to use this.

While doing so, I discovered that the log4j-server module (JmsServer 
class) used JmsManager in log4j-core, which you have changed in a 
non-backwards compatible way. I did some quick fix to get it to compile, 
but I am not sure that I used JmsManager properly. Maybe you can have a 
look at it?


On 2017-07-23 23:46, Gary Gregory wrote:
> Also, I'd like to see this used from our socket server classes to make sure
> it fits nicely with that use case.
> 
> Gary
> 
> On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
> wrote:
> 
>> OK, but RuntimeException should still never be thrown here...
>>
>> Gary
>>
>> On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org> wrote:
>>
>>> But that's not quite correct. I want to distinguish between unable to
>>> parse due to malformed input (ParseException), and unable to read from
>>> input (IOException). When reading from String or byte[], unable to read
>>> from input shouldn't happen.
>>>
>>>
>>> On 2017-07-23 21:50, Gary Gregory wrote:
>>>
>>>> You already declare and throw ParseException, so I would use
>>>> ParseException.
>>>>
>>>> Gary
>>>>
>>>>
>>>>
>>>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
>>>> wrote:
>>>>
>>>> Do you have another suggestion?
>>>>>
>>>>>
>>>>> On 2017-07-23 05:01, Gary Gregory wrote:
>>>>>
>>>>> -1 to throw new RuntimeException(e)
>>>>>>
>>>>>> Gary
>>>>>>
>>>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>>>>
>>>>>> Repository: logging-log4j2
>>>>>> Updated Branches:
>>>>>>      refs/heads/LOG4J2-1986 [created] c4814a873
>>>>>>
>>>>>>
>>>>>> LOG4J2-1986 Public API for parsing the output from
>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>> into a LogEvent
>>>>>>
>>>>>>
>>>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>>>>> commit/c4814a87
>>>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
>>>>>> 4814a87
>>>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
>>>>>> 4814a87
>>>>>>
>>>>>> Branch: refs/heads/LOG4J2-1986
>>>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>>>>> Parents: eea873e
>>>>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>>>>
>>>>>> ----------------------------------------------------------------------
>>>>>>     .../parser/AbstractJacksonLogEventParser.java   |  85
>>>>>> +++++++++++++++
>>>>>>     .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>>>>     .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>>>>>     .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>>>>     .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>>>>     .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>>>>     .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>>>>     .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>>>>     .../core/parser/JsonLogEventParserTest.java     | 103
>>>>>> +++++++++++++++++++
>>>>>>     src/changes/changes.xml                         |   3 +
>>>>>>     10 files changed, 456 insertions(+)
>>>>>> ----------------------------------------------------------------------
>>>>>>
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> AbstractJacksonLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/
>>>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
>>>>>> Parser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..8b00e57
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>>>> AbstractJacksonLogEventParser.java
>>>>>> @@ -0,0 +1,85 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>>>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.InputStream;
>>>>>> +import java.io.Reader;
>>>>>> +
>>>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>>>>>> +    final ObjectReader objectReader;
>>>>>> +
>>>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>>>>> +        this.objectReader = objectReader;
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(String input) throws ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +
>>>>>> +    @Override
>>>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>>>>>> throws
>>>>>> ParseException {
>>>>>> +        try {
>>>>>> +            return objectReader.readValue(input, offset, length);
>>>>>> +        } catch (JsonProcessingException e) {
>>>>>> +            throw new ParseException(e);
>>>>>> +        } catch (IOException e) {
>>>>>> +            throw new RuntimeException(e);
>>>>>> +        }
>>>>>> +    }
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> JsonLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/JsonLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..218de0b
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParser.java
>>>>>> @@ -0,0 +1,32 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from JsonLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser
>>>>>> {
>>>>>> +
>>>>>> +    public JsonLogEventParser() {
>>>>>> +        super(new Log4jJsonObjectMapper().reader
>>>>>> For(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/LogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/LogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/LogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..88e84fb
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/LogEventParser.java
>>>>>> @@ -0,0 +1,64 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.InputStream;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from a layout into instances of {@link LogEvent}.
>>>>>> + */
>>>>>> +public interface LogEventParser {
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from an InputStream. The input stream may contain
>>>>>> multiple
>>>>>> log events.
>>>>>> +     * This method will read one log event and leave the stream open
>>>>>> and
>>>>>> positioned to read the
>>>>>> +     * next log event.
>>>>>> +     *
>>>>>> +     * @param input  the input stream
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>> input is
>>>>>> reached.
>>>>>> +     * @throws IOException if unable to read from the input
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>>>>> ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a byte array, which is expected to contain exactly
>>>>>> one
>>>>>> log event.
>>>>>> +     *
>>>>>> +     * @param input  the byte array
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a specified range of a byte array. The specified
>>>>>> range
>>>>>> is expected to contain
>>>>>> +     * exactly one log event.
>>>>>> +     *
>>>>>> +     * @param input  the byte array
>>>>>> +     * @param offset  the initial offset
>>>>>> +     * @param length  the length
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>>>>>> ParseException;
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/ParseException.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/ParseException.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/ParseException.java
>>>>>> new file mode 100644
>>>>>> index 0000000..8f5fa54
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/ParseException.java
>>>>>> @@ -0,0 +1,34 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +/**
>>>>>> + * Thrown when the input cannot be parsed.
>>>>>> + */
>>>>>> +public class ParseException extends Exception {
>>>>>> +    public ParseException(String message) {
>>>>>> +        super(message);
>>>>>> +    }
>>>>>> +
>>>>>> +    public ParseException(String message, Throwable cause) {
>>>>>> +        super(message, cause);
>>>>>> +    }
>>>>>> +
>>>>>> +    public ParseException(Throwable cause) {
>>>>>> +        super(cause);
>>>>>> +    }
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> TextLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/TextLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/TextLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..3428024
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/TextLogEventParser.java
>>>>>> @@ -0,0 +1,50 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +
>>>>>> +import java.io.IOException;
>>>>>> +import java.io.Reader;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from a text based layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public interface TextLogEventParser extends LogEventParser {
>>>>>> +    /**
>>>>>> +     * Parse from a Reader. The reader may contain multiple log
>>>>>> events.
>>>>>> +     * This method will read one log event and leave the reader open
>>>>>> and
>>>>>> positioned to read the
>>>>>> +     * next log event.
>>>>>> +     *
>>>>>> +     * @param input  the reader
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>>> input is
>>>>>> reached.
>>>>>> +     * @throws IOException if unable to read from the input
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(Reader input) throws IOException,
>>>>>> ParseException;
>>>>>> +
>>>>>> +    /**
>>>>>> +     * Parse from a String, which is expected to contain exactly one
>>>>>> log
>>>>>> event.
>>>>>> +     *
>>>>>> +     * @param input  the string
>>>>>> +     *
>>>>>> +     * @return the parsed LogEvent
>>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>>> parsed as a LogEvent
>>>>>> +     */
>>>>>> +    LogEvent parseFrom(String input) throws ParseException;
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/XmlLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/XmlLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/XmlLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..597e70b
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/XmlLogEventParser.java
>>>>>> @@ -0,0 +1,32 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from XmlLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>>>>>> +
>>>>>> +    public XmlLogEventParser() {
>>>>>> +        super(new Log4jXmlObjectMapper().readerF
>>>>>> or(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> YamlLogEventParser.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/YamlLogEventParser.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/YamlLogEventParser.java
>>>>>> new file mode 100644
>>>>>> index 0000000..59b3463
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/YamlLogEventParser.java
>>>>>> @@ -0,0 +1,33 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>>>>>> +
>>>>>> +/**
>>>>>> + * Parse the output from YamlLayout layout into instances of {@link
>>>>>> LogEvent}.
>>>>>> + */
>>>>>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser
>>>>>> {
>>>>>> +
>>>>>> +    public YamlLogEventParser() {
>>>>>> +        super(new Log4jYamlObjectMapper().reader
>>>>>> For(Log4jLogEvent.class));
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>>> core/parser/package-info.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/par
>>>>>> ser/package-info.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/package-info.java
>>>>>> new file mode 100644
>>>>>> index 0000000..3506570
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>>> parser/package-info.java
>>>>>> @@ -0,0 +1,20 @@
>>>>>> +/*
>>>>>> + * 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.
>>>>>> + */
>>>>>> +/**
>>>>>> + * Parsers for the output of various layouts.
>>>>>> + */
>>>>>> +package org.apache.logging.log4j.core.parser;
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
>>>>>> ore/parser/
>>>>>> JsonLogEventParserTest.java
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>>>>> new file mode 100644
>>>>>> index 0000000..5684602
>>>>>> --- /dev/null
>>>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>>> parser/JsonLogEventParserTest.java
>>>>>> @@ -0,0 +1,103 @@
>>>>>> +/*
>>>>>> + * 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.parser;
>>>>>> +
>>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>>> +import org.junit.Before;
>>>>>> +import org.junit.Test;
>>>>>> +
>>>>>> +import static org.junit.Assert.assertEquals;
>>>>>> +import static org.junit.Assert.assertNotNull;
>>>>>> +
>>>>>> +public class JsonLogEventParserTest {
>>>>>> +
>>>>>> +    private JsonLogEventParser parser;
>>>>>> +
>>>>>> +    private static final String JSON = "{\n" +
>>>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>>>>> +            "  \"thread\" : \"main\",\n" +
>>>>>> +            "  \"level\" : \"INFO\",\n" +
>>>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>>>>> +            "  \"marker\" : {\n" +
>>>>>> +            "    \"name\" : \"child\",\n" +
>>>>>> +            "    \"parents\" : [ {\n" +
>>>>>> +            "      \"name\" : \"parent\",\n" +
>>>>>> +            "      \"parents\" : [ {\n" +
>>>>>> +            "        \"name\" : \"grandparent\"\n" +
>>>>>> +            "      } ]\n" +
>>>>>> +            "    } ]\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>>>>> +            "  \"thrown\" : {\n" +
>>>>>> +            "    \"commonElementCount\" : 0,\n" +
>>>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>>>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>>>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>>>>> +            "      \"method\" : \"main\",\n" +
>>>>>> +            "      \"file\" : \"Main.java\",\n" +
>>>>>> +            "      \"line\" : 29,\n" +
>>>>>> +            "      \"exact\" : true,\n" +
>>>>>> +            "      \"location\" : \"classes/\",\n" +
>>>>>> +            "      \"version\" : \"?\"\n" +
>>>>>> +            "    } ]\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>>>>> +            "  \"endOfBatch\" : false,\n" +
>>>>>> +            "  \"loggerFqcn\" :
>>>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>>>>> +
>>>>>> +            "  \"contextMap\" : {\n" +
>>>>>> +            "    \"bar\" : \"BAR\",\n" +
>>>>>> +            "    \"foo\" : \"FOO\"\n" +
>>>>>> +            "  },\n" +
>>>>>> +            "  \"threadId\" : 1,\n" +
>>>>>> +            "  \"threadPriority\" : 5,\n" +
>>>>>> +            "  \"source\" : {\n" +
>>>>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>>>>> +            "    \"method\" : \"main\",\n" +
>>>>>> +            "    \"file\" : \"Main.java\",\n" +
>>>>>> +            "    \"line\" : 29\n" +
>>>>>> +            "  }\n" +
>>>>>> +            "}";
>>>>>> +
>>>>>> +    @Before
>>>>>> +    public void setup() {
>>>>>> +        parser = new JsonLogEventParser();
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test
>>>>>> +    public void testString() throws ParseException {
>>>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>>>>> +        assertNotNull(logEvent);
>>>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>>>>> +        // TODO assert more here
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringEmpty() throws ParseException {
>>>>>> +        parser.parseFrom("");
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringInvalidJson() throws ParseException {
>>>>>> +        parser.parseFrom("foobar");
>>>>>> +    }
>>>>>> +
>>>>>> +    @Test(expected = ParseException.class)
>>>>>> +    public void testStringInvalidProperty() throws ParseException {
>>>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>>>>> +    }
>>>>>> +
>>>>>> +}
>>>>>>
>>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>>> c4814a87/src/changes/changes.xml
>>>>>> ----------------------------------------------------------------------
>>>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>>> index 074e31c..522f49b 100644
>>>>>> --- a/src/changes/changes.xml
>>>>>> +++ b/src/changes/changes.xml
>>>>>> @@ -31,6 +31,9 @@
>>>>>>              - "remove" - Removed
>>>>>>         -->
>>>>>>         <release version="2.9.0" date="2017-MM-DD" description="GA
>>>>>> Release
>>>>>> 2.9.0">
>>>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>>>>> +        Public API for parsing the output from
>>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>>> into a LogEvent.
>>>>>> +      </action>
>>>>>>           <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>>>>             Allow maxLength of StructuredData to be specified by the
>>>>>> user.
>>>>>>           </action>
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
Also, I'd like to see this used from our socket server classes to make sure
it fits nicely with that use case.

Gary

On Sun, Jul 23, 2017 at 1:05 PM, Gary Gregory <ga...@gmail.com>
wrote:

> OK, but RuntimeException should still never be thrown here...
>
> Gary
>
> On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org> wrote:
>
>> But that's not quite correct. I want to distinguish between unable to
>> parse due to malformed input (ParseException), and unable to read from
>> input (IOException). When reading from String or byte[], unable to read
>> from input shouldn't happen.
>>
>>
>> On 2017-07-23 21:50, Gary Gregory wrote:
>>
>>> You already declare and throw ParseException, so I would use
>>> ParseException.
>>>
>>> Gary
>>>
>>>
>>>
>>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
>>> wrote:
>>>
>>> Do you have another suggestion?
>>>>
>>>>
>>>> On 2017-07-23 05:01, Gary Gregory wrote:
>>>>
>>>> -1 to throw new RuntimeException(e)
>>>>>
>>>>> Gary
>>>>>
>>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>>>
>>>>> Repository: logging-log4j2
>>>>> Updated Branches:
>>>>>     refs/heads/LOG4J2-1986 [created] c4814a873
>>>>>
>>>>>
>>>>> LOG4J2-1986 Public API for parsing the output from
>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>> into a LogEvent
>>>>>
>>>>>
>>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>>>> commit/c4814a87
>>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
>>>>> 4814a87
>>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
>>>>> 4814a87
>>>>>
>>>>> Branch: refs/heads/LOG4J2-1986
>>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>>>> Parents: eea873e
>>>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>>>
>>>>> ----------------------------------------------------------------------
>>>>>    .../parser/AbstractJacksonLogEventParser.java   |  85
>>>>> +++++++++++++++
>>>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>>>    .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>>>    .../core/parser/JsonLogEventParserTest.java     | 103
>>>>> +++++++++++++++++++
>>>>>    src/changes/changes.xml                         |   3 +
>>>>>    10 files changed, 456 insertions(+)
>>>>> ----------------------------------------------------------------------
>>>>>
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>> ore/parser/
>>>>> AbstractJacksonLogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/
>>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEvent
>>>>> Parser.java
>>>>> new file mode 100644
>>>>> index 0000000..8b00e57
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>>> AbstractJacksonLogEventParser.java
>>>>> @@ -0,0 +1,85 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>> +
>>>>> +import java.io.IOException;
>>>>> +import java.io.InputStream;
>>>>> +import java.io.Reader;
>>>>> +
>>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>>>>> +    final ObjectReader objectReader;
>>>>> +
>>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>>>> +        this.objectReader = objectReader;
>>>>> +    }
>>>>> +
>>>>> +    @Override
>>>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>>>>> ParseException {
>>>>> +        try {
>>>>> +            return objectReader.readValue(input);
>>>>> +        } catch (JsonProcessingException e) {
>>>>> +            throw new ParseException(e);
>>>>> +        }
>>>>> +    }
>>>>> +
>>>>> +    @Override
>>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>>>> ParseException {
>>>>> +        try {
>>>>> +            return objectReader.readValue(input);
>>>>> +        } catch (JsonProcessingException e) {
>>>>> +            throw new ParseException(e);
>>>>> +        }
>>>>> +    }
>>>>> +
>>>>> +    @Override
>>>>> +    public LogEvent parseFrom(String input) throws ParseException {
>>>>> +        try {
>>>>> +            return objectReader.readValue(input);
>>>>> +        } catch (JsonProcessingException e) {
>>>>> +            throw new ParseException(e);
>>>>> +        } catch (IOException e) {
>>>>> +            throw new RuntimeException(e);
>>>>> +        }
>>>>> +    }
>>>>> +
>>>>> +    @Override
>>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>>>>> +        try {
>>>>> +            return objectReader.readValue(input);
>>>>> +        } catch (JsonProcessingException e) {
>>>>> +            throw new ParseException(e);
>>>>> +        } catch (IOException e) {
>>>>> +            throw new RuntimeException(e);
>>>>> +        }
>>>>> +    }
>>>>> +
>>>>> +    @Override
>>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>>>>> throws
>>>>> ParseException {
>>>>> +        try {
>>>>> +            return objectReader.readValue(input, offset, length);
>>>>> +        } catch (JsonProcessingException e) {
>>>>> +            throw new ParseException(e);
>>>>> +        } catch (IOException e) {
>>>>> +            throw new RuntimeException(e);
>>>>> +        }
>>>>> +    }
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>> ore/parser/
>>>>> JsonLogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/JsonLogEventParser.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/JsonLogEventParser.java
>>>>> new file mode 100644
>>>>> index 0000000..218de0b
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/JsonLogEventParser.java
>>>>> @@ -0,0 +1,32 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>>> +
>>>>> +/**
>>>>> + * Parse the output from JsonLayout layout into instances of {@link
>>>>> LogEvent}.
>>>>> + */
>>>>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser
>>>>> {
>>>>> +
>>>>> +    public JsonLogEventParser() {
>>>>> +        super(new Log4jJsonObjectMapper().reader
>>>>> For(Log4jLogEvent.class));
>>>>> +    }
>>>>> +
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>> core/parser/LogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/LogEventParser.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/LogEventParser.java
>>>>> new file mode 100644
>>>>> index 0000000..88e84fb
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/LogEventParser.java
>>>>> @@ -0,0 +1,64 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +
>>>>> +import java.io.IOException;
>>>>> +import java.io.InputStream;
>>>>> +
>>>>> +/**
>>>>> + * Parse the output from a layout into instances of {@link LogEvent}.
>>>>> + */
>>>>> +public interface LogEventParser {
>>>>> +
>>>>> +    /**
>>>>> +     * Parse from an InputStream. The input stream may contain
>>>>> multiple
>>>>> log events.
>>>>> +     * This method will read one log event and leave the stream open
>>>>> and
>>>>> positioned to read the
>>>>> +     * next log event.
>>>>> +     *
>>>>> +     * @param input  the input stream
>>>>> +     *
>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>> input is
>>>>> reached.
>>>>> +     * @throws IOException if unable to read from the input
>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>> parsed as a LogEvent
>>>>> +     */
>>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>>>> ParseException;
>>>>> +
>>>>> +    /**
>>>>> +     * Parse from a byte array, which is expected to contain exactly
>>>>> one
>>>>> log event.
>>>>> +     *
>>>>> +     * @param input  the byte array
>>>>> +     *
>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>> parsed as a LogEvent
>>>>> +     */
>>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>>>> +
>>>>> +    /**
>>>>> +     * Parse from a specified range of a byte array. The specified
>>>>> range
>>>>> is expected to contain
>>>>> +     * exactly one log event.
>>>>> +     *
>>>>> +     * @param input  the byte array
>>>>> +     * @param offset  the initial offset
>>>>> +     * @param length  the length
>>>>> +     *
>>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>> parsed as a LogEvent
>>>>> +     */
>>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>>>>> ParseException;
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>> core/parser/ParseException.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/ParseException.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/ParseException.java
>>>>> new file mode 100644
>>>>> index 0000000..8f5fa54
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/ParseException.java
>>>>> @@ -0,0 +1,34 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +/**
>>>>> + * Thrown when the input cannot be parsed.
>>>>> + */
>>>>> +public class ParseException extends Exception {
>>>>> +    public ParseException(String message) {
>>>>> +        super(message);
>>>>> +    }
>>>>> +
>>>>> +    public ParseException(String message, Throwable cause) {
>>>>> +        super(message, cause);
>>>>> +    }
>>>>> +
>>>>> +    public ParseException(Throwable cause) {
>>>>> +        super(cause);
>>>>> +    }
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>> ore/parser/
>>>>> TextLogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/TextLogEventParser.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/TextLogEventParser.java
>>>>> new file mode 100644
>>>>> index 0000000..3428024
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/TextLogEventParser.java
>>>>> @@ -0,0 +1,50 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +
>>>>> +import java.io.IOException;
>>>>> +import java.io.Reader;
>>>>> +
>>>>> +/**
>>>>> + * Parse the output from a text based layout into instances of {@link
>>>>> LogEvent}.
>>>>> + */
>>>>> +public interface TextLogEventParser extends LogEventParser {
>>>>> +    /**
>>>>> +     * Parse from a Reader. The reader may contain multiple log
>>>>> events.
>>>>> +     * This method will read one log event and leave the reader open
>>>>> and
>>>>> positioned to read the
>>>>> +     * next log event.
>>>>> +     *
>>>>> +     * @param input  the reader
>>>>> +     *
>>>>> +     * @return the parsed LogEvent, or {@literal null} of end of
>>>>> input is
>>>>> reached.
>>>>> +     * @throws IOException if unable to read from the input
>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>> parsed as a LogEvent
>>>>> +     */
>>>>> +    LogEvent parseFrom(Reader input) throws IOException,
>>>>> ParseException;
>>>>> +
>>>>> +    /**
>>>>> +     * Parse from a String, which is expected to contain exactly one
>>>>> log
>>>>> event.
>>>>> +     *
>>>>> +     * @param input  the string
>>>>> +     *
>>>>> +     * @return the parsed LogEvent
>>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>>> parsed as a LogEvent
>>>>> +     */
>>>>> +    LogEvent parseFrom(String input) throws ParseException;
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>> core/parser/XmlLogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/XmlLogEventParser.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/XmlLogEventParser.java
>>>>> new file mode 100644
>>>>> index 0000000..597e70b
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/XmlLogEventParser.java
>>>>> @@ -0,0 +1,32 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>> +
>>>>> +/**
>>>>> + * Parse the output from XmlLayout layout into instances of {@link
>>>>> LogEvent}.
>>>>> + */
>>>>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>>>>> +
>>>>> +    public XmlLogEventParser() {
>>>>> +        super(new Log4jXmlObjectMapper().readerF
>>>>> or(Log4jLogEvent.class));
>>>>> +    }
>>>>> +
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/c
>>>>> ore/parser/
>>>>> YamlLogEventParser.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/YamlLogEventParser.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/YamlLogEventParser.java
>>>>> new file mode 100644
>>>>> index 0000000..59b3463
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/YamlLogEventParser.java
>>>>> @@ -0,0 +1,33 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>>>>> +
>>>>> +/**
>>>>> + * Parse the output from YamlLayout layout into instances of {@link
>>>>> LogEvent}.
>>>>> + */
>>>>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser
>>>>> {
>>>>> +
>>>>> +    public YamlLogEventParser() {
>>>>> +        super(new Log4jYamlObjectMapper().reader
>>>>> For(Log4jLogEvent.class));
>>>>> +    }
>>>>> +
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>>> core/parser/package-info.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/par
>>>>> ser/package-info.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/package-info.java
>>>>> new file mode 100644
>>>>> index 0000000..3506570
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>>> parser/package-info.java
>>>>> @@ -0,0 +1,20 @@
>>>>> +/*
>>>>> + * 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.
>>>>> + */
>>>>> +/**
>>>>> + * Parsers for the output of various layouts.
>>>>> + */
>>>>> +package org.apache.logging.log4j.core.parser;
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/c
>>>>> ore/parser/
>>>>> JsonLogEventParserTest.java
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>>>> new file mode 100644
>>>>> index 0000000..5684602
>>>>> --- /dev/null
>>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>>> parser/JsonLogEventParserTest.java
>>>>> @@ -0,0 +1,103 @@
>>>>> +/*
>>>>> + * 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.parser;
>>>>> +
>>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>>> +import org.junit.Before;
>>>>> +import org.junit.Test;
>>>>> +
>>>>> +import static org.junit.Assert.assertEquals;
>>>>> +import static org.junit.Assert.assertNotNull;
>>>>> +
>>>>> +public class JsonLogEventParserTest {
>>>>> +
>>>>> +    private JsonLogEventParser parser;
>>>>> +
>>>>> +    private static final String JSON = "{\n" +
>>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>>>> +            "  \"thread\" : \"main\",\n" +
>>>>> +            "  \"level\" : \"INFO\",\n" +
>>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>>>> +            "  \"marker\" : {\n" +
>>>>> +            "    \"name\" : \"child\",\n" +
>>>>> +            "    \"parents\" : [ {\n" +
>>>>> +            "      \"name\" : \"parent\",\n" +
>>>>> +            "      \"parents\" : [ {\n" +
>>>>> +            "        \"name\" : \"grandparent\"\n" +
>>>>> +            "      } ]\n" +
>>>>> +            "    } ]\n" +
>>>>> +            "  },\n" +
>>>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>>>> +            "  \"thrown\" : {\n" +
>>>>> +            "    \"commonElementCount\" : 0,\n" +
>>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>>>> +            "      \"method\" : \"main\",\n" +
>>>>> +            "      \"file\" : \"Main.java\",\n" +
>>>>> +            "      \"line\" : 29,\n" +
>>>>> +            "      \"exact\" : true,\n" +
>>>>> +            "      \"location\" : \"classes/\",\n" +
>>>>> +            "      \"version\" : \"?\"\n" +
>>>>> +            "    } ]\n" +
>>>>> +            "  },\n" +
>>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>>>> +            "  \"endOfBatch\" : false,\n" +
>>>>> +            "  \"loggerFqcn\" :
>>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>>>> +
>>>>> +            "  \"contextMap\" : {\n" +
>>>>> +            "    \"bar\" : \"BAR\",\n" +
>>>>> +            "    \"foo\" : \"FOO\"\n" +
>>>>> +            "  },\n" +
>>>>> +            "  \"threadId\" : 1,\n" +
>>>>> +            "  \"threadPriority\" : 5,\n" +
>>>>> +            "  \"source\" : {\n" +
>>>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>>>> +            "    \"method\" : \"main\",\n" +
>>>>> +            "    \"file\" : \"Main.java\",\n" +
>>>>> +            "    \"line\" : 29\n" +
>>>>> +            "  }\n" +
>>>>> +            "}";
>>>>> +
>>>>> +    @Before
>>>>> +    public void setup() {
>>>>> +        parser = new JsonLogEventParser();
>>>>> +    }
>>>>> +
>>>>> +    @Test
>>>>> +    public void testString() throws ParseException {
>>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>>>> +        assertNotNull(logEvent);
>>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>>>> +        // TODO assert more here
>>>>> +    }
>>>>> +
>>>>> +    @Test(expected = ParseException.class)
>>>>> +    public void testStringEmpty() throws ParseException {
>>>>> +        parser.parseFrom("");
>>>>> +    }
>>>>> +
>>>>> +    @Test(expected = ParseException.class)
>>>>> +    public void testStringInvalidJson() throws ParseException {
>>>>> +        parser.parseFrom("foobar");
>>>>> +    }
>>>>> +
>>>>> +    @Test(expected = ParseException.class)
>>>>> +    public void testStringInvalidProperty() throws ParseException {
>>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>>>> +    }
>>>>> +
>>>>> +}
>>>>>
>>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>>> c4814a87/src/changes/changes.xml
>>>>> ----------------------------------------------------------------------
>>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>> index 074e31c..522f49b 100644
>>>>> --- a/src/changes/changes.xml
>>>>> +++ b/src/changes/changes.xml
>>>>> @@ -31,6 +31,9 @@
>>>>>             - "remove" - Removed
>>>>>        -->
>>>>>        <release version="2.9.0" date="2017-MM-DD" description="GA
>>>>> Release
>>>>> 2.9.0">
>>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>>>> +        Public API for parsing the output from
>>>>> JsonLayout/XmlLayout/YamlLayout
>>>>> into a LogEvent.
>>>>> +      </action>
>>>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>>>            Allow maxLength of StructuredData to be specified by the
>>>>> user.
>>>>>          </action>
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
OK, but RuntimeException should still never be thrown here...

Gary

On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <mi...@apache.org> wrote:

> But that's not quite correct. I want to distinguish between unable to
> parse due to malformed input (ParseException), and unable to read from
> input (IOException). When reading from String or byte[], unable to read
> from input shouldn't happen.
>
>
> On 2017-07-23 21:50, Gary Gregory wrote:
>
>> You already declare and throw ParseException, so I would use
>> ParseException.
>>
>> Gary
>>
>>
>>
>> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org>
>> wrote:
>>
>> Do you have another suggestion?
>>>
>>>
>>> On 2017-07-23 05:01, Gary Gregory wrote:
>>>
>>> -1 to throw new RuntimeException(e)
>>>>
>>>> Gary
>>>>
>>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>>
>>>> Repository: logging-log4j2
>>>> Updated Branches:
>>>>     refs/heads/LOG4J2-1986 [created] c4814a873
>>>>
>>>>
>>>> LOG4J2-1986 Public API for parsing the output from
>>>> JsonLayout/XmlLayout/YamlLayout
>>>> into a LogEvent
>>>>
>>>>
>>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>>> commit/c4814a87
>>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c
>>>> 4814a87
>>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c
>>>> 4814a87
>>>>
>>>> Branch: refs/heads/LOG4J2-1986
>>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>>> Parents: eea873e
>>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>>
>>>> ----------------------------------------------------------------------
>>>>    .../parser/AbstractJacksonLogEventParser.java   |  85
>>>> +++++++++++++++
>>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>>    .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>>    .../core/parser/JsonLogEventParserTest.java     | 103
>>>> +++++++++++++++++++
>>>>    src/changes/changes.xml                         |   3 +
>>>>    10 files changed, 456 insertions(+)
>>>> ----------------------------------------------------------------------
>>>>
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>> AbstractJacksonLogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/
>>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..8b00e57
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>> AbstractJacksonLogEventParser.java
>>>> @@ -0,0 +1,85 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>> +
>>>> +import java.io.IOException;
>>>> +import java.io.InputStream;
>>>> +import java.io.Reader;
>>>> +
>>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>>>> +    final ObjectReader objectReader;
>>>> +
>>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>>> +        this.objectReader = objectReader;
>>>> +    }
>>>> +
>>>> +    @Override
>>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>>>> ParseException {
>>>> +        try {
>>>> +            return objectReader.readValue(input);
>>>> +        } catch (JsonProcessingException e) {
>>>> +            throw new ParseException(e);
>>>> +        }
>>>> +    }
>>>> +
>>>> +    @Override
>>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>>> ParseException {
>>>> +        try {
>>>> +            return objectReader.readValue(input);
>>>> +        } catch (JsonProcessingException e) {
>>>> +            throw new ParseException(e);
>>>> +        }
>>>> +    }
>>>> +
>>>> +    @Override
>>>> +    public LogEvent parseFrom(String input) throws ParseException {
>>>> +        try {
>>>> +            return objectReader.readValue(input);
>>>> +        } catch (JsonProcessingException e) {
>>>> +            throw new ParseException(e);
>>>> +        } catch (IOException e) {
>>>> +            throw new RuntimeException(e);
>>>> +        }
>>>> +    }
>>>> +
>>>> +    @Override
>>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>>>> +        try {
>>>> +            return objectReader.readValue(input);
>>>> +        } catch (JsonProcessingException e) {
>>>> +            throw new ParseException(e);
>>>> +        } catch (IOException e) {
>>>> +            throw new RuntimeException(e);
>>>> +        }
>>>> +    }
>>>> +
>>>> +    @Override
>>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>>>> throws
>>>> ParseException {
>>>> +        try {
>>>> +            return objectReader.readValue(input, offset, length);
>>>> +        } catch (JsonProcessingException e) {
>>>> +            throw new ParseException(e);
>>>> +        } catch (IOException e) {
>>>> +            throw new RuntimeException(e);
>>>> +        }
>>>> +    }
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>> JsonLogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/JsonLogEventParser.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/JsonLogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..218de0b
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/JsonLogEventParser.java
>>>> @@ -0,0 +1,32 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>>> +
>>>> +/**
>>>> + * Parse the output from JsonLayout layout into instances of {@link
>>>> LogEvent}.
>>>> + */
>>>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
>>>> +
>>>> +    public JsonLogEventParser() {
>>>> +        super(new Log4jJsonObjectMapper().reader
>>>> For(Log4jLogEvent.class));
>>>> +    }
>>>> +
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>> core/parser/LogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/LogEventParser.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/LogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..88e84fb
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/LogEventParser.java
>>>> @@ -0,0 +1,64 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +
>>>> +import java.io.IOException;
>>>> +import java.io.InputStream;
>>>> +
>>>> +/**
>>>> + * Parse the output from a layout into instances of {@link LogEvent}.
>>>> + */
>>>> +public interface LogEventParser {
>>>> +
>>>> +    /**
>>>> +     * Parse from an InputStream. The input stream may contain multiple
>>>> log events.
>>>> +     * This method will read one log event and leave the stream open
>>>> and
>>>> positioned to read the
>>>> +     * next log event.
>>>> +     *
>>>> +     * @param input  the input stream
>>>> +     *
>>>> +     * @return the parsed LogEvent, or {@literal null} of end of input
>>>> is
>>>> reached.
>>>> +     * @throws IOException if unable to read from the input
>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>> parsed as a LogEvent
>>>> +     */
>>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>>> ParseException;
>>>> +
>>>> +    /**
>>>> +     * Parse from a byte array, which is expected to contain exactly
>>>> one
>>>> log event.
>>>> +     *
>>>> +     * @param input  the byte array
>>>> +     *
>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>> parsed as a LogEvent
>>>> +     */
>>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>>> +
>>>> +    /**
>>>> +     * Parse from a specified range of a byte array. The specified
>>>> range
>>>> is expected to contain
>>>> +     * exactly one log event.
>>>> +     *
>>>> +     * @param input  the byte array
>>>> +     * @param offset  the initial offset
>>>> +     * @param length  the length
>>>> +     *
>>>> +     * @return the parsed LogEvent, never {@literal null}.
>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>> parsed as a LogEvent
>>>> +     */
>>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>>>> ParseException;
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>> core/parser/ParseException.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/ParseException.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/ParseException.java
>>>> new file mode 100644
>>>> index 0000000..8f5fa54
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/ParseException.java
>>>> @@ -0,0 +1,34 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +/**
>>>> + * Thrown when the input cannot be parsed.
>>>> + */
>>>> +public class ParseException extends Exception {
>>>> +    public ParseException(String message) {
>>>> +        super(message);
>>>> +    }
>>>> +
>>>> +    public ParseException(String message, Throwable cause) {
>>>> +        super(message, cause);
>>>> +    }
>>>> +
>>>> +    public ParseException(Throwable cause) {
>>>> +        super(cause);
>>>> +    }
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>> TextLogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/TextLogEventParser.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/TextLogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..3428024
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/TextLogEventParser.java
>>>> @@ -0,0 +1,50 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +
>>>> +import java.io.IOException;
>>>> +import java.io.Reader;
>>>> +
>>>> +/**
>>>> + * Parse the output from a text based layout into instances of {@link
>>>> LogEvent}.
>>>> + */
>>>> +public interface TextLogEventParser extends LogEventParser {
>>>> +    /**
>>>> +     * Parse from a Reader. The reader may contain multiple log events.
>>>> +     * This method will read one log event and leave the reader open
>>>> and
>>>> positioned to read the
>>>> +     * next log event.
>>>> +     *
>>>> +     * @param input  the reader
>>>> +     *
>>>> +     * @return the parsed LogEvent, or {@literal null} of end of input
>>>> is
>>>> reached.
>>>> +     * @throws IOException if unable to read from the input
>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>> parsed as a LogEvent
>>>> +     */
>>>> +    LogEvent parseFrom(Reader input) throws IOException,
>>>> ParseException;
>>>> +
>>>> +    /**
>>>> +     * Parse from a String, which is expected to contain exactly one
>>>> log
>>>> event.
>>>> +     *
>>>> +     * @param input  the string
>>>> +     *
>>>> +     * @return the parsed LogEvent
>>>> +     * @throws ParseException if the input is malformed and cannot be
>>>> parsed as a LogEvent
>>>> +     */
>>>> +    LogEvent parseFrom(String input) throws ParseException;
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>> core/parser/XmlLogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/XmlLogEventParser.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/XmlLogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..597e70b
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/XmlLogEventParser.java
>>>> @@ -0,0 +1,32 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>> +
>>>> +/**
>>>> + * Parse the output from XmlLayout layout into instances of {@link
>>>> LogEvent}.
>>>> + */
>>>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>>>> +
>>>> +    public XmlLogEventParser() {
>>>> +        super(new Log4jXmlObjectMapper().readerF
>>>> or(Log4jLogEvent.class));
>>>> +    }
>>>> +
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>>> YamlLogEventParser.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/YamlLogEventParser.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/YamlLogEventParser.java
>>>> new file mode 100644
>>>> index 0000000..59b3463
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/YamlLogEventParser.java
>>>> @@ -0,0 +1,33 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>>>> +
>>>> +/**
>>>> + * Parse the output from YamlLayout layout into instances of {@link
>>>> LogEvent}.
>>>> + */
>>>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
>>>> +
>>>> +    public YamlLogEventParser() {
>>>> +        super(new Log4jYamlObjectMapper().reader
>>>> For(Log4jLogEvent.class));
>>>> +    }
>>>> +
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>>> core/parser/package-info.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>>> ser/package-info.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/package-info.java
>>>> new file mode 100644
>>>> index 0000000..3506570
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>>> parser/package-info.java
>>>> @@ -0,0 +1,20 @@
>>>> +/*
>>>> + * 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.
>>>> + */
>>>> +/**
>>>> + * Parsers for the output of various layouts.
>>>> + */
>>>> +package org.apache.logging.log4j.core.parser;
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
>>>> JsonLogEventParserTest.java
>>>> ----------------------------------------------------------------------
>>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>>> new file mode 100644
>>>> index 0000000..5684602
>>>> --- /dev/null
>>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>>> parser/JsonLogEventParserTest.java
>>>> @@ -0,0 +1,103 @@
>>>> +/*
>>>> + * 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.parser;
>>>> +
>>>> +import org.apache.logging.log4j.core.LogEvent;
>>>> +import org.junit.Before;
>>>> +import org.junit.Test;
>>>> +
>>>> +import static org.junit.Assert.assertEquals;
>>>> +import static org.junit.Assert.assertNotNull;
>>>> +
>>>> +public class JsonLogEventParserTest {
>>>> +
>>>> +    private JsonLogEventParser parser;
>>>> +
>>>> +    private static final String JSON = "{\n" +
>>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>>> +            "  \"thread\" : \"main\",\n" +
>>>> +            "  \"level\" : \"INFO\",\n" +
>>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>>> +            "  \"marker\" : {\n" +
>>>> +            "    \"name\" : \"child\",\n" +
>>>> +            "    \"parents\" : [ {\n" +
>>>> +            "      \"name\" : \"parent\",\n" +
>>>> +            "      \"parents\" : [ {\n" +
>>>> +            "        \"name\" : \"grandparent\"\n" +
>>>> +            "      } ]\n" +
>>>> +            "    } ]\n" +
>>>> +            "  },\n" +
>>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>>> +            "  \"thrown\" : {\n" +
>>>> +            "    \"commonElementCount\" : 0,\n" +
>>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>>> +            "      \"method\" : \"main\",\n" +
>>>> +            "      \"file\" : \"Main.java\",\n" +
>>>> +            "      \"line\" : 29,\n" +
>>>> +            "      \"exact\" : true,\n" +
>>>> +            "      \"location\" : \"classes/\",\n" +
>>>> +            "      \"version\" : \"?\"\n" +
>>>> +            "    } ]\n" +
>>>> +            "  },\n" +
>>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>>> +            "  \"endOfBatch\" : false,\n" +
>>>> +            "  \"loggerFqcn\" :
>>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>>> +
>>>> +            "  \"contextMap\" : {\n" +
>>>> +            "    \"bar\" : \"BAR\",\n" +
>>>> +            "    \"foo\" : \"FOO\"\n" +
>>>> +            "  },\n" +
>>>> +            "  \"threadId\" : 1,\n" +
>>>> +            "  \"threadPriority\" : 5,\n" +
>>>> +            "  \"source\" : {\n" +
>>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>>> +            "    \"method\" : \"main\",\n" +
>>>> +            "    \"file\" : \"Main.java\",\n" +
>>>> +            "    \"line\" : 29\n" +
>>>> +            "  }\n" +
>>>> +            "}";
>>>> +
>>>> +    @Before
>>>> +    public void setup() {
>>>> +        parser = new JsonLogEventParser();
>>>> +    }
>>>> +
>>>> +    @Test
>>>> +    public void testString() throws ParseException {
>>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>>> +        assertNotNull(logEvent);
>>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>>> +        // TODO assert more here
>>>> +    }
>>>> +
>>>> +    @Test(expected = ParseException.class)
>>>> +    public void testStringEmpty() throws ParseException {
>>>> +        parser.parseFrom("");
>>>> +    }
>>>> +
>>>> +    @Test(expected = ParseException.class)
>>>> +    public void testStringInvalidJson() throws ParseException {
>>>> +        parser.parseFrom("foobar");
>>>> +    }
>>>> +
>>>> +    @Test(expected = ParseException.class)
>>>> +    public void testStringInvalidProperty() throws ParseException {
>>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>>> +    }
>>>> +
>>>> +}
>>>>
>>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>>> c4814a87/src/changes/changes.xml
>>>> ----------------------------------------------------------------------
>>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>> index 074e31c..522f49b 100644
>>>> --- a/src/changes/changes.xml
>>>> +++ b/src/changes/changes.xml
>>>> @@ -31,6 +31,9 @@
>>>>             - "remove" - Removed
>>>>        -->
>>>>        <release version="2.9.0" date="2017-MM-DD" description="GA
>>>> Release
>>>> 2.9.0">
>>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>>> +        Public API for parsing the output from
>>>> JsonLayout/XmlLayout/YamlLayout
>>>> into a LogEvent.
>>>> +      </action>
>>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>>            Allow maxLength of StructuredData to be specified by the
>>>> user.
>>>>          </action>
>>>>
>>>>
>>>>
>>>
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
But that's not quite correct. I want to distinguish between unable to 
parse due to malformed input (ParseException), and unable to read from 
input (IOException). When reading from String or byte[], unable to read 
from input shouldn't happen.

On 2017-07-23 21:50, Gary Gregory wrote:
> You already declare and throw ParseException, so I would use ParseException.
> 
> Gary
> 
> 
> 
> On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org> wrote:
> 
>> Do you have another suggestion?
>>
>>
>> On 2017-07-23 05:01, Gary Gregory wrote:
>>
>>> -1 to throw new RuntimeException(e)
>>>
>>> Gary
>>>
>>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>>
>>> Repository: logging-log4j2
>>> Updated Branches:
>>>     refs/heads/LOG4J2-1986 [created] c4814a873
>>>
>>>
>>> LOG4J2-1986 Public API for parsing the output from
>>> JsonLayout/XmlLayout/YamlLayout
>>> into a LogEvent
>>>
>>>
>>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>>> commit/c4814a87
>>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
>>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87
>>>
>>> Branch: refs/heads/LOG4J2-1986
>>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>>> Parents: eea873e
>>> Author: Mikael Ståldal <mi...@staldal.nu>
>>> Authored: Sat Jul 22 22:48:24 2017 +0200
>>> Committer: Mikael Ståldal <mi...@staldal.nu>
>>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>>
>>> ----------------------------------------------------------------------
>>>    .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
>>>    .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>>    .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>>    .../log4j/core/parser/ParseException.java       |  34 ++++++
>>>    .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>>    .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>>    .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>>    .../logging/log4j/core/parser/package-info.java |  20 ++++
>>>    .../core/parser/JsonLogEventParserTest.java     | 103
>>> +++++++++++++++++++
>>>    src/changes/changes.xml                         |   3 +
>>>    10 files changed, 456 insertions(+)
>>> ----------------------------------------------------------------------
>>>
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>> AbstractJacksonLogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/
>>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
>>> new file mode 100644
>>> index 0000000..8b00e57
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>> AbstractJacksonLogEventParser.java
>>> @@ -0,0 +1,85 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import com.fasterxml.jackson.core.JsonProcessingException;
>>> +import com.fasterxml.jackson.databind.ObjectReader;
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>> +
>>> +import java.io.IOException;
>>> +import java.io.InputStream;
>>> +import java.io.Reader;
>>> +
>>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>>> +    final ObjectReader objectReader;
>>> +
>>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>>> +        this.objectReader = objectReader;
>>> +    }
>>> +
>>> +    @Override
>>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>>> ParseException {
>>> +        try {
>>> +            return objectReader.readValue(input);
>>> +        } catch (JsonProcessingException e) {
>>> +            throw new ParseException(e);
>>> +        }
>>> +    }
>>> +
>>> +    @Override
>>> +    public LogEvent parseFrom(Reader input) throws IOException,
>>> ParseException {
>>> +        try {
>>> +            return objectReader.readValue(input);
>>> +        } catch (JsonProcessingException e) {
>>> +            throw new ParseException(e);
>>> +        }
>>> +    }
>>> +
>>> +    @Override
>>> +    public LogEvent parseFrom(String input) throws ParseException {
>>> +        try {
>>> +            return objectReader.readValue(input);
>>> +        } catch (JsonProcessingException e) {
>>> +            throw new ParseException(e);
>>> +        } catch (IOException e) {
>>> +            throw new RuntimeException(e);
>>> +        }
>>> +    }
>>> +
>>> +    @Override
>>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>>> +        try {
>>> +            return objectReader.readValue(input);
>>> +        } catch (JsonProcessingException e) {
>>> +            throw new ParseException(e);
>>> +        } catch (IOException e) {
>>> +            throw new RuntimeException(e);
>>> +        }
>>> +    }
>>> +
>>> +    @Override
>>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>>> throws
>>> ParseException {
>>> +        try {
>>> +            return objectReader.readValue(input, offset, length);
>>> +        } catch (JsonProcessingException e) {
>>> +            throw new ParseException(e);
>>> +        } catch (IOException e) {
>>> +            throw new RuntimeException(e);
>>> +        }
>>> +    }
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>> JsonLogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/JsonLogEventParser.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/JsonLogEventParser.java
>>> new file mode 100644
>>> index 0000000..218de0b
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/JsonLogEventParser.java
>>> @@ -0,0 +1,32 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>>> +
>>> +/**
>>> + * Parse the output from JsonLayout layout into instances of {@link
>>> LogEvent}.
>>> + */
>>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
>>> +
>>> +    public JsonLogEventParser() {
>>> +        super(new Log4jJsonObjectMapper().reader
>>> For(Log4jLogEvent.class));
>>> +    }
>>> +
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>> core/parser/LogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/LogEventParser.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/LogEventParser.java
>>> new file mode 100644
>>> index 0000000..88e84fb
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/LogEventParser.java
>>> @@ -0,0 +1,64 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +
>>> +import java.io.IOException;
>>> +import java.io.InputStream;
>>> +
>>> +/**
>>> + * Parse the output from a layout into instances of {@link LogEvent}.
>>> + */
>>> +public interface LogEventParser {
>>> +
>>> +    /**
>>> +     * Parse from an InputStream. The input stream may contain multiple
>>> log events.
>>> +     * This method will read one log event and leave the stream open and
>>> positioned to read the
>>> +     * next log event.
>>> +     *
>>> +     * @param input  the input stream
>>> +     *
>>> +     * @return the parsed LogEvent, or {@literal null} of end of input is
>>> reached.
>>> +     * @throws IOException if unable to read from the input
>>> +     * @throws ParseException if the input is malformed and cannot be
>>> parsed as a LogEvent
>>> +     */
>>> +    LogEvent parseFrom(InputStream input) throws IOException,
>>> ParseException;
>>> +
>>> +    /**
>>> +     * Parse from a byte array, which is expected to contain exactly one
>>> log event.
>>> +     *
>>> +     * @param input  the byte array
>>> +     *
>>> +     * @return the parsed LogEvent, never {@literal null}.
>>> +     * @throws ParseException if the input is malformed and cannot be
>>> parsed as a LogEvent
>>> +     */
>>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>>> +
>>> +    /**
>>> +     * Parse from a specified range of a byte array. The specified range
>>> is expected to contain
>>> +     * exactly one log event.
>>> +     *
>>> +     * @param input  the byte array
>>> +     * @param offset  the initial offset
>>> +     * @param length  the length
>>> +     *
>>> +     * @return the parsed LogEvent, never {@literal null}.
>>> +     * @throws ParseException if the input is malformed and cannot be
>>> parsed as a LogEvent
>>> +     */
>>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>>> ParseException;
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>> core/parser/ParseException.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/ParseException.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/ParseException.java
>>> new file mode 100644
>>> index 0000000..8f5fa54
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/ParseException.java
>>> @@ -0,0 +1,34 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +/**
>>> + * Thrown when the input cannot be parsed.
>>> + */
>>> +public class ParseException extends Exception {
>>> +    public ParseException(String message) {
>>> +        super(message);
>>> +    }
>>> +
>>> +    public ParseException(String message, Throwable cause) {
>>> +        super(message, cause);
>>> +    }
>>> +
>>> +    public ParseException(Throwable cause) {
>>> +        super(cause);
>>> +    }
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>> TextLogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/TextLogEventParser.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/TextLogEventParser.java
>>> new file mode 100644
>>> index 0000000..3428024
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/TextLogEventParser.java
>>> @@ -0,0 +1,50 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +
>>> +import java.io.IOException;
>>> +import java.io.Reader;
>>> +
>>> +/**
>>> + * Parse the output from a text based layout into instances of {@link
>>> LogEvent}.
>>> + */
>>> +public interface TextLogEventParser extends LogEventParser {
>>> +    /**
>>> +     * Parse from a Reader. The reader may contain multiple log events.
>>> +     * This method will read one log event and leave the reader open and
>>> positioned to read the
>>> +     * next log event.
>>> +     *
>>> +     * @param input  the reader
>>> +     *
>>> +     * @return the parsed LogEvent, or {@literal null} of end of input is
>>> reached.
>>> +     * @throws IOException if unable to read from the input
>>> +     * @throws ParseException if the input is malformed and cannot be
>>> parsed as a LogEvent
>>> +     */
>>> +    LogEvent parseFrom(Reader input) throws IOException, ParseException;
>>> +
>>> +    /**
>>> +     * Parse from a String, which is expected to contain exactly one log
>>> event.
>>> +     *
>>> +     * @param input  the string
>>> +     *
>>> +     * @return the parsed LogEvent
>>> +     * @throws ParseException if the input is malformed and cannot be
>>> parsed as a LogEvent
>>> +     */
>>> +    LogEvent parseFrom(String input) throws ParseException;
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>> core/parser/XmlLogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/XmlLogEventParser.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/XmlLogEventParser.java
>>> new file mode 100644
>>> index 0000000..597e70b
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/XmlLogEventParser.java
>>> @@ -0,0 +1,32 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>> +
>>> +/**
>>> + * Parse the output from XmlLayout layout into instances of {@link
>>> LogEvent}.
>>> + */
>>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>>> +
>>> +    public XmlLogEventParser() {
>>> +        super(new Log4jXmlObjectMapper().readerF
>>> or(Log4jLogEvent.class));
>>> +    }
>>> +
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>>> YamlLogEventParser.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/YamlLogEventParser.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/YamlLogEventParser.java
>>> new file mode 100644
>>> index 0000000..59b3463
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/YamlLogEventParser.java
>>> @@ -0,0 +1,33 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>>> +
>>> +/**
>>> + * Parse the output from YamlLayout layout into instances of {@link
>>> LogEvent}.
>>> + */
>>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
>>> +
>>> +    public YamlLogEventParser() {
>>> +        super(new Log4jYamlObjectMapper().reader
>>> For(Log4jLogEvent.class));
>>> +    }
>>> +
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>>> core/parser/package-info.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>>> ser/package-info.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/package-info.java
>>> new file mode 100644
>>> index 0000000..3506570
>>> --- /dev/null
>>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>>> parser/package-info.java
>>> @@ -0,0 +1,20 @@
>>> +/*
>>> + * 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.
>>> + */
>>> +/**
>>> + * Parsers for the output of various layouts.
>>> + */
>>> +package org.apache.logging.log4j.core.parser;
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
>>> JsonLogEventParserTest.java
>>> ----------------------------------------------------------------------
>>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>>> new file mode 100644
>>> index 0000000..5684602
>>> --- /dev/null
>>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>>> parser/JsonLogEventParserTest.java
>>> @@ -0,0 +1,103 @@
>>> +/*
>>> + * 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.parser;
>>> +
>>> +import org.apache.logging.log4j.core.LogEvent;
>>> +import org.junit.Before;
>>> +import org.junit.Test;
>>> +
>>> +import static org.junit.Assert.assertEquals;
>>> +import static org.junit.Assert.assertNotNull;
>>> +
>>> +public class JsonLogEventParserTest {
>>> +
>>> +    private JsonLogEventParser parser;
>>> +
>>> +    private static final String JSON = "{\n" +
>>> +            "  \"timeMillis\" : 1493121664118,\n" +
>>> +            "  \"thread\" : \"main\",\n" +
>>> +            "  \"level\" : \"INFO\",\n" +
>>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>>> +            "  \"marker\" : {\n" +
>>> +            "    \"name\" : \"child\",\n" +
>>> +            "    \"parents\" : [ {\n" +
>>> +            "      \"name\" : \"parent\",\n" +
>>> +            "      \"parents\" : [ {\n" +
>>> +            "        \"name\" : \"grandparent\"\n" +
>>> +            "      } ]\n" +
>>> +            "    } ]\n" +
>>> +            "  },\n" +
>>> +            "  \"message\" : \"Hello, world!\",\n" +
>>> +            "  \"thrown\" : {\n" +
>>> +            "    \"commonElementCount\" : 0,\n" +
>>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>>> +            "    \"extendedStackTrace\" : [ {\n" +
>>> +            "      \"class\" : \"logtest.Main\",\n" +
>>> +            "      \"method\" : \"main\",\n" +
>>> +            "      \"file\" : \"Main.java\",\n" +
>>> +            "      \"line\" : 29,\n" +
>>> +            "      \"exact\" : true,\n" +
>>> +            "      \"location\" : \"classes/\",\n" +
>>> +            "      \"version\" : \"?\"\n" +
>>> +            "    } ]\n" +
>>> +            "  },\n" +
>>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>>> +            "  \"endOfBatch\" : false,\n" +
>>> +            "  \"loggerFqcn\" :
>>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>>> +
>>> +            "  \"contextMap\" : {\n" +
>>> +            "    \"bar\" : \"BAR\",\n" +
>>> +            "    \"foo\" : \"FOO\"\n" +
>>> +            "  },\n" +
>>> +            "  \"threadId\" : 1,\n" +
>>> +            "  \"threadPriority\" : 5,\n" +
>>> +            "  \"source\" : {\n" +
>>> +            "    \"class\" : \"logtest.Main\",\n" +
>>> +            "    \"method\" : \"main\",\n" +
>>> +            "    \"file\" : \"Main.java\",\n" +
>>> +            "    \"line\" : 29\n" +
>>> +            "  }\n" +
>>> +            "}";
>>> +
>>> +    @Before
>>> +    public void setup() {
>>> +        parser = new JsonLogEventParser();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testString() throws ParseException {
>>> +        LogEvent logEvent = parser.parseFrom(JSON);
>>> +        assertNotNull(logEvent);
>>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>>> +        // TODO assert more here
>>> +    }
>>> +
>>> +    @Test(expected = ParseException.class)
>>> +    public void testStringEmpty() throws ParseException {
>>> +        parser.parseFrom("");
>>> +    }
>>> +
>>> +    @Test(expected = ParseException.class)
>>> +    public void testStringInvalidJson() throws ParseException {
>>> +        parser.parseFrom("foobar");
>>> +    }
>>> +
>>> +    @Test(expected = ParseException.class)
>>> +    public void testStringInvalidProperty() throws ParseException {
>>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>>> +    }
>>> +
>>> +}
>>>
>>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>>> c4814a87/src/changes/changes.xml
>>> ----------------------------------------------------------------------
>>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>> index 074e31c..522f49b 100644
>>> --- a/src/changes/changes.xml
>>> +++ b/src/changes/changes.xml
>>> @@ -31,6 +31,9 @@
>>>             - "remove" - Removed
>>>        -->
>>>        <release version="2.9.0" date="2017-MM-DD" description="GA Release
>>> 2.9.0">
>>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>>> +        Public API for parsing the output from
>>> JsonLayout/XmlLayout/YamlLayout
>>> into a LogEvent.
>>> +      </action>
>>>          <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>>            Allow maxLength of StructuredData to be specified by the user.
>>>          </action>
>>>
>>>
>>
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
You already declare and throw ParseException, so I would use ParseException.

Gary



On Sun, Jul 23, 2017 at 11:25 AM, Mikael Ståldal <mi...@apache.org> wrote:

> Do you have another suggestion?
>
>
> On 2017-07-23 05:01, Gary Gregory wrote:
>
>> -1 to throw new RuntimeException(e)
>>
>> Gary
>>
>> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
>>
>> Repository: logging-log4j2
>> Updated Branches:
>>    refs/heads/LOG4J2-1986 [created] c4814a873
>>
>>
>> LOG4J2-1986 Public API for parsing the output from
>> JsonLayout/XmlLayout/YamlLayout
>> into a LogEvent
>>
>>
>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
>> commit/c4814a87
>> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
>> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87
>>
>> Branch: refs/heads/LOG4J2-1986
>> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
>> Parents: eea873e
>> Author: Mikael Ståldal <mi...@staldal.nu>
>> Authored: Sat Jul 22 22:48:24 2017 +0200
>> Committer: Mikael Ståldal <mi...@staldal.nu>
>> Committed: Sat Jul 22 22:48:24 2017 +0200
>>
>> ----------------------------------------------------------------------
>>   .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
>>   .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>>   .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>>   .../log4j/core/parser/ParseException.java       |  34 ++++++
>>   .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>>   .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>>   .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>>   .../logging/log4j/core/parser/package-info.java |  20 ++++
>>   .../core/parser/JsonLogEventParserTest.java     | 103
>> +++++++++++++++++++
>>   src/changes/changes.xml                         |   3 +
>>   10 files changed, 456 insertions(+)
>> ----------------------------------------------------------------------
>>
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>> AbstractJacksonLogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/
>> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
>> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
>> new file mode 100644
>> index 0000000..8b00e57
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>> AbstractJacksonLogEventParser.java
>> @@ -0,0 +1,85 @@
>> +/*
>> + * 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.parser;
>> +
>> +import com.fasterxml.jackson.core.JsonProcessingException;
>> +import com.fasterxml.jackson.databind.ObjectReader;
>> +import org.apache.logging.log4j.core.LogEvent;
>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>> +
>> +import java.io.IOException;
>> +import java.io.InputStream;
>> +import java.io.Reader;
>> +
>> +class AbstractJacksonLogEventParser implements TextLogEventParser {
>> +    final ObjectReader objectReader;
>> +
>> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
>> +        this.objectReader = objectReader;
>> +    }
>> +
>> +    @Override
>> +    public LogEvent parseFrom(InputStream input) throws IOException,
>> ParseException {
>> +        try {
>> +            return objectReader.readValue(input);
>> +        } catch (JsonProcessingException e) {
>> +            throw new ParseException(e);
>> +        }
>> +    }
>> +
>> +    @Override
>> +    public LogEvent parseFrom(Reader input) throws IOException,
>> ParseException {
>> +        try {
>> +            return objectReader.readValue(input);
>> +        } catch (JsonProcessingException e) {
>> +            throw new ParseException(e);
>> +        }
>> +    }
>> +
>> +    @Override
>> +    public LogEvent parseFrom(String input) throws ParseException {
>> +        try {
>> +            return objectReader.readValue(input);
>> +        } catch (JsonProcessingException e) {
>> +            throw new ParseException(e);
>> +        } catch (IOException e) {
>> +            throw new RuntimeException(e);
>> +        }
>> +    }
>> +
>> +    @Override
>> +    public LogEvent parseFrom(byte[] input) throws ParseException {
>> +        try {
>> +            return objectReader.readValue(input);
>> +        } catch (JsonProcessingException e) {
>> +            throw new ParseException(e);
>> +        } catch (IOException e) {
>> +            throw new RuntimeException(e);
>> +        }
>> +    }
>> +
>> +    @Override
>> +    public LogEvent parseFrom(byte[] input, int offset, int length)
>> throws
>> ParseException {
>> +        try {
>> +            return objectReader.readValue(input, offset, length);
>> +        } catch (JsonProcessingException e) {
>> +            throw new ParseException(e);
>> +        } catch (IOException e) {
>> +            throw new RuntimeException(e);
>> +        }
>> +    }
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>> JsonLogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/JsonLogEventParser.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/JsonLogEventParser.java
>> new file mode 100644
>> index 0000000..218de0b
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/JsonLogEventParser.java
>> @@ -0,0 +1,32 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
>> +
>> +/**
>> + * Parse the output from JsonLayout layout into instances of {@link
>> LogEvent}.
>> + */
>> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
>> +
>> +    public JsonLogEventParser() {
>> +        super(new Log4jJsonObjectMapper().reader
>> For(Log4jLogEvent.class));
>> +    }
>> +
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>> core/parser/LogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/LogEventParser.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/LogEventParser.java
>> new file mode 100644
>> index 0000000..88e84fb
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/LogEventParser.java
>> @@ -0,0 +1,64 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +
>> +import java.io.IOException;
>> +import java.io.InputStream;
>> +
>> +/**
>> + * Parse the output from a layout into instances of {@link LogEvent}.
>> + */
>> +public interface LogEventParser {
>> +
>> +    /**
>> +     * Parse from an InputStream. The input stream may contain multiple
>> log events.
>> +     * This method will read one log event and leave the stream open and
>> positioned to read the
>> +     * next log event.
>> +     *
>> +     * @param input  the input stream
>> +     *
>> +     * @return the parsed LogEvent, or {@literal null} of end of input is
>> reached.
>> +     * @throws IOException if unable to read from the input
>> +     * @throws ParseException if the input is malformed and cannot be
>> parsed as a LogEvent
>> +     */
>> +    LogEvent parseFrom(InputStream input) throws IOException,
>> ParseException;
>> +
>> +    /**
>> +     * Parse from a byte array, which is expected to contain exactly one
>> log event.
>> +     *
>> +     * @param input  the byte array
>> +     *
>> +     * @return the parsed LogEvent, never {@literal null}.
>> +     * @throws ParseException if the input is malformed and cannot be
>> parsed as a LogEvent
>> +     */
>> +    LogEvent parseFrom(byte[] input) throws ParseException;
>> +
>> +    /**
>> +     * Parse from a specified range of a byte array. The specified range
>> is expected to contain
>> +     * exactly one log event.
>> +     *
>> +     * @param input  the byte array
>> +     * @param offset  the initial offset
>> +     * @param length  the length
>> +     *
>> +     * @return the parsed LogEvent, never {@literal null}.
>> +     * @throws ParseException if the input is malformed and cannot be
>> parsed as a LogEvent
>> +     */
>> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
>> ParseException;
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>> core/parser/ParseException.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/ParseException.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/ParseException.java
>> new file mode 100644
>> index 0000000..8f5fa54
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/ParseException.java
>> @@ -0,0 +1,34 @@
>> +/*
>> + * 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.parser;
>> +
>> +/**
>> + * Thrown when the input cannot be parsed.
>> + */
>> +public class ParseException extends Exception {
>> +    public ParseException(String message) {
>> +        super(message);
>> +    }
>> +
>> +    public ParseException(String message, Throwable cause) {
>> +        super(message, cause);
>> +    }
>> +
>> +    public ParseException(Throwable cause) {
>> +        super(cause);
>> +    }
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>> TextLogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/TextLogEventParser.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/TextLogEventParser.java
>> new file mode 100644
>> index 0000000..3428024
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/TextLogEventParser.java
>> @@ -0,0 +1,50 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +
>> +import java.io.IOException;
>> +import java.io.Reader;
>> +
>> +/**
>> + * Parse the output from a text based layout into instances of {@link
>> LogEvent}.
>> + */
>> +public interface TextLogEventParser extends LogEventParser {
>> +    /**
>> +     * Parse from a Reader. The reader may contain multiple log events.
>> +     * This method will read one log event and leave the reader open and
>> positioned to read the
>> +     * next log event.
>> +     *
>> +     * @param input  the reader
>> +     *
>> +     * @return the parsed LogEvent, or {@literal null} of end of input is
>> reached.
>> +     * @throws IOException if unable to read from the input
>> +     * @throws ParseException if the input is malformed and cannot be
>> parsed as a LogEvent
>> +     */
>> +    LogEvent parseFrom(Reader input) throws IOException, ParseException;
>> +
>> +    /**
>> +     * Parse from a String, which is expected to contain exactly one log
>> event.
>> +     *
>> +     * @param input  the string
>> +     *
>> +     * @return the parsed LogEvent
>> +     * @throws ParseException if the input is malformed and cannot be
>> parsed as a LogEvent
>> +     */
>> +    LogEvent parseFrom(String input) throws ParseException;
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>> core/parser/XmlLogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/XmlLogEventParser.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/XmlLogEventParser.java
>> new file mode 100644
>> index 0000000..597e70b
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/XmlLogEventParser.java
>> @@ -0,0 +1,32 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>> +
>> +/**
>> + * Parse the output from XmlLayout layout into instances of {@link
>> LogEvent}.
>> + */
>> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
>> +
>> +    public XmlLogEventParser() {
>> +        super(new Log4jXmlObjectMapper().readerF
>> or(Log4jLogEvent.class));
>> +    }
>> +
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
>> YamlLogEventParser.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/YamlLogEventParser.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/YamlLogEventParser.java
>> new file mode 100644
>> index 0000000..59b3463
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/YamlLogEventParser.java
>> @@ -0,0 +1,33 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
>> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
>> +
>> +/**
>> + * Parse the output from YamlLayout layout into instances of {@link
>> LogEvent}.
>> + */
>> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
>> +
>> +    public YamlLogEventParser() {
>> +        super(new Log4jYamlObjectMapper().reader
>> For(Log4jLogEvent.class));
>> +    }
>> +
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
>> core/parser/package-info.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/par
>> ser/package-info.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/package-info.java
>> new file mode 100644
>> index 0000000..3506570
>> --- /dev/null
>> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
>> parser/package-info.java
>> @@ -0,0 +1,20 @@
>> +/*
>> + * 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.
>> + */
>> +/**
>> + * Parsers for the output of various layouts.
>> + */
>> +package org.apache.logging.log4j.core.parser;
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
>> JsonLogEventParserTest.java
>> ----------------------------------------------------------------------
>> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
>> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
>> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
>> new file mode 100644
>> index 0000000..5684602
>> --- /dev/null
>> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
>> parser/JsonLogEventParserTest.java
>> @@ -0,0 +1,103 @@
>> +/*
>> + * 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.parser;
>> +
>> +import org.apache.logging.log4j.core.LogEvent;
>> +import org.junit.Before;
>> +import org.junit.Test;
>> +
>> +import static org.junit.Assert.assertEquals;
>> +import static org.junit.Assert.assertNotNull;
>> +
>> +public class JsonLogEventParserTest {
>> +
>> +    private JsonLogEventParser parser;
>> +
>> +    private static final String JSON = "{\n" +
>> +            "  \"timeMillis\" : 1493121664118,\n" +
>> +            "  \"thread\" : \"main\",\n" +
>> +            "  \"level\" : \"INFO\",\n" +
>> +            "  \"loggerName\" : \"HelloWorld\",\n" +
>> +            "  \"marker\" : {\n" +
>> +            "    \"name\" : \"child\",\n" +
>> +            "    \"parents\" : [ {\n" +
>> +            "      \"name\" : \"parent\",\n" +
>> +            "      \"parents\" : [ {\n" +
>> +            "        \"name\" : \"grandparent\"\n" +
>> +            "      } ]\n" +
>> +            "    } ]\n" +
>> +            "  },\n" +
>> +            "  \"message\" : \"Hello, world!\",\n" +
>> +            "  \"thrown\" : {\n" +
>> +            "    \"commonElementCount\" : 0,\n" +
>> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
>> +            "    \"extendedStackTrace\" : [ {\n" +
>> +            "      \"class\" : \"logtest.Main\",\n" +
>> +            "      \"method\" : \"main\",\n" +
>> +            "      \"file\" : \"Main.java\",\n" +
>> +            "      \"line\" : 29,\n" +
>> +            "      \"exact\" : true,\n" +
>> +            "      \"location\" : \"classes/\",\n" +
>> +            "      \"version\" : \"?\"\n" +
>> +            "    } ]\n" +
>> +            "  },\n" +
>> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
>> +            "  \"endOfBatch\" : false,\n" +
>> +            "  \"loggerFqcn\" :
>> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
>> +
>> +            "  \"contextMap\" : {\n" +
>> +            "    \"bar\" : \"BAR\",\n" +
>> +            "    \"foo\" : \"FOO\"\n" +
>> +            "  },\n" +
>> +            "  \"threadId\" : 1,\n" +
>> +            "  \"threadPriority\" : 5,\n" +
>> +            "  \"source\" : {\n" +
>> +            "    \"class\" : \"logtest.Main\",\n" +
>> +            "    \"method\" : \"main\",\n" +
>> +            "    \"file\" : \"Main.java\",\n" +
>> +            "    \"line\" : 29\n" +
>> +            "  }\n" +
>> +            "}";
>> +
>> +    @Before
>> +    public void setup() {
>> +        parser = new JsonLogEventParser();
>> +    }
>> +
>> +    @Test
>> +    public void testString() throws ParseException {
>> +        LogEvent logEvent = parser.parseFrom(JSON);
>> +        assertNotNull(logEvent);
>> +        assertEquals("HelloWorld", logEvent.getLoggerName());
>> +        // TODO assert more here
>> +    }
>> +
>> +    @Test(expected = ParseException.class)
>> +    public void testStringEmpty() throws ParseException {
>> +        parser.parseFrom("");
>> +    }
>> +
>> +    @Test(expected = ParseException.class)
>> +    public void testStringInvalidJson() throws ParseException {
>> +        parser.parseFrom("foobar");
>> +    }
>> +
>> +    @Test(expected = ParseException.class)
>> +    public void testStringInvalidProperty() throws ParseException {
>> +        parser.parseFrom("{\"foo\":\"bar\"}");
>> +    }
>> +
>> +}
>>
>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
>> c4814a87/src/changes/changes.xml
>> ----------------------------------------------------------------------
>> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>> index 074e31c..522f49b 100644
>> --- a/src/changes/changes.xml
>> +++ b/src/changes/changes.xml
>> @@ -31,6 +31,9 @@
>>            - "remove" - Removed
>>       -->
>>       <release version="2.9.0" date="2017-MM-DD" description="GA Release
>> 2.9.0">
>> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
>> +        Public API for parsing the output from
>> JsonLayout/XmlLayout/YamlLayout
>> into a LogEvent.
>> +      </action>
>>         <action issue="LOG4J2-1984" dev="rgoers" type="update">
>>           Allow maxLength of StructuredData to be specified by the user.
>>         </action>
>>
>>
>

Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Mikael Ståldal <mi...@apache.org>.
Do you have another suggestion?

On 2017-07-23 05:01, Gary Gregory wrote:
> -1 to throw new RuntimeException(e)
> 
> Gary
> 
> On Jul 22, 2017 13:48, <mi...@apache.org> wrote:
> 
> Repository: logging-log4j2
> Updated Branches:
>    refs/heads/LOG4J2-1986 [created] c4814a873
> 
> 
> LOG4J2-1986 Public API for parsing the output from
> JsonLayout/XmlLayout/YamlLayout
> into a LogEvent
> 
> 
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> commit/c4814a87
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87
> 
> Branch: refs/heads/LOG4J2-1986
> Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
> Parents: eea873e
> Author: Mikael Ståldal <mi...@staldal.nu>
> Authored: Sat Jul 22 22:48:24 2017 +0200
> Committer: Mikael Ståldal <mi...@staldal.nu>
> Committed: Sat Jul 22 22:48:24 2017 +0200
> 
> ----------------------------------------------------------------------
>   .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
>   .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
>   .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
>   .../log4j/core/parser/ParseException.java       |  34 ++++++
>   .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
>   .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
>   .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
>   .../logging/log4j/core/parser/package-info.java |  20 ++++
>   .../core/parser/JsonLogEventParserTest.java     | 103 +++++++++++++++++++
>   src/changes/changes.xml                         |   3 +
>   10 files changed, 456 insertions(+)
> ----------------------------------------------------------------------
> 
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
> org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
> new file mode 100644
> index 0000000..8b00e57
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> AbstractJacksonLogEventParser.java
> @@ -0,0 +1,85 @@
> +/*
> + * 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.parser;
> +
> +import com.fasterxml.jackson.core.JsonProcessingException;
> +import com.fasterxml.jackson.databind.ObjectReader;
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +import java.io.Reader;
> +
> +class AbstractJacksonLogEventParser implements TextLogEventParser {
> +    final ObjectReader objectReader;
> +
> +    AbstractJacksonLogEventParser(ObjectReader objectReader) {
> +        this.objectReader = objectReader;
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(InputStream input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(Reader input) throws IOException,
> ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(String input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input) throws ParseException {
> +        try {
> +            return objectReader.readValue(input);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +
> +    @Override
> +    public LogEvent parseFrom(byte[] input, int offset, int length) throws
> ParseException {
> +        try {
> +            return objectReader.readValue(input, offset, length);
> +        } catch (JsonProcessingException e) {
> +            throw new ParseException(e);
> +        } catch (IOException e) {
> +            throw new RuntimeException(e);
> +        }
> +    }
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> new file mode 100644
> index 0000000..218de0b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
> +
> +/**
> + * Parse the output from JsonLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class JsonLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public JsonLogEventParser() {
> +        super(new Log4jJsonObjectMapper().readerFor(Log4jLogEvent.class));
> +    }
> +
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/LogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> new file mode 100644
> index 0000000..88e84fb
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/LogEventParser.java
> @@ -0,0 +1,64 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +
> +/**
> + * Parse the output from a layout into instances of {@link LogEvent}.
> + */
> +public interface LogEventParser {
> +
> +    /**
> +     * Parse from an InputStream. The input stream may contain multiple
> log events.
> +     * This method will read one log event and leave the stream open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the input stream
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(InputStream input) throws IOException,
> ParseException;
> +
> +    /**
> +     * Parse from a byte array, which is expected to contain exactly one
> log event.
> +     *
> +     * @param input  the byte array
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input) throws ParseException;
> +
> +    /**
> +     * Parse from a specified range of a byte array. The specified range
> is expected to contain
> +     * exactly one log event.
> +     *
> +     * @param input  the byte array
> +     * @param offset  the initial offset
> +     * @param length  the length
> +     *
> +     * @return the parsed LogEvent, never {@literal null}.
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(byte[] input, int offset, int length) throws
> ParseException;
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/ParseException.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> new file mode 100644
> index 0000000..8f5fa54
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/ParseException.java
> @@ -0,0 +1,34 @@
> +/*
> + * 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.parser;
> +
> +/**
> + * Thrown when the input cannot be parsed.
> + */
> +public class ParseException extends Exception {
> +    public ParseException(String message) {
> +        super(message);
> +    }
> +
> +    public ParseException(String message, Throwable cause) {
> +        super(message, cause);
> +    }
> +
> +    public ParseException(Throwable cause) {
> +        super(cause);
> +    }
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> TextLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> new file mode 100644
> index 0000000..3428024
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/TextLogEventParser.java
> @@ -0,0 +1,50 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +
> +import java.io.IOException;
> +import java.io.Reader;
> +
> +/**
> + * Parse the output from a text based layout into instances of {@link
> LogEvent}.
> + */
> +public interface TextLogEventParser extends LogEventParser {
> +    /**
> +     * Parse from a Reader. The reader may contain multiple log events.
> +     * This method will read one log event and leave the reader open and
> positioned to read the
> +     * next log event.
> +     *
> +     * @param input  the reader
> +     *
> +     * @return the parsed LogEvent, or {@literal null} of end of input is
> reached.
> +     * @throws IOException if unable to read from the input
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(Reader input) throws IOException, ParseException;
> +
> +    /**
> +     * Parse from a String, which is expected to contain exactly one log
> event.
> +     *
> +     * @param input  the string
> +     *
> +     * @return the parsed LogEvent
> +     * @throws ParseException if the input is malformed and cannot be
> parsed as a LogEvent
> +     */
> +    LogEvent parseFrom(String input) throws ParseException;
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/XmlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> new file mode 100644
> index 0000000..597e70b
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/XmlLogEventParser.java
> @@ -0,0 +1,32 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +
> +/**
> + * Parse the output from XmlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class XmlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public XmlLogEventParser() {
> +        super(new Log4jXmlObjectMapper().readerFor(Log4jLogEvent.class));
> +    }
> +
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
> YamlLogEventParser.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> new file mode 100644
> index 0000000..59b3463
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/YamlLogEventParser.java
> @@ -0,0 +1,33 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.apache.logging.log4j.core.impl.Log4jLogEvent;
> +import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
> +import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
> +
> +/**
> + * Parse the output from YamlLayout layout into instances of {@link
> LogEvent}.
> + */
> +public class YamlLogEventParser extends AbstractJacksonLogEventParser {
> +
> +    public YamlLogEventParser() {
> +        super(new Log4jYamlObjectMapper().readerFor(Log4jLogEvent.class));
> +    }
> +
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
> core/parser/package-info.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> new file mode 100644
> index 0000000..3506570
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> parser/package-info.java
> @@ -0,0 +1,20 @@
> +/*
> + * 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.
> + */
> +/**
> + * Parsers for the output of various layouts.
> + */
> +package org.apache.logging.log4j.core.parser;
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
> JsonLogEventParserTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
> org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
> new file mode 100644
> index 0000000..5684602
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> parser/JsonLogEventParserTest.java
> @@ -0,0 +1,103 @@
> +/*
> + * 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.parser;
> +
> +import org.apache.logging.log4j.core.LogEvent;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import static org.junit.Assert.assertEquals;
> +import static org.junit.Assert.assertNotNull;
> +
> +public class JsonLogEventParserTest {
> +
> +    private JsonLogEventParser parser;
> +
> +    private static final String JSON = "{\n" +
> +            "  \"timeMillis\" : 1493121664118,\n" +
> +            "  \"thread\" : \"main\",\n" +
> +            "  \"level\" : \"INFO\",\n" +
> +            "  \"loggerName\" : \"HelloWorld\",\n" +
> +            "  \"marker\" : {\n" +
> +            "    \"name\" : \"child\",\n" +
> +            "    \"parents\" : [ {\n" +
> +            "      \"name\" : \"parent\",\n" +
> +            "      \"parents\" : [ {\n" +
> +            "        \"name\" : \"grandparent\"\n" +
> +            "      } ]\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"message\" : \"Hello, world!\",\n" +
> +            "  \"thrown\" : {\n" +
> +            "    \"commonElementCount\" : 0,\n" +
> +            "    \"name\" : \"java.lang.RuntimeException\",\n" +
> +            "    \"extendedStackTrace\" : [ {\n" +
> +            "      \"class\" : \"logtest.Main\",\n" +
> +            "      \"method\" : \"main\",\n" +
> +            "      \"file\" : \"Main.java\",\n" +
> +            "      \"line\" : 29,\n" +
> +            "      \"exact\" : true,\n" +
> +            "      \"location\" : \"classes/\",\n" +
> +            "      \"version\" : \"?\"\n" +
> +            "    } ]\n" +
> +            "  },\n" +
> +            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
> +            "  \"endOfBatch\" : false,\n" +
> +            "  \"loggerFqcn\" :
> \"org.apache.logging.log4j.spi.AbstractLogger\",\n"
> +
> +            "  \"contextMap\" : {\n" +
> +            "    \"bar\" : \"BAR\",\n" +
> +            "    \"foo\" : \"FOO\"\n" +
> +            "  },\n" +
> +            "  \"threadId\" : 1,\n" +
> +            "  \"threadPriority\" : 5,\n" +
> +            "  \"source\" : {\n" +
> +            "    \"class\" : \"logtest.Main\",\n" +
> +            "    \"method\" : \"main\",\n" +
> +            "    \"file\" : \"Main.java\",\n" +
> +            "    \"line\" : 29\n" +
> +            "  }\n" +
> +            "}";
> +
> +    @Before
> +    public void setup() {
> +        parser = new JsonLogEventParser();
> +    }
> +
> +    @Test
> +    public void testString() throws ParseException {
> +        LogEvent logEvent = parser.parseFrom(JSON);
> +        assertNotNull(logEvent);
> +        assertEquals("HelloWorld", logEvent.getLoggerName());
> +        // TODO assert more here
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringEmpty() throws ParseException {
> +        parser.parseFrom("");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidJson() throws ParseException {
> +        parser.parseFrom("foobar");
> +    }
> +
> +    @Test(expected = ParseException.class)
> +    public void testStringInvalidProperty() throws ParseException {
> +        parser.parseFrom("{\"foo\":\"bar\"}");
> +    }
> +
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> c4814a87/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index 074e31c..522f49b 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -31,6 +31,9 @@
>            - "remove" - Removed
>       -->
>       <release version="2.9.0" date="2017-MM-DD" description="GA Release
> 2.9.0">
> +      <action issue="LOG4J2-1986" dev="mikes" type="add">
> +        Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
> into a LogEvent.
> +      </action>
>         <action issue="LOG4J2-1984" dev="rgoers" type="update">
>           Allow maxLength of StructuredData to be specified by the user.
>         </action>
> 


Re: logging-log4j2 git commit: LOG4J2-1986 Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent

Posted by Gary Gregory <ga...@gmail.com>.
-1 to throw new RuntimeException(e)

Gary

On Jul 22, 2017 13:48, <mi...@apache.org> wrote:

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1986 [created] c4814a873


LOG4J2-1986 Public API for parsing the output from
JsonLayout/XmlLayout/YamlLayout
into a LogEvent


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
commit/c4814a87
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4814a87
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4814a87

Branch: refs/heads/LOG4J2-1986
Commit: c4814a8730eb24bdf89cf23c89b4a957b8418ba5
Parents: eea873e
Author: Mikael Ståldal <mi...@staldal.nu>
Authored: Sat Jul 22 22:48:24 2017 +0200
Committer: Mikael Ståldal <mi...@staldal.nu>
Committed: Sat Jul 22 22:48:24 2017 +0200

----------------------------------------------------------------------
 .../parser/AbstractJacksonLogEventParser.java   |  85 +++++++++++++++
 .../log4j/core/parser/JsonLogEventParser.java   |  32 ++++++
 .../log4j/core/parser/LogEventParser.java       |  64 ++++++++++++
 .../log4j/core/parser/ParseException.java       |  34 ++++++
 .../log4j/core/parser/TextLogEventParser.java   |  50 +++++++++
 .../log4j/core/parser/XmlLogEventParser.java    |  32 ++++++
 .../log4j/core/parser/YamlLogEventParser.java   |  33 ++++++
 .../logging/log4j/core/parser/package-info.java |  20 ++++
 .../core/parser/JsonLogEventParserTest.java     | 103 +++++++++++++++++++
 src/changes/changes.xml                         |   3 +
 10 files changed, 456 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
AbstractJacksonLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/
org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java
new file mode 100644
index 0000000..8b00e57
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
AbstractJacksonLogEventParser.java
@@ -0,0 +1,85 @@
+/*
+ * 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.parser;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectReader;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+class AbstractJacksonLogEventParser implements TextLogEventParser {
+    final ObjectReader objectReader;
+
+    AbstractJacksonLogEventParser(ObjectReader objectReader) {
+        this.objectReader = objectReader;
+    }
+
+    @Override
+    public LogEvent parseFrom(InputStream input) throws IOException,
ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(Reader input) throws IOException,
ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(String input) throws ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(byte[] input) throws ParseException {
+        try {
+            return objectReader.readValue(input);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public LogEvent parseFrom(byte[] input, int offset, int length) throws
ParseException {
+        try {
+            return objectReader.readValue(input, offset, length);
+        } catch (JsonProcessingException e) {
+            throw new ParseException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
JsonLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/JsonLogEventParser.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/JsonLogEventParser.java
new file mode 100644
index 0000000..218de0b
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/JsonLogEventParser.java
@@ -0,0 +1,32 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+
+/**
+ * Parse the output from JsonLayout layout into instances of {@link
LogEvent}.
+ */
+public class JsonLogEventParser extends AbstractJacksonLogEventParser {
+
+    public JsonLogEventParser() {
+        super(new Log4jJsonObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
core/parser/LogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/LogEventParser.java
new file mode 100644
index 0000000..88e84fb
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/LogEventParser.java
@@ -0,0 +1,64 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Parse the output from a layout into instances of {@link LogEvent}.
+ */
+public interface LogEventParser {
+
+    /**
+     * Parse from an InputStream. The input stream may contain multiple
log events.
+     * This method will read one log event and leave the stream open and
positioned to read the
+     * next log event.
+     *
+     * @param input  the input stream
+     *
+     * @return the parsed LogEvent, or {@literal null} of end of input is
reached.
+     * @throws IOException if unable to read from the input
+     * @throws ParseException if the input is malformed and cannot be
parsed as a LogEvent
+     */
+    LogEvent parseFrom(InputStream input) throws IOException,
ParseException;
+
+    /**
+     * Parse from a byte array, which is expected to contain exactly one
log event.
+     *
+     * @param input  the byte array
+     *
+     * @return the parsed LogEvent, never {@literal null}.
+     * @throws ParseException if the input is malformed and cannot be
parsed as a LogEvent
+     */
+    LogEvent parseFrom(byte[] input) throws ParseException;
+
+    /**
+     * Parse from a specified range of a byte array. The specified range
is expected to contain
+     * exactly one log event.
+     *
+     * @param input  the byte array
+     * @param offset  the initial offset
+     * @param length  the length
+     *
+     * @return the parsed LogEvent, never {@literal null}.
+     * @throws ParseException if the input is malformed and cannot be
parsed as a LogEvent
+     */
+    LogEvent parseFrom(byte[] input, int offset, int length) throws
ParseException;
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
core/parser/ParseException.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/ParseException.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/ParseException.java
new file mode 100644
index 0000000..8f5fa54
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/ParseException.java
@@ -0,0 +1,34 @@
+/*
+ * 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.parser;
+
+/**
+ * Thrown when the input cannot be parsed.
+ */
+public class ParseException extends Exception {
+    public ParseException(String message) {
+        super(message);
+    }
+
+    public ParseException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public ParseException(Throwable cause) {
+        super(cause);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
TextLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/TextLogEventParser.java
new file mode 100644
index 0000000..3428024
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/TextLogEventParser.java
@@ -0,0 +1,50 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Parse the output from a text based layout into instances of {@link
LogEvent}.
+ */
+public interface TextLogEventParser extends LogEventParser {
+    /**
+     * Parse from a Reader. The reader may contain multiple log events.
+     * This method will read one log event and leave the reader open and
positioned to read the
+     * next log event.
+     *
+     * @param input  the reader
+     *
+     * @return the parsed LogEvent, or {@literal null} of end of input is
reached.
+     * @throws IOException if unable to read from the input
+     * @throws ParseException if the input is malformed and cannot be
parsed as a LogEvent
+     */
+    LogEvent parseFrom(Reader input) throws IOException, ParseException;
+
+    /**
+     * Parse from a String, which is expected to contain exactly one log
event.
+     *
+     * @param input  the string
+     *
+     * @return the parsed LogEvent
+     * @throws ParseException if the input is malformed and cannot be
parsed as a LogEvent
+     */
+    LogEvent parseFrom(String input) throws ParseException;
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
core/parser/XmlLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/XmlLogEventParser.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/XmlLogEventParser.java
new file mode 100644
index 0000000..597e70b
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/XmlLogEventParser.java
@@ -0,0 +1,32 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
+
+/**
+ * Parse the output from XmlLayout layout into instances of {@link
LogEvent}.
+ */
+public class XmlLogEventParser extends AbstractJacksonLogEventParser {
+
+    public XmlLogEventParser() {
+        super(new Log4jXmlObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/
YamlLogEventParser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/YamlLogEventParser.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/YamlLogEventParser.java
new file mode 100644
index 0000000..59b3463
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/YamlLogEventParser.java
@@ -0,0 +1,33 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
+import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
+
+/**
+ * Parse the output from YamlLayout layout into instances of {@link
LogEvent}.
+ */
+public class YamlLogEventParser extends AbstractJacksonLogEventParser {
+
+    public YamlLogEventParser() {
+        super(new Log4jYamlObjectMapper().readerFor(Log4jLogEvent.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/main/java/org/apache/logging/log4j/
core/parser/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/package-info.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/package-info.java
new file mode 100644
index 0000000..3506570
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
parser/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * Parsers for the output of various layouts.
+ */
+package org.apache.logging.log4j.core.parser;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/
JsonLogEventParserTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
parser/JsonLogEventParserTest.java b/log4j-core/src/test/java/
org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
new file mode 100644
index 0000000..5684602
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
parser/JsonLogEventParserTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.parser;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class JsonLogEventParserTest {
+
+    private JsonLogEventParser parser;
+
+    private static final String JSON = "{\n" +
+            "  \"timeMillis\" : 1493121664118,\n" +
+            "  \"thread\" : \"main\",\n" +
+            "  \"level\" : \"INFO\",\n" +
+            "  \"loggerName\" : \"HelloWorld\",\n" +
+            "  \"marker\" : {\n" +
+            "    \"name\" : \"child\",\n" +
+            "    \"parents\" : [ {\n" +
+            "      \"name\" : \"parent\",\n" +
+            "      \"parents\" : [ {\n" +
+            "        \"name\" : \"grandparent\"\n" +
+            "      } ]\n" +
+            "    } ]\n" +
+            "  },\n" +
+            "  \"message\" : \"Hello, world!\",\n" +
+            "  \"thrown\" : {\n" +
+            "    \"commonElementCount\" : 0,\n" +
+            "    \"name\" : \"java.lang.RuntimeException\",\n" +
+            "    \"extendedStackTrace\" : [ {\n" +
+            "      \"class\" : \"logtest.Main\",\n" +
+            "      \"method\" : \"main\",\n" +
+            "      \"file\" : \"Main.java\",\n" +
+            "      \"line\" : 29,\n" +
+            "      \"exact\" : true,\n" +
+            "      \"location\" : \"classes/\",\n" +
+            "      \"version\" : \"?\"\n" +
+            "    } ]\n" +
+            "  },\n" +
+            "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
+            "  \"endOfBatch\" : false,\n" +
+            "  \"loggerFqcn\" :
\"org.apache.logging.log4j.spi.AbstractLogger\",\n"
+
+            "  \"contextMap\" : {\n" +
+            "    \"bar\" : \"BAR\",\n" +
+            "    \"foo\" : \"FOO\"\n" +
+            "  },\n" +
+            "  \"threadId\" : 1,\n" +
+            "  \"threadPriority\" : 5,\n" +
+            "  \"source\" : {\n" +
+            "    \"class\" : \"logtest.Main\",\n" +
+            "    \"method\" : \"main\",\n" +
+            "    \"file\" : \"Main.java\",\n" +
+            "    \"line\" : 29\n" +
+            "  }\n" +
+            "}";
+
+    @Before
+    public void setup() {
+        parser = new JsonLogEventParser();
+    }
+
+    @Test
+    public void testString() throws ParseException {
+        LogEvent logEvent = parser.parseFrom(JSON);
+        assertNotNull(logEvent);
+        assertEquals("HelloWorld", logEvent.getLoggerName());
+        // TODO assert more here
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringEmpty() throws ParseException {
+        parser.parseFrom("");
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringInvalidJson() throws ParseException {
+        parser.parseFrom("foobar");
+    }
+
+    @Test(expected = ParseException.class)
+    public void testStringInvalidProperty() throws ParseException {
+        parser.parseFrom("{\"foo\":\"bar\"}");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
c4814a87/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 074e31c..522f49b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
          - "remove" - Removed
     -->
     <release version="2.9.0" date="2017-MM-DD" description="GA Release
2.9.0">
+      <action issue="LOG4J2-1986" dev="mikes" type="add">
+        Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout
into a LogEvent.
+      </action>
       <action issue="LOG4J2-1984" dev="rgoers" type="update">
         Allow maxLength of StructuredData to be specified by the user.
       </action>