Java / Android - How to print out a full stack trace?

98,894

Solution 1

There's overrides of all the log methods with (String tag, String msg, Throwable tr) signatures.

Passing an exception as the third parameter should give you the full stacktrace in logcat.

Solution 2

The following should do the trick:

Log.d("myapp", Log.getStackTraceString(new Exception()));

Note that ...x more at the end does not cut off any information from the stack trace:

(This indicates) that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception).

...or in other words, replace x more with the last x lines from the first exception.

Solution 3

Use Log.getStackTraceString(Throwable t). You can get longer stack traces by digging deeper. For example:

try {
    ...
} catch(Exception e) {
    Log.d("Some tag", Log.getStackTraceString(e.getCause().getCause()));
}

Retreived from http://developer.android.com/reference/android/util/Log.html#getStackTraceString%28java.lang.Throwable%29

Solution 4

private static String buildStackTraceString(final StackTraceElement[] elements) {
    StringBuilder sb = new StringBuilder();
    if (elements != null && elements.length > 0) {
        for (StackTraceElement element : elements) {
            sb.append(element.toString());
        }
    }
    return sb.toString();
}


// call this at your check point
Log.d(TAG, buildStackTraceString(Thread.currentThread().getStackTrace()));

Solution 5

You may use this:

public static String toString(StackTraceElement[] stackTraceElements) {
    if (stackTraceElements == null)
        return "";
    StringBuilder stringBuilder = new StringBuilder();
    for (StackTraceElement element : stackTraceElements)
        stringBuilder.append(element.toString()).append("\n");
    return stringBuilder.toString();
}
Share:
98,894
Jake Wilson
Author by

Jake Wilson

Experienced in developing tools for 3D animation, motion capture, video game and movie production, web development, Android development, responsive design, etc...

Updated on July 08, 2022

Comments

  • Jake Wilson
    Jake Wilson almost 2 years

    In Android (Java) how do I print out a full stack trace? If my application crashes from nullPointerException or something, it prints out a (almost) full stack trace like so:

    java.io.IOException: Attempted read from closed stream.
    com.android.music.sync.common.SoftSyncException: java.io.IOException: Attempted read from closed stream.
        at com.android.music.sync.google.MusicSyncAdapter.getChangesFromServerAsDom(MusicSyncAdapter.java:545)
        at com.android.music.sync.google.MusicSyncAdapter.fetchDataFromServer(MusicSyncAdapter.java:488)
        at com.android.music.sync.common.AbstractSyncAdapter.download(AbstractSyncAdapter.java:417)
        at com.android.music.sync.common.AbstractSyncAdapter.innerPerformSync(AbstractSyncAdapter.java:313)
        at com.android.music.sync.common.AbstractSyncAdapter.onPerformLoggedSync(AbstractSyncAdapter.java:243)
        at com.google.android.common.LoggingThreadedSyncAdapter.onPerformSync(LoggingThreadedSyncAdapter.java:33)
        at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:164)
    Caused by: java.io.IOException: Attempted read from closed stream.
        at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:148)
        at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
        at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:212)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:64)
        at android.net.http.AndroidHttpClient.getUngzippedContent(AndroidHttpClient.java:218)
        at com.android.music.sync.api.MusicApiClientImpl.createAndExecuteMethod(MusicApiClientImpl.java:312)
        at com.android.music.sync.api.MusicApiClientImpl.getItems(MusicApiClientImpl.java:588)
        at com.android.music.sync.api.MusicApiClientImpl.getTracks(MusicApiClientImpl.java:638)
        at com.android.music.sync.google.MusicSyncAdapter.getChangesFromServerAsDom(MusicSyncAdapter.java:512)
        ... 6 more
    

    However sometimes, for debugging purposes, I want to log a full stack trace from where I am in the code. I figured I could just do this:

    StackTraceElement trace = new Exception().getStackTrace();
    Log.d("myapp", trace.toString());
    

    But this just prints out the pointer to the object... Do I have to iterate through all the stack trace elements to print them out? Or is there a simple method to print it all out?

  • Philipp Reichart
    Philipp Reichart over 12 years
    FYI the three-argument log methods use the getStackTraceString() method mentioned by @Thomas behind the scenes.
  • Jake Wilson
    Jake Wilson over 12 years
    Where is getStackTraceString() from? Doesn't show up in Eclipse? It's not part of Throwable or Exception....
  • Anh Tuan
    Anh Tuan over 12 years
    I've developed Android for two years and never noticed this before. Thank you very much.
  • Ehtesh Choudhury
    Ehtesh Choudhury about 11 years
    Looking at the source code, yup you're right @PhilippReichart. Here's the code for Log.d on AOSP 4.2.2_r1
  • Philipp Reichart
    Philipp Reichart over 10 years
    Log.getStackTraceString() does not log the stack trace it just returns it as String. The above code won't log anything and will also fail with a NPE if e.getCause() is null.
  • Tomasz
    Tomasz about 10 years
    "...6 more" at the end is not cutting off any information. It is telling you, that the rest of the stack trace is the the same as it was for the top exception. Just look at the last 6 lines from the first exception.
  • Dan
    Dan almost 8 years
    You'd probably need to import the logging library (using import android.util.Log;).
  • blinker
    blinker over 2 years
    Log.e and Log.d not working :(