Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format exception message to contain the whole stacktrace in Google Analytics for Android?

I would like to have the whole stacktrace in Google Analytic's report's for my mobile application.

I wrote class that print's the stacktrace and put's it into string, but it doesn't work.

My custom ExceptionParser:

@Override
public String getDescription(String threadName, Throwable throwable) {
    return threadName + " " + getStackTrace(throwable);
}

private String getStackTrace(Throwable throwable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    throwable.printStackTrace(printWriter);

    return result.toString();
}

And I set it like this:

EasyTracker.getTracker().setExceptionParser(new StacktraceExceptionParser());
like image 309
pixel Avatar asked Nov 29 '12 15:11

pixel


1 Answers

The method below combines the entire stack trace into a single comma separated line, which may help in case Analytics returns just the first line. But there still may be a length limit so it may be prudent to do filtering to eliminate items you do not need (see comment)

 public String getCombinedStackTrace(Throwable aThrowable) {

    final StringBuilder result = new StringBuilder();
    result.append(aThrowable.toString());
    result.append(',');

    String oneElement;

    for (StackTraceElement element : aThrowable.getStackTrace() ) {
        // you can do some filtering here, selecting only the elements you need
        oneElement = element.toString();
        result.append( oneElement );
        result.append( ",");
    }
    return result.toString();
}

I second Nikolay's comment about using an error reporting library. I found it to be tremendously helpful.

like image 79
gabriel Avatar answered Oct 06 '22 06:10

gabriel