Last active
March 22, 2022 09:57
-
-
Save nostra13/88930c545e93cab8cb591ac672d36b91 to your computer and use it in GitHub Desktop.
Android Logger (concise wrapper of Timber) + CrashlyticsLogger
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
package com.nostra13.log | |
import android.util.Log | |
import com.google.firebase.crashlytics.FirebaseCrashlytics | |
class CrashlyticsLogger(private val isDebug: Boolean) : Logger() { | |
private val priorityStr = mapOf( | |
Log.VERBOSE to "V", | |
Log.DEBUG to "D", | |
Log.INFO to "I", | |
Log.WARN to "W", | |
Log.ERROR to "E", | |
Log.ASSERT to "A" | |
) | |
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { | |
if (isDebug || priority >= Log.INFO) { | |
FirebaseCrashlytics.getInstance().log("${priorityStr[priority]}/$tag: $message") | |
} | |
} | |
fun nonFatal(t: Throwable) = FirebaseCrashlytics.getInstance().recordException(t) | |
} | |
fun L.nonFatal(t: Throwable, message: String) { | |
w(t, message) | |
loggers().filterIsInstance<CrashlyticsLogger>().firstOrNull()?.nonFatal(t) | |
} | |
fun L.nonFatal(message: String) = nonFatal(RuntimeException(message), message) |
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
package com.nostra13.log | |
import timber.log.Timber | |
typealias Logger = Timber.Tree | |
typealias DebugLogger = Timber.DebugTree | |
object L { | |
fun add(vararg loggers: Logger) = Timber.plant(*loggers) | |
fun loggers(): List<Logger> = Timber.forest() | |
fun v(message: String) = Timber.v(message) | |
fun v(t: Throwable, message: String) = Timber.v(t, message) | |
fun v(t: Throwable) = Timber.v(t) | |
fun d(message: String) = Timber.d(message) | |
fun d(t: Throwable, message: String) = Timber.d(t, message) | |
fun d(t: Throwable) = Timber.d(t) | |
fun i(message: String) = Timber.i(message) | |
fun i(t: Throwable, message: String) = Timber.i(t, message) | |
fun i(t: Throwable) = Timber.i(t) | |
fun w(message: String) = Timber.w(message) | |
fun w(t: Throwable, message: String) = Timber.w(t, message) | |
fun w(t: Throwable) = Timber.w(t) | |
fun e(message: String) = Timber.e(message) | |
fun e(t: Throwable, message: String) = Timber.e(t, message) | |
fun e(t: Throwable) = Timber.e(t) | |
fun wtf(message: String) = Timber.wtf(message) | |
fun wtf(t: Throwable, message: String) = Timber.wtf(t, message) | |
fun wtf(t: Throwable) = Timber.wtf(t) | |
} |
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
timber_version = '4.7.1' | |
implementation "com.jakewharton.timber:timber:$timber_version" | |
if (BuildConfig.DEBUG) { | |
L.add(DebugLogger(), CrashlyticsLogger(BuildConfig.DEBUG)) | |
} else { | |
L.add(CrashlyticsLogger(BuildConfig.DEBUG)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment