Skip to content

Instantly share code, notes, and snippets.

@ktvipin27
Last active June 11, 2020 12:39
Show Gist options
  • Select an option

  • Save ktvipin27/515c42a1e074ab328bc9f1ec44df7e83 to your computer and use it in GitHub Desktop.

Select an option

Save ktvipin27/515c42a1e074ab328bc9f1ec44df7e83 to your computer and use it in GitHub Desktop.
Timber tree for adding logs to firebase realtime database.
class TimberRemoteTree(private val deviceDetails: DeviceDetails) : Timber.DebugTree() {
private val dateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
private val timeFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS a zzz", Locale.getDefault())
private val date = dateFormat.format(Date(System.currentTimeMillis()))
private val logRef = Firebase.database.getReference("logs/$date/${deviceDetails.deviceId}")
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (BuildConfig.REMOTE_LOG_ENABLED) {
val timestamp = System.currentTimeMillis()
val time = timeFormat.format(Date(timestamp))
val remoteLog = RemoteLog(priorityAsString(priority), tag, message, t.toString(), time)
with(logRef) {
updateChildren(mapOf(Pair("-DeviceDetails", deviceDetails)))
child(timestamp.toString()).setValue(remoteLog)
}
} else super.log(priority, tag, message, t)
}
private fun priorityAsString(priority: Int): String = when (priority) {
Log.VERBOSE -> "VERBOSE"
Log.DEBUG -> "DEBUG"
Log.INFO -> "INFO"
Log.WARN -> "WARN"
Log.ERROR -> "ERROR"
Log.ASSERT -> "ASSERT"
else -> priority.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment