Nothing wrong with making more custom exceptions. On 23 July 2017 at 16:46, Gary Gregory <[email protected]> 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 <[email protected]> > wrote: > > > OK, but RuntimeException should still never be thrown here... > > > > Gary > > > > On Sun, Jul 23, 2017 at 1:02 PM, Mikael Ståldal <[email protected]> > 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 <[email protected]> > >>> 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, <[email protected]> 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 <[email protected]> > >>>>> Authored: Sat Jul 22 22:48:24 2017 +0200 > >>>>> Committer: Mikael Ståldal <[email protected]> > >>>>> 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 <[email protected]>
