Combining exception handling & logging

45 Views Asked by At

I'm curious around best practices for exception handling & logging.

I have a mobile App which uses Airbrake for exception monitoring and basic Android.Log for logging.

So there are several spots in code that look similar to

// some request
AirbrakeNotifier.notify(exception)

After some updates I'm using a logger interface and Timber. I'm interested in the code above being changed into something like

//some request
Timber.e("Failed to blah blah", exception.)

And a custom timber tree could be planted like

class MyTree: Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
    //if log level error
    ExceptionMonitor.notify(t)
    logger.log(....)
  }
}

In my case, any error I'm logging I want to send to the remote monitoring service as well. All other Log levels will simply log.

Is bundling logging/exception handling under an interface bad practice?

1

There are 1 best solutions below

0
mol On

It's quite usual to use Timber for crash analytics services. In fact, the official Timber sample uses FakeCrashLibrary as an example of this use case.

Normally, you shouldn't have debug logs in production anyway, and Timber helps to replace debug logs with some crash analytics service for prod builds.