You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flume.apache.org by ar...@apache.org on 2012/03/22 07:36:26 UTC

svn commit: r1303664 - in /incubator/flume/trunk/flume-ng-sdk: pom.xml src/main/java/org/apache/flume/event/SimpleEvent.java src/test/java/org/apache/flume/event/TestSimpleEvent.java

Author: arvind
Date: Thu Mar 22 06:36:26 2012
New Revision: 1303664

URL: http://svn.apache.org/viewvc?rev=1303664&view=rev
Log:
FLUME-828. Fix LoggerSink represenation of event body.

(Brock Noland via Arvind Prabhakar)

Added:
    incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java   (with props)
Modified:
    incubator/flume/trunk/flume-ng-sdk/pom.xml
    incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java

Modified: incubator/flume/trunk/flume-ng-sdk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-sdk/pom.xml?rev=1303664&r1=1303663&r2=1303664&view=diff
==============================================================================
--- incubator/flume/trunk/flume-ng-sdk/pom.xml (original)
+++ incubator/flume/trunk/flume-ng-sdk/pom.xml Thu Mar 22 06:36:26 2012
@@ -100,5 +100,10 @@ limitations under the License.
       <artifactId>avro-ipc</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
+
   </dependencies>
 </project>

Modified: incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java?rev=1303664&r1=1303663&r2=1303664&view=diff
==============================================================================
--- incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java (original)
+++ incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java Thu Mar 22 06:36:26 2012
@@ -19,13 +19,22 @@
 
 package org.apache.flume.event;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.io.HexDump;
+import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.flume.Event;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SimpleEvent implements Event {
 
+  private static final Logger LOGGER = LoggerFactory
+      .getLogger(SimpleEvent.class);
+  private static final String EOL = System.getProperty("line.separator", "\n");
+  private static final String HEXDUMP_OFFSET = "00000000";
   private Map<String, String> headers;
   private byte[] body;
 
@@ -56,7 +65,32 @@ public class SimpleEvent implements Even
 
   @Override
   public String toString() {
-    return "{ headers:" + headers + " body:" + body + " }";
+    StringBuilder buffer = new StringBuilder();
+    if(body == null) {
+      buffer.append("null");
+    } else {
+      byte[] data = Arrays.copyOf(body, Math.min(body.length, 16));
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      try {
+        HexDump.dump(data, 0, out, 0);
+        String hexDump = new String(out.toByteArray());
+        // remove offset since it's not relevant for such a small dataset
+        if(hexDump.startsWith(HEXDUMP_OFFSET)) {
+          hexDump = hexDump.substring(HEXDUMP_OFFSET.length());
+        }
+        buffer.append(hexDump);
+      } catch (Exception e) {
+       if(LOGGER.isInfoEnabled()) {
+         LOGGER.info("Exception while dumping event", e);
+       }
+       buffer.append("...Exception while dumping: " + e.getMessage());
+      }
+      String result = buffer.toString();
+      if(result.endsWith(EOL) && buffer.length() > EOL.length()) {
+        result = buffer.delete(buffer.length() - EOL.length(), buffer.length()).toString();
+      }
+    }
+    return "{ headers:" + headers + " body:" + buffer + " }";
   }
 
 }

Added: incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java?rev=1303664&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java (added)
+++ incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java Thu Mar 22 06:36:26 2012
@@ -0,0 +1,42 @@
+/*
+ * 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.flume.event;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestSimpleEvent {
+
+  @Test
+  public void testToString() {
+    SimpleEvent event = new SimpleEvent();
+    event.setBody("Some text".getBytes());
+    System.out.println(event);
+    assertTrue(event.toString(), event.toString().contains("Some text"));
+  }
+
+  @Test
+  public void testNonPrintable() {
+    SimpleEvent event = new SimpleEvent();
+    byte[] body = new byte[5];
+    event.setBody(body);
+    assertTrue(event.toString(), event.toString().contains("....."));
+  }
+}

Propchange: incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java
------------------------------------------------------------------------------
    svn:eol-style = native