flume-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1305718 - in /incubator/flume/trunk: flume-ng-core/src/main/java/org/apache/flume/event/ flume-ng-core/src/main/java/org/apache/flume/sink/ flume-ng-core/src/test/java/org/apache/flume/event/ flume-ng-sdk/src/main/java/org/apache/flume/eve...
Date Tue, 27 Mar 2012 03:04:27 GMT
Author: brock
Date: Tue Mar 27 03:04:26 2012
New Revision: 1305718

URL: http://svn.apache.org/viewvc?rev=1305718&view=rev
Log:
FLUME-1047: Client SDK has dependency on apache commons

(Mike Percy via Brock Noland)

Added:
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/event/EventHelper.java
    incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/
    incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/TestEventHelper.java
Removed:
    incubator/flume/trunk/flume-ng-sdk/src/test/java/org/apache/flume/event/TestSimpleEvent.java
Modified:
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/sink/LoggerSink.java
    incubator/flume/trunk/flume-ng-sdk/src/main/java/org/apache/flume/event/SimpleEvent.java

Added: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/event/EventHelper.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/event/EventHelper.java?rev=1305718&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/event/EventHelper.java
(added)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/event/EventHelper.java
Tue Mar 27 03:04:26 2012
@@ -0,0 +1,71 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+import org.apache.commons.io.HexDump;
+import org.apache.flume.Event;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EventHelper {
+
+  private static final String HEXDUMP_OFFSET = "00000000";
+  private static final String EOL = System.getProperty("line.separator", "\n");
+  private static final int DEFAULT_MAX_BYTES = 16;
+
+  private static final Logger LOGGER = LoggerFactory
+      .getLogger(EventHelper.class);
+
+  public static String dumpEvent(Event event) {
+    return dumpEvent(event, DEFAULT_MAX_BYTES);
+  }
+
+  public static String dumpEvent(Event event, int maxBytes) {
+    StringBuilder buffer = new StringBuilder();
+    if(event == null) {
+      buffer.append("null");
+    } else {
+      byte[] body = event.getBody();
+      byte[] data = Arrays.copyOf(body, Math.min(body.length, maxBytes));
+      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: ").append(e.getMessage());
+      }
+      String result = buffer.toString();
+      if(result.endsWith(EOL) && buffer.length() > EOL.length()) {
+        buffer.delete(buffer.length() - EOL.length(), buffer.length()).toString();
+      }
+    }
+    return "{ headers:" + event.getHeaders() + " body:" + buffer + " }";
+  }
+}

Modified: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/sink/LoggerSink.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/sink/LoggerSink.java?rev=1305718&r1=1305717&r2=1305718&view=diff
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/sink/LoggerSink.java
(original)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/sink/LoggerSink.java
Tue Mar 27 03:04:26 2012
@@ -15,6 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.flume.sink;
 
 import org.apache.flume.Channel;
@@ -22,6 +23,7 @@ import org.apache.flume.Event;
 import org.apache.flume.EventDeliveryException;
 import org.apache.flume.Sink;
 import org.apache.flume.Transaction;
+import org.apache.flume.event.EventHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,7 +67,7 @@ public class LoggerSink extends Abstract
 
       if (event != null) {
         if (logger.isInfoEnabled()) {
-          logger.info("Event: " + event);
+          logger.info("Event: " + EventHelper.dumpEvent(event));
         }
       } else {
         // No event found, request back-off semantics from the sink runner
@@ -74,7 +76,8 @@ public class LoggerSink extends Abstract
       transaction.commit();
     } catch (Exception ex) {
       transaction.rollback();
-      throw new EventDeliveryException("Failed to log event: " + event, ex);
+      throw new EventDeliveryException("Failed to log event: " +
+          EventHelper.dumpEvent(event), ex);
     } finally {
       transaction.close();
     }

Added: incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/TestEventHelper.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/TestEventHelper.java?rev=1305718&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/TestEventHelper.java
(added)
+++ incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/event/TestEventHelper.java
Tue Mar 27 03:04:26 2012
@@ -0,0 +1,43 @@
+/*
+ * 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 org.junit.Assert;
+import org.junit.Test;
+
+public class TestEventHelper {
+
+  @Test
+  public void testPrintable() {
+    SimpleEvent event = new SimpleEvent();
+    event.setBody("Some text".getBytes());
+    String eventDump = EventHelper.dumpEvent(event);
+    System.out.println(eventDump);
+    Assert.assertTrue(eventDump, eventDump.contains("Some text"));
+  }
+
+  @Test
+  public void testNonPrintable() {
+    SimpleEvent event = new SimpleEvent();
+    byte[] body = new byte[5];
+    event.setBody(body);
+    String eventDump = EventHelper.dumpEvent(event);
+    Assert.assertTrue(eventDump, eventDump.contains("....."));
+  }
+}

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=1305718&r1=1305717&r2=1305718&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
Tue Mar 27 03:04:26 2012
@@ -19,22 +19,13 @@
 
 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;
 
@@ -65,32 +56,9 @@ public class SimpleEvent implements Even
 
   @Override
   public String toString() {
-    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 + " }";
+    Integer bodyLen = null;
+    if (body != null) bodyLen = body.length;
+    return "[Event headers = " + headers + ", body.length = " + bodyLen + " ]";
   }
 
 }



Mime
View raw message