Skip to content

Instantly share code, notes, and snippets.

@johnkil
Created February 20, 2023 15:35
Show Gist options
  • Save johnkil/d59b619a0322abbc977c63afe435c483 to your computer and use it in GitHub Desktop.
Save johnkil/d59b619a0322abbc977c63afe435c483 to your computer and use it in GitHub Desktop.
import android.util.Log
import com.bugsnag.android.Bugsnag
import timber.log.Timber
import java.util.*
class BugsnagTree : Timber.DebugTree() {
// Adding one to the initial size accounts for the add before remove.
private val buffer = ArrayDeque<String>(BUFFER_SIZE + 1)
override fun isLoggable(tag: String?, priority: Int): Boolean = priority >= Log.INFO
override fun log(priority: Int, tag: String?, message: String, exception: Throwable?) {
val adjustedMessage = """${System.currentTimeMillis()} ${priorityToString(priority)} $message"""
synchronized(buffer) {
buffer.addLast(adjustedMessage)
if (buffer.size > BUFFER_SIZE) {
buffer.removeFirst()
}
}
if (exception != null && priority == Log.ERROR) {
Bugsnag.notify(exception)
}
}
companion object {
private const val BUFFER_SIZE = 200
private fun priorityToString(priority: Int) = when (priority) {
Log.ERROR -> "E"
Log.WARN -> "W"
Log.INFO -> "I"
Log.DEBUG -> "D"
else -> priority.toString()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment