You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2017/08/14 00:49:18 UTC
[1/2] logging-log4j2 git commit: LOG4J2-2008 - support multiple
structured data elements
Repository: logging-log4j2
Updated Branches:
refs/heads/master 4992db7be -> 1f15422a9
LOG4J2-2008 - support multiple structured data elements
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3df7f50e
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3df7f50e
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3df7f50e
Branch: refs/heads/master
Commit: 3df7f50e91c402cc38363762bc653744ad557e3b
Parents: 4992db7
Author: Ralph Goers <rg...@nextiva.com>
Authored: Sun Aug 13 17:47:42 2017 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Sun Aug 13 17:47:48 2017 -0700
----------------------------------------------------------------------
.../log4j/message/MessageCollectionMessage.java | 26 ++++++
.../StructuredDataCollectionMessage.java | 93 ++++++++++++++++++++
.../logging/log4j/util/ProcessIdUtil.java | 8 ++
.../log4j/core/layout/Rfc5424Layout.java | 44 +++++----
.../log4j/core/layout/Rfc5424LayoutTest.java | 71 +++++++++++++++
5 files changed, 226 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3df7f50e/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageCollectionMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageCollectionMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageCollectionMessage.java
new file mode 100644
index 0000000..19d083e
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/MessageCollectionMessage.java
@@ -0,0 +1,26 @@
+/*
+ * 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.message;
+
+/**
+ * A Message that is a collection of Messages.
+ * @param <T> The Message type.
+ */
+public interface MessageCollectionMessage<T> extends Message, Iterable<T> {
+
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3df7f50e/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataCollectionMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataCollectionMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataCollectionMessage.java
new file mode 100644
index 0000000..3199193
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataCollectionMessage.java
@@ -0,0 +1,93 @@
+/*
+ * 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.message;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * A collection of StructuredDataMessages.
+ */
+public class StructuredDataCollectionMessage implements MessageCollectionMessage<StructuredDataMessage> {
+
+ private List<StructuredDataMessage> structuredDataMessageList;
+
+ public StructuredDataCollectionMessage(List<StructuredDataMessage> messages) {
+ this.structuredDataMessageList = messages;
+ }
+
+ @Override
+ public Iterator<StructuredDataMessage> iterator() {
+ return structuredDataMessageList.iterator();
+ }
+
+ @Override
+ public String getFormattedMessage() {
+ StringBuilder sb = new StringBuilder();
+ for (StructuredDataMessage msg : structuredDataMessageList) {
+ sb.append(msg.getFormattedMessage());
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public String getFormat() {
+ StringBuilder sb = new StringBuilder();
+ for (StructuredDataMessage msg : structuredDataMessageList) {
+ if (msg.getFormat() != null) {
+ if (sb.length() > 0) {
+ sb.append(", ");
+ }
+ sb.append(msg.getFormat());
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public Object[] getParameters() {
+ List<Object[]> objectList = new ArrayList<>();
+ int count = 0;
+ for (StructuredDataMessage msg : structuredDataMessageList) {
+ Object[] objects = msg.getParameters();
+ if (objects != null) {
+ objectList.add(objects);
+ count += objects.length;
+ }
+ }
+ Object[] objects = new Object[count];
+ int index = 0;
+ for (Object[] objs : objectList) {
+ for (Object obj : objs) {
+ objects[index++] = obj;
+ }
+ }
+ return objects;
+ }
+
+ @Override
+ public Throwable getThrowable() {
+ for (StructuredDataMessage msg : structuredDataMessageList) {
+ Throwable t = msg.getThrowable();
+ if (t != null) {
+ return t;
+ }
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3df7f50e/log4j-api/src/main/java/org/apache/logging/log4j/util/ProcessIdUtil.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/ProcessIdUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/ProcessIdUtil.java
new file mode 100644
index 0000000..9852291
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/ProcessIdUtil.java
@@ -0,0 +1,8 @@
+package org.apache.logging.log4j.util;
+
+public class ProcessIdUtil {
+
+ public static String getProcessId() {
+ return "-";
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3df7f50e/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/Rfc5424Layout.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/Rfc5424Layout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/Rfc5424Layout.java
index 304e0a0..77466b1 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/Rfc5424Layout.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/Rfc5424Layout.java
@@ -51,8 +51,11 @@ import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.apache.logging.log4j.core.util.NetUtils;
import org.apache.logging.log4j.core.util.Patterns;
import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.MessageCollectionMessage;
+import org.apache.logging.log4j.message.StructuredDataCollectionMessage;
import org.apache.logging.log4j.message.StructuredDataId;
import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.apache.logging.log4j.util.ProcessIdUtil;
import org.apache.logging.log4j.util.StringBuilders;
import org.apache.logging.log4j.util.Strings;
@@ -190,8 +193,7 @@ public final class Rfc5424Layout extends AbstractStringLayout {
final String name = config == null ? null : config.getName();
configName = Strings.isNotEmpty(name) ? name : null;
this.fieldFormatters = createFieldFormatters(loggerFields, config);
- // TODO Java 9: ProcessHandle.current().getPid();
- this.procId = "-";
+ this.procId = ProcessIdUtil.getProcessId();
}
private Map<String, FieldFormatter> createFieldFormatters(final LoggerFields[] loggerFields,
@@ -331,8 +333,8 @@ public final class Rfc5424Layout extends AbstractStringLayout {
private void appendMessage(final StringBuilder buffer, final LogEvent event) {
final Message message = event.getMessage();
// This layout formats StructuredDataMessages instead of delegating to the Message itself.
- final String text = (message instanceof StructuredDataMessage) ? message.getFormat() : message
- .getFormattedMessage();
+ final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage)
+ ? message.getFormat() : message.getFormattedMessage();
if (text != null && text.length() > 0) {
buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
@@ -352,7 +354,8 @@ public final class Rfc5424Layout extends AbstractStringLayout {
private void appendStructuredElements(final StringBuilder buffer, final LogEvent event) {
final Message message = event.getMessage();
- final boolean isStructured = message instanceof StructuredDataMessage;
+ final boolean isStructured = message instanceof StructuredDataMessage ||
+ message instanceof StructuredDataCollectionMessage;
if (!isStructured && (fieldFormatters != null && fieldFormatters.isEmpty()) && !includeMdc) {
buffer.append('-');
@@ -387,18 +390,12 @@ public final class Rfc5424Layout extends AbstractStringLayout {
}
if (isStructured) {
- final StructuredDataMessage data = (StructuredDataMessage) message;
- final Map<String, String> map = data.getData();
- final StructuredDataId id = data.getId();
- final String sdId = getId(id);
-
- if (sdElements.containsKey(sdId)) {
- final StructuredDataElement union = sdElements.get(id.toString());
- union.union(map);
- sdElements.put(sdId, union);
+ if (message instanceof MessageCollectionMessage) {
+ for (StructuredDataMessage data : ((StructuredDataCollectionMessage)message)) {
+ addStructuredData(sdElements, data);
+ }
} else {
- final StructuredDataElement formattedData = new StructuredDataElement(map, eventPrefix, false);
- sdElements.put(sdId, formattedData);
+ addStructuredData(sdElements, (StructuredDataMessage) message);
}
}
@@ -412,6 +409,21 @@ public final class Rfc5424Layout extends AbstractStringLayout {
}
}
+ private void addStructuredData(final Map<String, StructuredDataElement> sdElements, final StructuredDataMessage data) {
+ final Map<String, String> map = data.getData();
+ final StructuredDataId id = data.getId();
+ final String sdId = getId(id);
+
+ if (sdElements.containsKey(sdId)) {
+ final StructuredDataElement union = sdElements.get(id.toString());
+ union.union(map);
+ sdElements.put(sdId, union);
+ } else {
+ final StructuredDataElement formattedData = new StructuredDataElement(map, eventPrefix, false);
+ sdElements.put(sdId, formattedData);
+ }
+ }
+
private String escapeNewlines(final String text, final String replacement) {
if (null == replacement) {
return text;
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3df7f50e/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/Rfc5424LayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/Rfc5424LayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/Rfc5424LayoutTest.java
index fbfd8f5..9c3792a 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/Rfc5424LayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/Rfc5424LayoutTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.layout;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@@ -31,6 +32,7 @@ import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.net.Facility;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.junit.ThreadContextRule;
+import org.apache.logging.log4j.message.StructuredDataCollectionMessage;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
@@ -58,6 +60,9 @@ public class Rfc5424LayoutTest {
private static final String lineEscaped4 =
"ATM - Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"]" +
"[RequestContext@3692 escaped=\"Testing escaping #012 \\\" \\] \\\"\" ipAddress=\"192.168.0.120\" loginId=\"JohnDoe\"] Transfer Complete";
+ private static final String collectionLine = "[Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" " +
+ "ToAccount=\"123456\"][Extra@18060 Item1=\"Hello\" Item2=\"World\"][RequestContext@3692 " +
+ "ipAddress=\"192.168.0.120\" loginId=\"JohnDoe\"] Transfer Complete";
static ConfigurationFactory cf = new BasicConfigurationFactory();
@@ -148,6 +153,72 @@ public class Rfc5424LayoutTest {
list = appender.getMessages();
assertTrue("No messages expected, found " + list.size(), list.isEmpty());
} finally {
+ ThreadContext.clearMap();
+ root.removeAppender(appender);
+ appender.stop();
+ }
+ }
+
+ /**
+ * Test case for MDC conversion pattern.
+ */
+ @Test
+ public void testCollection() throws Exception {
+ for (final Appender appender : root.getAppenders().values()) {
+ root.removeAppender(appender);
+ }
+ // set up appender
+ final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext",
+ null, null, true, null, "ATM", null, "key1, key2, locale", null, "loginId", null, true, null, null);
+ final ListAppender appender = new ListAppender("List", null, layout, true, false);
+
+ appender.start();
+
+ // set appender on root and set level to debug
+ root.addAppender(appender);
+ root.setLevel(Level.DEBUG);
+
+ ThreadContext.put("loginId", "JohnDoe");
+ ThreadContext.put("ipAddress", "192.168.0.120");
+ ThreadContext.put("locale", Locale.US.getDisplayName());
+ try {
+ final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
+ msg.put("ToAccount", "123456");
+ msg.put("FromAccount", "123457");
+ msg.put("Amount", "200.00");
+ final StructuredDataMessage msg2 = new StructuredDataMessage("Extra@18060", null, "Audit");
+ msg2.put("Item1", "Hello");
+ msg2.put("Item2", "World");
+ List<StructuredDataMessage> messages = new ArrayList<>();
+ messages.add(msg);
+ messages.add(msg2);
+ final StructuredDataCollectionMessage collectionMessage = new StructuredDataCollectionMessage(messages);
+
+ root.info(MarkerManager.getMarker("EVENT"), collectionMessage);
+
+ List<String> list = appender.getMessages();
+
+ assertTrue("Expected line 1 to end with: " + collectionLine + " Actual " + list.get(0),
+ list.get(0).endsWith(collectionLine));
+
+ for (final String frame : list) {
+ int length = -1;
+ final int frameLength = frame.length();
+ final int firstSpacePosition = frame.indexOf(' ');
+ final String messageLength = frame.substring(0, firstSpacePosition);
+ try {
+ length = Integer.parseInt(messageLength);
+ // the ListAppender removes the ending newline, so we expect one less size
+ assertEquals(frameLength, messageLength.length() + length);
+ }
+ catch (final NumberFormatException e) {
+ assertTrue("Not a valid RFC 5425 frame", false);
+ }
+ }
+
+ appender.clear();
+ } finally {
+ ThreadContext.clearMap();
root.removeAppender(appender);
appender.stop();
}
[2/2] logging-log4j2 git commit: LOG4J2-2008 - support multiple
structured data elements
Posted by rg...@apache.org.
LOG4J2-2008 - support multiple structured data elements
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/1f15422a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1f15422a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1f15422a
Branch: refs/heads/master
Commit: 1f15422a969d2c6a06fe3ccea6313c2aee9c2533
Parents: 3df7f50
Author: Ralph Goers <rg...@nextiva.com>
Authored: Sun Aug 13 17:49:10 2017 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Sun Aug 13 17:49:10 2017 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1f15422a/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a3f6cdc..14837d7 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-2008" dev="rgoers" type="add">
+ Support printing multiple StructuredData elements in RFC5424Layout.
+ </action>
<action issue="LOG4J2-1986" dev="mikes" type="add">
Public API for parsing the output from JsonLayout/XmlLayout/YamlLayout into a LogEvent.
</action>