This is an automated email from the ASF dual-hosted git repository. vy pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 5f043f6c833986342a2ab73a57787676f7d1b7ce Author: Fabricc <[email protected]> AuthorDate: Wed Feb 3 09:41:48 2021 +0100 LOG4J2-2973 Rename EventTemplateAdditionalField#type (conflicting with properties file parser) to #format. (#462) --- .../layout/template/json/JsonTemplateLayout.java | 24 ++++++------- .../template/json/resolver/TemplateResolvers.java | 6 ++-- ...onTemplateLayoutAdditionalFieldTestHelper.java} | 34 +++--------------- ...emplateLayoutAdditionalFieldTestProperties.java | 34 ++++++++++++++++++ .../JsonTemplateLayoutAdditionalFieldTestXML.java | 33 ++++++++++++++++++ .../JsonTemplateLayoutAdditionalFieldTestYAML.java | 34 ++++++++++++++++++ .../template/json/JsonTemplateLayoutTest.java | 4 +-- ...eldEnrichedJsonTemplateLayoutLogging.properties | 40 ++++++++++++++++++++++ ...ionalFieldEnrichedJsonTemplateLayoutLogging.xml | 6 ++-- ...onalFieldEnrichedJsonTemplateLayoutLogging.yaml | 39 +++++++++++++++++++++ src/changes/changes.xml | 3 ++ ...layout.adoc.vm => json-template-layout.vm.adoc} | 26 +++++++++++--- 12 files changed, 229 insertions(+), 54 deletions(-) diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java index 1ef2b13..b0a2b66 100644 --- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java +++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java @@ -531,18 +531,18 @@ public class JsonTemplateLayout implements StringLayout { printObject = true) public static final class EventTemplateAdditionalField { - public enum Type { STRING, JSON } + public enum Format { STRING, JSON } private final String key; private final String value; - private final Type type; + private final Format format; private EventTemplateAdditionalField(final Builder builder) { this.key = builder.key; this.value = builder.value; - this.type = builder.type; + this.format = builder.format; } public String getKey() { @@ -553,8 +553,8 @@ public class JsonTemplateLayout implements StringLayout { return value; } - public Type getType() { - return type; + public Format getFormat() { + return format; } @Override @@ -568,17 +568,17 @@ public class JsonTemplateLayout implements StringLayout { EventTemplateAdditionalField that = (EventTemplateAdditionalField) object; return key.equals(that.key) && value.equals(that.value) && - type == that.type; + format == that.format; } @Override public int hashCode() { - return Objects.hash(key, value, type); + return Objects.hash(key, value, format); } @Override public String toString() { - final String formattedValue = Type.STRING.equals(type) + final String formattedValue = Format.STRING.equals(format) ? String.format("\"%s\"", value) : value; return String.format("%s=%s", key, formattedValue); @@ -599,7 +599,7 @@ public class JsonTemplateLayout implements StringLayout { private String value; @PluginBuilderAttribute - private Type type = Type.STRING; + private Format format = Format.STRING; public Builder setKey(final String key) { this.key = key; @@ -611,8 +611,8 @@ public class JsonTemplateLayout implements StringLayout { return this; } - public Builder setType(final Type type) { - this.type = type; + public Builder setFormat(final Format format) { + this.format = format; return this; } @@ -629,7 +629,7 @@ public class JsonTemplateLayout implements StringLayout { if (Strings.isBlank(value)) { throw new IllegalArgumentException("blank value"); } - Objects.requireNonNull(type, "type"); + Objects.requireNonNull(format, "format"); } } diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolvers.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolvers.java index d713edd..b0cede2 100644 --- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolvers.java +++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolvers.java @@ -129,7 +129,7 @@ public final class TemplateResolvers { for (final EventTemplateAdditionalField additionalField : additionalFields) { final String additionalFieldKey = additionalField.getKey(); final Object additionalFieldValue; - switch (additionalField.getType()) { + switch (additionalField.getFormat()) { case STRING: additionalFieldValue = additionalField.getValue(); break; @@ -145,8 +145,8 @@ public final class TemplateResolvers { break; default: { final String message = String.format( - "unknown type %s for additional field: %s", - additionalFieldKey, additionalField.getType()); + "unknown format %s for additional field: %s", + additionalFieldKey, additionalField.getFormat()); throw new IllegalArgumentException(message); } } diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTest.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestHelper.java similarity index 54% rename from log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTest.java rename to log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestHelper.java index 465bcc6..605cc3a 100644 --- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTest.java +++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestHelper.java @@ -1,47 +1,23 @@ -/* - * 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.layout.template.json; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.junit.LoggerContextSource; -import org.apache.logging.log4j.junit.Named; import org.apache.logging.log4j.layout.template.json.util.JsonReader; import org.apache.logging.log4j.test.appender.ListAppender; import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; -@LoggerContextSource("additionalFieldEnrichedJsonTemplateLayoutLogging.xml") -class JsonTemplateLayoutAdditionalFieldTest { - - @Test - void test_additional_fields_are_resolved( - final LoggerContext loggerContext, - @Named("List") final ListAppender appender) { +class JsonTemplateLayoutAdditionalFieldTestHelper { + static void assertAdditionalFields(final LoggerContext loggerContext, ListAppender appender) { // Log an event. final Logger logger = loggerContext.getLogger( - JsonTemplateLayoutAdditionalFieldTest.class); + JsonTemplateLayoutAdditionalFieldTestHelper.class); logger.info("trigger"); // Verify that the appender has logged the event. @@ -56,8 +32,7 @@ class JsonTemplateLayoutAdditionalFieldTest { JsonTemplateLayoutDefaults.getCharset()); final Object serializedEventObject = JsonReader.read(serializedEventJson); Assertions.assertThat(serializedEventObject).isInstanceOf(Map.class); - @SuppressWarnings("unchecked") - final Map<String, Object> serializedEventMap = + @SuppressWarnings("unchecked") final Map<String, Object> serializedEventMap = (Map<String, Object>) serializedEventObject; // Verify the serialized additional fields. @@ -69,5 +44,4 @@ class JsonTemplateLayoutAdditionalFieldTest { .containsEntry("listField", Arrays.asList(1, "two")); } - } diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestProperties.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestProperties.java new file mode 100644 index 0000000..5c3911e --- /dev/null +++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestProperties.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.layout.template.json; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.junit.LoggerContextSource; +import org.apache.logging.log4j.junit.Named; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.jupiter.api.Test; + +@LoggerContextSource("additionalFieldEnrichedJsonTemplateLayoutLogging.properties") +class JsonTemplateLayoutAdditionalFieldTestProperties { + + @Test + void test_additional_fields_are_resolved( + final LoggerContext loggerContext, + final @Named(value = "List") ListAppender appender) { + JsonTemplateLayoutAdditionalFieldTestHelper.assertAdditionalFields(loggerContext, appender); + } +} diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestXML.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestXML.java new file mode 100644 index 0000000..d96da36 --- /dev/null +++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestXML.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.layout.template.json; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.junit.LoggerContextSource; +import org.apache.logging.log4j.junit.Named; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.jupiter.api.Test; + +@LoggerContextSource("additionalFieldEnrichedJsonTemplateLayoutLogging.xml") +class JsonTemplateLayoutAdditionalFieldTestXML { + @Test + void test_additional_fields_are_resolved( + final LoggerContext loggerContext, + final @Named(value = "List") ListAppender appender) { + JsonTemplateLayoutAdditionalFieldTestHelper.assertAdditionalFields(loggerContext, appender); + } +} diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestYAML.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestYAML.java new file mode 100644 index 0000000..204ead8 --- /dev/null +++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutAdditionalFieldTestYAML.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.layout.template.json; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.junit.LoggerContextSource; +import org.apache.logging.log4j.junit.Named; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.jupiter.api.Test; + +@LoggerContextSource("additionalFieldEnrichedJsonTemplateLayoutLogging.yaml") +class JsonTemplateLayoutAdditionalFieldTestYAML { + + @Test + void test_additional_fields_are_resolved( + final LoggerContext loggerContext, + final @Named(value = "List") ListAppender appender) { + JsonTemplateLayoutAdditionalFieldTestHelper.assertAdditionalFields(loggerContext, appender); + } +} diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutTest.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutTest.java index f0d5b6c..24c2040 100644 --- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutTest.java +++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutTest.java @@ -1359,7 +1359,7 @@ class JsonTemplateLayoutTest { .newBuilder() .setKey("number") .setValue("1") - .setType(EventTemplateAdditionalField.Type.JSON) + .setFormat(EventTemplateAdditionalField.Format.JSON) .build(), EventTemplateAdditionalField .newBuilder() @@ -1370,7 +1370,7 @@ class JsonTemplateLayoutTest { .newBuilder() .setKey("level") .setValue("{\"$resolver\": \"level\", \"field\": \"name\"}") - .setType(EventTemplateAdditionalField.Type.JSON) + .setFormat(EventTemplateAdditionalField.Format.JSON) .build() }; final JsonTemplateLayout layout = JsonTemplateLayout diff --git a/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.properties b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.properties new file mode 100644 index 0000000..58e65ba --- /dev/null +++ b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.properties @@ -0,0 +1,40 @@ +# 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. + +appender.list.name = List +appender.list.type = List +appender.list.raw = true +appender.list.layout.type = JsonTemplateLayout +appender.list.layout.charset = UTF-8 +appender.list.layout.extra[0].key = stringField +appender.list.layout.extra[0].value = string +appender.list.layout.extra[0].type = EventTemplateAdditionalField +appender.list.layout.extra[0].format = STRING +appender.list.layout.extra[1].key = numberField +appender.list.layout.extra[1].value = 1 +appender.list.layout.extra[1].format = JSON +appender.list.layout.extra[1].type = EventTemplateAdditionalField +appender.list.layout.extra[2].key = objectField +appender.list.layout.extra[2].value = {"numberField":1} +appender.list.layout.extra[2].format = JSON +appender.list.layout.extra[2].type = EventTemplateAdditionalField +appender.list.layout.extra[3].key = listField +appender.list.layout.extra[3].value = [1, "two"] +appender.list.layout.extra[3].format = JSON +appender.list.layout.extra[3].type = EventTemplateAdditionalField + +rootLogger.level = info +rootLogger.appenderRef.stdout.ref = List + diff --git a/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.xml b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.xml index 3449c6d..b27bfde 100644 --- a/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.xml +++ b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.xml @@ -20,9 +20,9 @@ <List name="List" raw="true"> <JsonTemplateLayout> <EventTemplateAdditionalField key="stringField" value="string"/> - <EventTemplateAdditionalField key="numberField" value="1" type="JSON"/> - <EventTemplateAdditionalField key="objectField" value='{"numberField":1}' type="JSON"/> - <EventTemplateAdditionalField key="listField" value='[1, "two"]' type="JSON"/> + <EventTemplateAdditionalField key="numberField" value="1" format="JSON"/> + <EventTemplateAdditionalField key="objectField" value='{"numberField":1}' format="JSON"/> + <EventTemplateAdditionalField key="listField" value='[1, "two"]' format="JSON"/> </JsonTemplateLayout> </List> </Appenders> diff --git a/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.yaml b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.yaml new file mode 100644 index 0000000..676b3ce --- /dev/null +++ b/log4j-layout-template-json/src/test/resources/additionalFieldEnrichedJsonTemplateLayoutLogging.yaml @@ -0,0 +1,39 @@ +# 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. + +Configuration: + status: OFF + appenders: + List: + name: "List" + raw: "true" + JsonTemplateLayout: + EventTemplateadditionalField: + - key: "stringField" + value: "string" + - key: "numberField" + value: "1" + format: "JSON" + - key: "objectField" + value: '{"numberField":1}' + format: "JSON" + - key: "listField" + value: '[1, "two"]' + format: "JSON" + loggers: + root: + level: "trace" + AppenderRef: + ref: List diff --git a/src/changes/changes.xml b/src/changes/changes.xml index af502dc..89a3715 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -34,6 +34,9 @@ <action issue="LOG4J2-2998" dev="vy" type="fix"> Fix truncation of excessive strings ending with a high surrogate in JsonWriter. </action> + <action issue="LOG4J2-2973" dev="fricchiuti" type="fix"> + Rename EventTemplateAdditionalField#type (conflicting with properties file parser) to "format". + </action> <action issue="LOG4J2-2972" dev="vy" type="fix"> Refactor AsyncAppender and AppenderControl for handling of Throwables. </action> diff --git a/src/site/asciidoc/manual/json-template-layout.adoc.vm b/src/site/asciidoc/manual/json-template-layout.vm.adoc similarity index 97% rename from src/site/asciidoc/manual/json-template-layout.adoc.vm rename to src/site/asciidoc/manual/json-template-layout.vm.adoc index fdc6534..d3f700c 100644 --- a/src/site/asciidoc/manual/json-template-layout.adoc.vm +++ b/src/site/asciidoc/manual/json-template-layout.vm.adoc @@ -256,25 +256,43 @@ custom fields: </JsonTemplateLayout> ---- -The default `type` for the added new fields are `String`. +The default `format` for the added new fields are `String`. One can also pass JSON literals into additional fields: [source,xml] ---- <EventTemplateAdditionalField key="marker" - type="JSON" + format="JSON" value='{"$resolver": "marker", "field": "name"}'/> <EventTemplateAdditionalField key="aNumber" - type="JSON" + format="JSON" value="1"/> <EventTemplateAdditionalField key="aList" - type="JSON" + format="JSON" value='[1,2,"string"]'/> ---- +When using a configuration file with a `.properties` extension, additional fields can be added as follows: + +[source, properties] +---- +appender.console.layout.extra[0].key = marker +appender.console.layout.extra[0].value = {"$resolver": "marker", "field": "name"} +appender.console.layout.extra[0].format = JSON +appender.console.layout.extra[0].type = EventTemplateAdditionalField +appender.console.layout.extra[1].key = aNumber +appender.console.layout.extra[1].value = 1 +appender.console.layout.extra[1].format = JSON +appender.console.layout.extra[1].type = EventTemplateAdditionalField +appender.console.layout.extra[2].key = aList +appender.console.layout.extra[2].value = [1,2,"string"] +appender.console.layout.extra[2].format = JSON +appender.console.layout.extra[2].type = EventTemplateAdditionalField +---- + [#recycling-strategy] === Recycling strategy
