Moocar / logback-gelf

Logback plugin to send GELF messages to graylog2 server
Apache License 2.0
147 stars 59 forks source link

NPE when there is no callerData #44

Closed quhw closed 9 years ago

quhw commented 9 years ago

I am building the log forwarder which forwards the LoggingEventVO to a GelfAppender. But the event has no callerData, so that the GelfConverter does not work.

We should check if hasCallerData before using it.

private void stackTraceField(Map<String, Object> map, ILoggingEvent eventObject) {
    IThrowableProxy throwableProxy = eventObject.getThrowableProxy();
    if (throwableProxy != null ) {
        StackTraceElementProxy[] proxyStackTraces = throwableProxy.getStackTraceElementProxyArray();
        if (proxyStackTraces != null && proxyStackTraces.length > 0) {
            StackTraceElement[] callStackTraces = eventObject.getCallerData();
            if (callStackTraces.length > 0) {
                StackTraceElement lastStack = callStackTraces[0];
                map.put("file", lastStack.getFileName());
                map.put("line", String.valueOf(lastStack.getLineNumber()));
            }
        }
    }
}
Moocar commented 9 years ago

Thanks!