10

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());
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
pixel
  • 24,905
  • 36
  • 149
  • 251

3 Answers3

6

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.

gabriel
  • 1,163
  • 2
  • 9
  • 15
6

I know this thread is old but I am trying to figure out how to get this working, but just for completeness there is a useful method on Log that does what you want

String stackTraceStr = Log.getStackTraceString(exception);

EDIT: In response to the 100 char limit comment

I could never get EasyTracker.getTracker().setExceptionParser(...) working, infact I do not think it works, so I followed the blog post here http://dandar3.blogspot.co.uk/2013/03/google-analytics-easytracker-detailed.html

The important point in the blog post is to make sure you set your ExceptionParser on the GA exception handler:

// Make sure you set the context on EasyTracker first
EasyTracker.getInstance().setContext(this);

// As in in the blog post, we have to get the ExceptionReporter
// in order to set the ExceptionParser
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
if (uncaughtExceptionHandler instanceof ExceptionReporter) {
  ExceptionReporter exceptionReporter = (ExceptionReporter) uncaughtExceptionHandler;
  exceptionReporter.setExceptionParser(new AnalyticsExceptionParser());
}

This worked for me and logs more than 100 chars.

Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • 1
    Unfortunately there is 100 character limit in Google Analytics like @Nikolay Elenkow said. I've ended up using Bugsense. – pixel Apr 04 '13 at 14:42
  • 1
    I have got it working and not seeing the 100 char limit, I have at least 1024 logged, I am going to update my answer with how I got it working. – Ian Warwick Apr 05 '13 at 09:33
  • i am getting a stack trace partially.That is before "caused by : " in stack trace. After "caused by:" in stack trace is not able to get. Can u please help me.Thanks in advance – Sakthimuthiah Jun 07 '13 at 12:11
  • @IanWarwick Hi I put Exception in onStop method.I not see more than 100.I unable to see whole logcat instead of first line. Could you please send me a demo.Please email me : mmaidul.islam@gmail.com – Md Maidul Islam Sep 05 '13 at 05:02
4

Analytics may be limiting the size of messages you can send. I suggest using a real error reporting library like ACRA or BugSense instead. You will get extra features such as device info, configuration details and better error reporting (combining multiple exceptions if they have the same trace, etc.).

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Actually there is no statement about limiting sive in the documentation. I would like to use one library instead several. Additionally in new Analytics you have features like combining multiple exceptions, device info and so on. The only thing missing is the full stacktrace. Suggesting other options is not an answer in this case :) – pixel Dec 05 '12 at 15:24
  • The fact the documentation doesn't state a limit doesn't mean that there isn't one. I am however sure that both ACRA and BugSense work better for error reporting than Analytics, which works OK, but for analytics :) You should at least consider them, regardless of whether you resolve your current issue or not. – Nikolay Elenkov Dec 05 '12 at 15:38