0

I want to use okhttp interceptor (build inside retrofit class) to add logs to sentry where several tags can be added along with error, for example i add the user(name) who was handling the app(which is stored in sharedpreferences) with an example code like eventBuilder.withTag(SentryConstants.TYPE_CUSTOMER_NAME, customer.getFirstname());

I have a static Retrofit class so that i can build it once and use for every api call, Since to access the user i need the applicationcontext for sharedpreferences i have 3 ways of adding logs

1) from the activity where i call the api pass context using getApplicationContext() to retrofit class

  • This makes me create a non static class as i have to pass context as parameter

2) before making the api(retrofit) call in the activity i can log it to sentry

  • I get the advantage of static retrofit class but lose central logging system(i have to make sure i log everytime)

3) make a static class with context as mentioned here

  • Not a recommended way, also in future i may have other options which fragment is calling the api

What would be the recommended approach

kobe24
  • 190
  • 1
  • 5
  • 16
  • 2
    The way we do it is use dependency injection through Dagger 2 and then we inject the preferences and not the application `Context`. Besides, there's no issue in giving in the application `Context` as long as you're sure it's the application `Context` and not an `Activity` `Context` as the application `Context` can't really be leaked as it is your whole process, so when Android decides to kill your app, it will be killed and the application `Context` will be GC'ed no matter what :-) – Darwind Oct 31 '18 at 11:33
  • @Darwind got it thanks, i created an static abstract class which extends Application – kobe24 Oct 31 '18 at 12:00
  • Just remember that if you want to unit test this bit of code (the `Interceptor`) you'll get into issues because you're sending in the `Application` and the `Application` doesn't exist outside of an Android context. – Darwind Oct 31 '18 at 12:22

0 Answers0