Package org.apache.flume.sink;
Import com.google.common.base.Strings;
Import Org.apache.flume.Channel;
Import Org.apache.flume.Context;
Import org.apache.flume.Event;
Import org.apache.flume.EventDeliveryException;
Import org.apache.flume.Transaction;
Import org.apache.flume.conf.Configurable;
Import Org.apache.flume.event.EventHelper;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
public class Loggersink extends Abstractsink implements configurable {
private static final Logger Logger = loggerfactory
. GetLogger (Loggersink.class);
Default Max bytes to dump
public static final int default_max_byte_dump = 16;
Max number of bytes to be dumped
private int maxbytestolog = Default_max_byte_dump;
public static final String Max_bytes_dump_key = "Maxbytestolog";
@Override
public void Configure (context context) {
String strmaxbytes = context.getstring (Max_bytes_dump_key);
if (! Strings.isnullorempty (strmaxbytes)) {
try {
Maxbytestolog = Integer.parseint (strmaxbytes);
} catch (NumberFormatException e) {
Logger.warn (String.Format ("Unable to convert%s to integer, using the default value (%d) for Maxbytetodump",
Strmaxbytes, Default_max_byte_dump));
Maxbytestolog = Default_max_byte_dump;
}
}
}
@Override
Public Status process () throws Eventdeliveryexception {
Status result = Status.ready;
Channel channel = Getchannel ();
Transaction Transaction = Channel.gettransaction ();
Event event = null;
try {
Transaction.begin ();
event = Channel.take ();
if (event! = null) {
if (logger.isinfoenabled ()) {
Logger.info ("event:" + eventhelper.dumpevent (event, Maxbytestolog));
}
} else {
No event found, request Back-off semantics from the sink runner
result = Status.backoff;
}
Transaction.commit ();
} catch (Exception ex) {
Transaction.rollback ();
throw new Eventdeliveryexception ("Failed to log Event:" + Event, ex);
} finally {
Transaction.close ();
}
return result;
}
}
Flume Source-loggersink