Last active
November 28, 2020 11:31
-
-
Save vkay94/0315a3a21f7dbdff66073bbcccdf0dff to your computer and use it in GitHub Desktop.
Custom Timber trees which print logs with lines and method names for faster navigation
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 de.vkay.android | |
import android.util.Log | |
import timber.log.Timber | |
class TimberTree { | |
class Debug : Timber.DebugTree() { | |
override fun createStackElementTag(element: StackTraceElement): String? { | |
val parts = super.createStackElementTag(element)?.split("$") | |
?: return super.createStackElementTag(element) | |
if (parts.size >= 2) { | |
return "(${element.fileName}:${element.lineNumber}) ${parts[1]}" | |
} | |
// https://stackoverflow.com/a/49216400/5199098 | |
return "(${element.fileName}:${element.lineNumber}) ${element.methodName}" | |
} | |
} | |
class Releasee : Timber.DebugTree() { | |
override fun isLoggable(tag: String?, priority: Int): Boolean { | |
return priority == Log.WARN || priority == Log.ERROR | |
} | |
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { | |
if (isLoggable(tag, priority)) { | |
super.log(priority, tag, message, 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
class App : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
Timber.plant(if (BuildConfig.DEBUG) TimberTree.Debug() else TimberTree.Releasee()) | |
/* ... */ | |
} | |
/* ... */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment