Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper class for Timber logging offering static method calls, e.g. TimberLogger.d("Hello %s", user.name). Evaluates BuildConfig.DEBUG to decide if an instance of Timber.DEBUG or Timber.PROD will be used.
package de.bsr.android.util;
import com.example.app.BuildConfig;
import timber.log.Timber;
/**
* Wrapper class for Timber that evaluates <code>BuildConfig.DEBUG</code> on construction
* and uses Timber.DEBUG or Timber.PROD appropriately. In PROD mode, no logging is done at all.
*
* @author Sebastian Engel <engel.sebastian@gmail.com>
*/
public class TimberLogger {
private static Logger instance;
private Timber timber;
private TimberLogger() {
timber = BuildConfig.DEBUG ? Timber.DEBUG : Timber.PROD;
}
private static TimberLogger getInstance() {
if (instance == null) {
instance = new TimberLogger();
}
return instance;
}
public static void d(String s, Object... objects) {
getInstance().timber.d(s, objects);
}
public static void d(Throwable throwable, String s, Object... objects) {
getInstance().timber.d(throwable, s, objects);
}
public static void i(String s, Object... objects) {
getInstance().timber.i(s, objects);
}
public static void i(Throwable throwable, String s, Object... objects) {
getInstance().timber.i(throwable, s, objects);
}
public static void w(String s, Object... objects) {
getInstance().timber.w(s, objects);
}
public static void w(Throwable throwable, String s, Object... objects) {
getInstance().timber.w(throwable, s, objects);
}
public static void e(String s, Object... objects) {
getInstance().timber.e(s, objects);
}
public static void e(Throwable throwable, String s, Object... objects) {
getInstance().timber.e(throwable, s, objects);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment