JakeWharton / timber

A logger with a small, extensible API which provides utility on top of Android's normal Log class.
https://jakewharton.github.io/timber/docs/5.x/
Apache License 2.0
10.43k stars 957 forks source link

Lazy Logging with lambdas #251

Open AlexTrotsenko opened 7 years ago

AlexTrotsenko commented 7 years ago

Given we have the need to write something like:

Timber.d("Something was done: " + foo + " and " + that + "with " + expensiveOperation());

It would be nice to avoid calls to expensiveOperation() when no logging is happening (e.g. when "forest" is empty, etc.).

Since now lambda expressions are supported in default toolchain, the syntax could be like this:

Timber.d(() -> "Something was done: " + foo + " and " + that + "with " + expensiveOperation());

For example, it's implemented in Log4j 2.4.

AlexTrotsenko commented 7 years ago

@JakeWharton just in case: if you also think, that adding such API is a good idea - I am volunteering to implement the changes in pull request.

JakeWharton commented 7 years ago

The argument I've made against this in the past is that you're always going to be logging to at least one place. In production it's a ring buffer of events that get included in crash reports and in development it's that plus the actual Android log (if not more places).

Also your first example should be:

Timber.d("Something was done: %s and %s with %s", foo, that, expensiveOperation())

It doesn't change the behavior, but it at least doesn't waste time doing string formatting when there are no loggers.

This is related to #230 somewhat.