Hello all,
I’ve spent a few hours today playing with the new Log4j avro appender, thanks to Jonathan et al for getting that in the latest release. It’s seems to work very reliably, however it only seems to take the message body of a log4j event and ignores the other elements. This is a bit of a problem for me as our software tracing relies on displaying the class, thread, MDC and stacktrace entries for debugging purposes. Essentially flume is ignoring the log4j pattern.
I had a look in the source and found that the Log4JEventAdaptor class and found that only the message of the event was being used, and the layout ignored. So I created a new constructor to allow me to pass in a Log4j layout to the event adaptor, I also modified the getBody method to use the layout if the layout object is not null and I added some code to print the stacktrace if it is present. See my hack below:
public Log4JEventAdaptor(LoggingEvent evt, Layout layout) {
super();
this.evt = evt;
this.layout = layout;
// This is needed to differentiate between events at the same millisecond.
this.nanos = System.nanoTime();
}
public byte[] getBody() {
if (layout == null) {
return evt.getRenderedMessage().getBytes();
} else {
String body = layout.format(evt);
String[] s = evt.getThrowableStrRep();
if (s != null) {
int len = s.length;
for (int i = 0; i < len; i++) {
body = body + s[i];
body = body + Layout.LINE_SEP;
}
}
return body.getBytes();
}
}
So now I get formatted log4j entries in flume! My question is; was there a reason for ignoring the log4j layout in the event adaptor and might what I have done be useful to anyone else?
Also stack traces are appearing all on one line with the line separator escape character instead of a new line. From what I have read the tail source can cope with stacktraces and make them into a single event, is it possible to do this with the log4j/avro source?
Thanks
Chris
This email message has been scanned for viruses by Mimecast