Skip to content

Instantly share code, notes, and snippets.

@ktvipin27
Created June 16, 2020 14:11
Show Gist options
  • Save ktvipin27/e032fcabc08412d0f8c5d1e107751eeb to your computer and use it in GitHub Desktop.
Save ktvipin27/e032fcabc08412d0f8c5d1e107751eeb to your computer and use it in GitHub Desktop.
RemoteLog class for Timber configuration for pushing logs to Firebase firestore.
class TimberFirestoreTree(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.firestore.collection("logs/10-06-2020/${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) {
document("-DeviceDetails").set(deviceDetails)
document(timestamp.toString()).set(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()
}
}
@vani491
Copy link

vani491 commented Apr 23, 2022

Please provide a complete repository on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment