简体   繁体   中英

How to log exception centrally at place of original stack trace?

All exceptions in an Android app are logged by one global method to log them in Firebase Crashlytics.

public class L {
    public static void e(String message, Exception e) {
        Crashlytics.logException(e);
    }
}

The problem is that in Crashlytics the exceptions are grouped by where they are logged. So all logged exceptions appear to Crashlytics as the same exception.

在此处输入图像描述

In other words, there is one additional line added to the stack trace which becomes the new top level which is used by Crashlytics to group exceptions. This makes it difficult to browse and analyze the exceptions as they are all thrown into one pot.

Is it possible to log the original exception without adding the additional top level? Maybe with a callback, or is there a more elegant way?

Indeed, Craslytics groups exception based on the top level line where the exception was thrown.

I suggest you move the call to Crashlytics.logException(e); where you call the original Log.e(), so you'll have

Crashlytics.logException(e);

Log.e(message, e);

You can also log additional infos into Crashlytics using

Crashlytics.log(priority, "YouTag", "YourMsg");

It's actually possible by modifying the stacktrace of the exception.

Crashlytics logs the stack trace of the exception. The stack trace is set to where the RuntimeException is created. By removing the top entry of the stack trace, the exception is logged by Crashlytics as if it was thrown at the new top stack entry.

public static e(@NonNull String message) {

    RuntimeException e = new RuntimeException(message);
    StackTraceElement[] s = Arrays.copyOfRange(e.getStackTrace(), 1, s.length);
    e.setStackTrace(s);
    Crashlytics.logException(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM