Created
February 20, 2023 15:35
-
-
Save johnkil/d59b619a0322abbc977c63afe435c483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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