Trouble with logging my data with crashlytics

后端 未结 4 1532
萌比男神i
萌比男神i 2020-12-14 00:51

I\'m trying to get logs with some service data with Crashlytics in my android application. But I don\'t see my logs in dashboard. I used this:

String myLog =         


        
相关标签:
4条回答
  • 2020-12-14 01:32

    Instead of catching the exception and logging it, you can instead use RuntimeExceptions:

    throw new RuntimeException("Test crash");

    Android Studio will not require you to catch these, thus having the app terminate and upload the report immediately.

    0 讨论(0)
  • 2020-12-14 01:50

    This is an old question but since I was having the same problem today, I figured I should post my solution (it's in Kotlin though).

    With Crashlytics now a part of Google's Firebase offering, it initializes behind the scenes, so a solution I wrote a few years ago had to be updated to this:

    1. Implement some kind of log caching, First In First Out. I put one together based on cache-lite.
    2. Put this function in YourApplicationClass that extends Application:

          fun setExceptionHandler() {
      
             val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
      
             Thread.setDefaultUncaughtExceptionHandler { thread, ex ->
             Crashlytics.log(cacheLoggingTree.getAll().joinToString("++"))
             Crashlytics.logException(ex)
             defaultExceptionHandler.uncaughtException(thread, ex)
             }
          }
      
    3. Then you call the function from the end of your onCreate function in your MainActivity:

           (application as YourApplicationClass).setExceptionHandler()
      

    With that, any exceptions that get thrown will first post the most recent log entries, then log the exception. You probably want to be cautious with how many lines you cache though, lest you overload the system.

    Also, I used ++ as a flag to manually replace with carriage returns when you download the log, since the \n I had gets stripped in the upload process.

    0 讨论(0)
  • 2020-12-14 01:52

    I had a similar situation. With some experimenting, I was able to deduce the rules to Crashlytics' behavior.

    I'm sharing my learnings here so (hopefully) others don't have to go through the arduous and time-consuming process that I did to figure it out.

    Crashlytics will only upload a crash report "to the dashboard" immediately if a fatal exception occurs. In other words, when your app crashes. And nothing shows in the dashboard unless and until a crash report is uploaded.

    If you log a non-fatal exception, using CrashLytics.logException(e), a crash report will not be uploaded till the next time your app is restarted. So you will not see the exception in the Crashlytics dashboard till an app restart.

    You can tell when an upload occurs because you'll see this sort of message in LogCat:

    07-17 19:30:41.477 18815-18906/com.foo.bar I/Crashlytics﹕ Crashlytics report upload complete: 55A9BA0C01D7-0001-462D-B8B4C49333333.cls

    A Crashlytics log message must be associated with a fatal or non-fatal exception to show up in the dashboard.

    Furthermore, log messages that aren't associated with an exception do not survive app restart.

    So, if you do something like log a few messages, then restart the app, then the app throws an exception, or it logs a non-fatal exception using Crashlytics.logException(), the log messages will be lost. They will not show up in the dashboard.

    If you want to log some messages without a fatal exception, use one or more Crashlytics.log() statements followed by Crashlytics.logException().

    To verify that it works, have the code run, then restart the app. In the dashboard, you should see the logs associated with the issue created for the non-fatal exception. In the wild, you'll just have to trust your users restart the app with some regularity.

    On the Fabric/Crashlytics dashboard, you'll need to select All Events or (if you want to see just your logging calls) Non-Fatals.

    0 讨论(0)
  • 2020-12-14 01:56

    according to Crashlytics knowledgebase:

    Logged messages are associated with your crash data and are visible in the Crashlytics dashboard if you look at the specific crash itself.

    And from my experience this seems true. However I am unsure as to what determines which logging is associated with a crash report. Perhaps a time window (time around crash) or a limited number of logs (before the crash) is associated with the crash report?

    However Crashlytics knowledgebase does say that exceptions can be logged:

    All logged exceptions will appear as "non-fatal" issues in the Crashlytics dashboard.

    So if you changed your try/catch to:

    try {
       int a = 0;
       a = 1/a;
    }
    catch (Exception e) {
       CrashLytics.logException(e);
    }
    

    then it should show up in the Crashlytics dashboard.

    0 讨论(0)
提交回复
热议问题