Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isLoggable of individual Trees not entirely respected #324

Open
gabrielittner opened this issue Jun 28, 2018 · 2 comments
Open

isLoggable of individual Trees not entirely respected #324

gabrielittner opened this issue Jun 28, 2018 · 2 comments

Comments

@gabrielittner
Copy link

(master branch)

The extension Timber.log(Int, Throwable, () -> String) method calls isLoggable() which will be true when any of the installed Trees return true. It will then go on to call rawLog on all Trees, even those where isLoggable() returns true.

For example

class LogcatTree2 : Tree() {
  override fun isLoggable(priority: Int, tag: String?): Boolean {
    return priority >= Log.ERROR
  }
  
  override fun performLog(priority: Int, tag: String?, throwable: Throwable?, message: String?) {
    Log.e("second tree", "don't call me")
  }
}

Timber.plant(LogcatTree())
Timber.plant(LogcatTree2())
Timber.warn { "foo" }

prints

W/App: foo
E/second tree: don't call me
@JakeWharton
Copy link
Owner

There's not really a good way to handle this

@consp1racy
Copy link

What we have

inline fun Timber.log(priority: Int, throwable: Throwable? = null, message: () -> String) {
  if (isLoggable(priority, null)) {
    rawLog(priority, null, throwable, message())
  }
}

What should solve this

inline fun Timber.log(priority: Int, throwable: Throwable? = null, message: () -> String) {
  if (isLoggable(priority, null)) {
    log(priority, null, throwable, message())
  }
}

We still get the message evaluated just once and each tree logs only if its own isLoggable returns true. The only downside is that each tree will call it twice.

Which... well, Log.isLoggable goes to native, but that's for debug builds, so who cares.

What else did I miss?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants