Repository: logging-log4j2
Updated Branches:
  refs/heads/master d2aa15bd1 -> c14eb2729


LOG4J2-2020 Remove default layout from KafkaAppender


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

Branch: refs/heads/master
Commit: c14eb272985cd718c210ca13e251fa49881eebc5
Parents: d2aa15b
Author: Mikael Ståldal <mik...@staldal.nu>
Authored: Thu Aug 17 20:59:01 2017 +0200
Committer: Mikael Ståldal <mik...@staldal.nu>
Committed: Thu Aug 17 20:59:01 2017 +0200

----------------------------------------------------------------------
 .../core/appender/mom/kafka/KafkaAppender.java  | 49 ++++++-----
 .../appender/mom/kafka/KafkaAppenderTest.java   | 15 +---
 .../src/test/resources/KafkaAppenderTest.xml    | 88 ++++++++++----------
 src/changes/changes.xml                         |  3 +
 src/site/xdoc/manual/appenders.xml              |  5 +-
 5 files changed, 74 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c14eb272/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
index 5fb1c7c..81ec09b 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
@@ -18,12 +18,12 @@
 package org.apache.logging.log4j.core.appender.mom.kafka;
 
 import java.io.Serializable;
-import java.nio.charset.StandardCharsets;
 import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
+import org.apache.logging.log4j.core.AbstractLifeCycle;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.Filter;
 import org.apache.logging.log4j.core.Layout;
@@ -35,11 +35,8 @@ import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
-import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import 
org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
 import org.apache.logging.log4j.core.layout.SerializedLayout;
-import org.apache.logging.log4j.core.util.StringEncoder;
 
 /**
  * Sends log events to an Apache Kafka topic.
@@ -66,8 +63,13 @@ public final class KafkaAppender extends AbstractAppender {
         @SuppressWarnings("resource")
         @Override
         public KafkaAppender build() {
+            final Layout<? extends Serializable> layout = getLayout();
+            if (layout == null) {
+                AbstractLifeCycle.LOGGER.error("No layout provided for 
KafkaAppender");
+                return null;
+            }
             final KafkaManager kafkaManager = new 
KafkaManager(getConfiguration().getLoggerContext(), getName(), topic, syncSend, 
properties);
-            return new KafkaAppender(getName(), getLayout(), getFilter(), 
isIgnoreExceptions(), kafkaManager);
+            return new KafkaAppender(getName(), layout, getFilter(), 
isIgnoreExceptions(), kafkaManager);
         }
 
         public String getTopic() {
@@ -100,13 +102,18 @@ public final class KafkaAppender extends AbstractAppender 
{
     
     @Deprecated
     public static KafkaAppender createAppender(
-            @PluginElement("Layout") final Layout<? extends Serializable> 
layout,
-            @PluginElement("Filter") final Filter filter,
-            @Required(message = "No name provided for KafkaAppender") 
@PluginAttribute("name") final String name,
-            @PluginAttribute(value = "ignoreExceptions", defaultBoolean = 
true) final boolean ignoreExceptions,
-            @Required(message = "No topic provided for KafkaAppender") 
@PluginAttribute("topic") final String topic,
-            @PluginElement("Properties") final Property[] properties,
-            @PluginConfiguration final Configuration configuration) {
+            final Layout<? extends Serializable> layout,
+            final Filter filter,
+            final String name,
+            final boolean ignoreExceptions,
+            final String topic,
+            final Property[] properties,
+            final Configuration configuration) {
+
+        if (layout == null) {
+            AbstractLifeCycle.LOGGER.error("No layout provided for 
KafkaAppender");
+            return null;
+        }
         final KafkaManager kafkaManager = new 
KafkaManager(configuration.getLoggerContext(), name, topic, true, properties);
         return new KafkaAppender(name, layout, filter, ignoreExceptions, 
kafkaManager);
     }
@@ -144,18 +151,14 @@ public final class KafkaAppender extends AbstractAppender 
{
     private void tryAppend(final LogEvent event) throws ExecutionException, 
InterruptedException, TimeoutException {
         final Layout<? extends Serializable> layout = getLayout();
         byte[] data;
-        if (layout != null) {
-            if (layout instanceof SerializedLayout) {
-                final byte[] header = layout.getHeader();
-                final byte[] body = layout.toByteArray(event);
-                data = new byte[header.length + body.length];
-                System.arraycopy(header, 0, data, 0, header.length);
-                System.arraycopy(body, 0, data, header.length, body.length);
-            } else {
-                data = layout.toByteArray(event);
-            }
+        if (layout instanceof SerializedLayout) {
+            final byte[] header = layout.getHeader();
+            final byte[] body = layout.toByteArray(event);
+            data = new byte[header.length + body.length];
+            System.arraycopy(header, 0, data, 0, header.length);
+            System.arraycopy(body, 0, data, header.length, body.length);
         } else {
-            data = 
StringEncoder.toBytes(event.getMessage().getFormattedMessage(), 
StandardCharsets.UTF_8);
+            data = layout.toByteArray(event);
         }
         manager.send(data);
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c14eb272/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppenderTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppenderTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppenderTest.java
index 4cac0db..2f60750 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppenderTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppenderTest.java
@@ -96,19 +96,6 @@ public class KafkaAppenderTest {
     }
 
     @Test
-    public void testAppend() throws Exception {
-        final Appender appender = ctx.getRequiredAppender("KafkaAppender");
-        appender.append(createLogEvent());
-        final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
-        assertEquals(1, history.size());
-        final ProducerRecord<byte[], byte[]> item = history.get(0);
-        assertNotNull(item);
-        assertEquals(TOPIC_NAME, item.topic());
-        assertNull(item.key());
-        assertEquals(LOG_MESSAGE, new String(item.value(), 
StandardCharsets.UTF_8));
-    }
-
-    @Test
     public void testAppendWithLayout() throws Exception {
         final Appender appender = 
ctx.getRequiredAppender("KafkaAppenderWithLayout");
         appender.append(createLogEvent());
@@ -157,7 +144,7 @@ public class KafkaAppenderTest {
 
     @Test(timeout = 2000)
     public void testClose() throws Exception {
-        final Appender appender = ctx.getRequiredAppender("KafkaAppender");
+        final Appender appender = 
ctx.getRequiredAppender("KafkaAppenderWithLayout");
         appender.stop();
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c14eb272/log4j-core/src/test/resources/KafkaAppenderTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/KafkaAppenderTest.xml 
b/log4j-core/src/test/resources/KafkaAppenderTest.xml
index dda8271..2af8586 100644
--- a/log4j-core/src/test/resources/KafkaAppenderTest.xml
+++ b/log4j-core/src/test/resources/KafkaAppenderTest.xml
@@ -1,47 +1,43 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ 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 name="KafkaAppenderTest" status="OFF">
-  <Appenders>
-    <Kafka name="KafkaAppender" topic="kafka-topic">
-      <Property name="bootstrap.servers">localhost:9092</Property>
-      <Property name="timeout.ms">1000</Property>
-    </Kafka>
-    <Kafka name="KafkaAppenderWithLayout" topic="kafka-topic">
-      <PatternLayout pattern="[%m]"/>
-      <Property name="bootstrap.servers">localhost:9092</Property>
-      <Property name="timeout.ms">1000</Property>
-    </Kafka>
-    <Kafka name="KafkaAppenderWithSerializedLayout" topic="kafka-topic">
-      <SerializedLayout/>
-      <Property name="bootstrap.servers">localhost:9092</Property>
-      <Property name="timeout.ms">1000</Property>
-    </Kafka>
-    <Kafka name="AsyncKafkaAppender" topic="kafka-topic">
-      <Property name="bootstrap.servers">localhost:9092</Property>
-      <Property name="syncSend">false</Property>
-    </Kafka>
-  </Appenders>
-  <Loggers>
-    <Root level="info">
-      <AppenderRef ref="KafkaAppender"/>
-      <AppenderRef ref="KafkaAppenderWithLayout"/>
-      <AppenderRef ref="KafkaAppenderWithSerializedLayout"/>
-      <AppenderRef ref="AsyncKafkaAppender"/>
-    </Root>
-  </Loggers>
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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 name="KafkaAppenderTest" status="OFF">
+  <Appenders>
+    <Kafka name="KafkaAppenderWithLayout" topic="kafka-topic">
+      <PatternLayout pattern="[%m]"/>
+      <Property name="bootstrap.servers">localhost:9092</Property>
+      <Property name="timeout.ms">1000</Property>
+    </Kafka>
+    <Kafka name="KafkaAppenderWithSerializedLayout" topic="kafka-topic">
+      <SerializedLayout/>
+      <Property name="bootstrap.servers">localhost:9092</Property>
+      <Property name="timeout.ms">1000</Property>
+    </Kafka>
+    <Kafka name="AsyncKafkaAppender" topic="kafka-topic">
+      <PatternLayout pattern="%m"/>
+      <Property name="bootstrap.servers">localhost:9092</Property>
+      <Property name="syncSend">false</Property>
+    </Kafka>
+  </Appenders>
+  <Loggers>
+    <Root level="info">
+      <AppenderRef ref="KafkaAppenderWithLayout"/>
+      <AppenderRef ref="KafkaAppenderWithSerializedLayout"/>
+      <AppenderRef ref="AsyncKafkaAppender"/>
+    </Root>
+  </Loggers>
 </Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c14eb272/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 326cdc7..69c8b53 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-2020" dev="mikes" type="update">
+        Remove default layout from KafkaAppender.
+      </action>
       <action issue="LOG4J2-2018" dev="rpopma" type="fix">
         Fix incorrect documentation for LoggerNameLevelRewritePolicy.
       </action>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c14eb272/src/site/xdoc/manual/appenders.xml
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/appenders.xml 
b/src/site/xdoc/manual/appenders.xml
index 3b7aa03..f24888e 100644
--- a/src/site/xdoc/manual/appenders.xml
+++ b/src/site/xdoc/manual/appenders.xml
@@ -1744,9 +1744,8 @@ public class JpaLogEntity extends 
AbstractLogEventWrapperEntity {
               <td>layout</td>
               <td>Layout</td>
               <td>
-                The Layout to use to format the LogEvent. If you do not 
specify a layout, the appender will send the
-                <a class="javadoc" 
href="http://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/message/Message.html#getFormattedMessage())">formatted
 message</a>
-                to Kafka as a UTF-8 encoded string.
+                The Layout to use to format the LogEvent. Required, there is 
no default.
+                <em>New since 2.9, in previous versions &lt;PatternLayout 
pattern="%m"/&gt; was default.</em>
               </td>
             </tr>
             <tr>

Reply via email to