29

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 = getServiceData(); //myLog is not null and non-empty
CrashLytics.log(myLog);

and this:

String myLog = getServiceData(); //myLog is not null and non-empty
CrashLytics.log(Log.Error, getString(R.string.app_name), myLog);

I tried to generate exception in my application and handle it, but have no results:

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

Also I read on Crashlytics log not sent I need to initialize crashlytics before log data. I put Crashlytics.start(this) in onStart() event of my Activity but didn't see my logs in dashboard again. At last I tried to put Crashlitycs.start(this) directly before logging my data, but still have no logs in dashboard.

Plase, tell me what I do wrong and how to get my custom logs in Crashlytics dashboard?

Community
  • 1
  • 1
ibogolyubskiy
  • 2,271
  • 3
  • 26
  • 52
  • Crashlytics isn't made for develop logging, it is made to cluster hundreds/thousands of logs from different users at runtime, this may cause that the dashboard doesn't show the logs in the exactly time they are produced. If you want to use Crashlytics to follow the code workflow and debug during development, you will need to use the trick in this answer. https://stackoverflow.com/a/69340289/5679560 – Rafael Lima Sep 27 '21 at 01:47

4 Answers4

65

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.

enter image description here

Julian A.
  • 10,928
  • 16
  • 67
  • 107
  • 3
    If I call Crashlytics.logException(). I can see this log on Dashboard after the app restarts. But another such logs Crashlytics.log(android.util.Log.INFO, tag, msg), Crashlytics.log(android.util.Log.ERROR, tag, msg), Crashlytics.log(android.util.Log.DEBUG, tag, msg) are called before and after Crashlytics.logException() do not appear on Dashboard. Please help me how to let them show on Dashboard. How do we log a Crashlytics log message be associated with a fatal or non-fatal exception? Thanks – Tai Le Anh Apr 14 '16 at 09:35
  • 6
    trust the user? never! – filthy_wizard Aug 01 '17 at 17:43
  • 3
    This needs to be printed out and put on the wall! – bajicdusko Sep 08 '17 at 14:25
  • 2
    You sir! are a legend! – MiaN KhaLiD May 04 '18 at 12:39
  • 1
    i wish logs were stored locally and uploaded without loss surviving app close – Jemshit Nov 15 '18 at 08:01
  • Crashlytics isn't made for develop logging, it is made to cluster hundreds/thousands of logs from different users at runtime, this may cause that the dashboard doesn't show the logs in the exactly time they are produced. If you want to use Crashlytics to follow the code workflow and debug during development, you will need to use the trick in this answer. https://stackoverflow.com/a/69340289/5679560 – Rafael Lima Sep 27 '21 at 01:47
5

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.

1

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.

TopherC
  • 79
  • 3
-1

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.

jhm
  • 4,379
  • 5
  • 33
  • 49
  • @RafaelLima - A developer who's helping debug why a CRASH reporting tool isn't working. In this specific case, intentional crashing of the app is desired. Did you even read the question before you downvoted me? Or considered asking what my reasoning was? – jhm Sep 10 '18 at 11:30
  • Probably you didn't and simple jumped into the easiest solution you could find to tr y to get some upvotes... the question is about tracking the normal use of an app with crashlytics, some guys rightly pointed that the logs are buffered until one exception is thrown, then you came with the brilliant idea of crashing the app in order to get those logs... it doesn't make sense in any situation and any person with reasonable understanding of right and wrong will agree with that... doesn't need to be a developper – Rafael Lima Sep 10 '18 at 11:37
  • if you wanted to advise the person how to log in the development enviorement you should just told him to use logcat... throwing the app in order to extract logs isn't a reasonable choice in any planet i know – Rafael Lima Sep 10 '18 at 11:38
  • If one is not doing it on a production app and just during development to test that his Crashlytics setup worked, then yes - it is feasible as a sanity check of the setup, so that one could at least verify that his integration to Crashlytics works. – jhm Sep 10 '18 at 12:00