Trouble with logging my data with crashlytics

后端 未结 4 1531
萌比男神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: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.

提交回复
热议问题