Skip to content

PaulWoitaschek/Slimber

Repository files navigation

Slimber - Zero overhead Logging with Timber

Timber is a great library that makes logging very easy. However traditional logging like

if (BuildConfig.DEBUG) {
  Log.d(TAG, expensiveToStringObject.toString())
}

has one big advantage: The whole block will only be executed when you are in debug mode. If you pass anything to Timber, the String itself and the precalculations will be created. Even if you never planted a tree.

And this is where Slimber comes into play: It uses Kotlins inline capabilities so we can archieve the no-cost-effect because the whole block is executed only when there are trees planted. And remember: In Kotlin you can specify the last function in an arugment of a function outside the parentheses.

So you can do it like this:

d { "onCreate called with $expensiveToStringObject" }

There are also functions that take a throwable as the first paramter, so you can use them like

e(throwable) { "there was a severe error" }

Besides from that just use Timber for planting trees like you normally do

Timber.plant(DebugTree())

Installation

Add this to your root settings.gradle.kts

dependencyResolutionManagement {
  repositories {
    maven("https://jitpack.io")
  }
}

And then this dependency to your project:

dependencies {
  implementation("com.github.PaulWoitaschek:Slimber:x.y.z")
}