Android开发中,不可避免的需要使用日志Log.Android原生的Log每次都需要指定Tag,而且指定Tag之后依然很难定位到确定位置.这就引出今天要讲的工具--Timber.
Timber 介绍
github地址
依赖地址:
implementation 'com.jakewharton.timber:timber:4.7.0'
官方介绍:
This is a logger with a small, extensible API which provides utility on top of Android's normal Log class.
I copy this class into all the little apps I make. I'm tired of doing it. Now it's a library.
Behavior is added through Tree instances. You can install an instance by calling Timber.plant. Installation of Trees should be done as early as possible. The onCreate of your application is the most logical choice.
The DebugTree implementation will automatically figure out from which class it's being called and use that class name as its tag. Since the tags vary, it works really well when coupled with a log reader like Pidcat.
There are no Tree implementations installed by default because every time you log in production, a puppy dies.
注:以上英文只是copy下来装13用,不用硬啃~~~
简单来说:
- Timber是一款可扩展的Logger工具
- Timber通过Timber.plant来添加tree实例
- Timber需要在使用前添加完成tree实例,最好在Application的onCreate中实现
- Timber默认实现的DebugTree将调用它的类的名称作为Tag(没有指定tag时使用)
通过以上几点,我们总结一下:
Timber需要在Application的onCreate中通过Timber.plant添加tree实例(默认Timber是没有Tree实例的).
Timber的使用
1. 初探
为了防止手忙脚乱,我们直接来看官方Demo的初始化:
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.plant(new DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}
}
像大部分的博客中介绍的一样,Timber的初始化根据打包状态(BuildConfig.DEBUG)的不同,添加不同的tree实例.
但是如果真的copy这段代码到项目中就会发现
哎,我CrashReportingTree呢?
在源码中得知,CrashReportingTree是框架外实现的类,源码如下
/**
* A tree which logs important information for crash reporting.
*/
private static class CrashReportingTree extends Timber.Tree {
@Override
protected void log(int priority, String tag, @NonNull String message, Throwable t) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) {
return;
}
FakeCrashLibrary.log(priority, tag, message);
if (t != null) {
if (priority == Log.ERROR) {
FakeCrashLibrary.logError(t);
} else if (priority == Log.WARN) {
FakeCrashLibrary.logWarning(t);
}
}
}
}
这次学乖了,不先copy代码,我们看一下这段源码内不熟悉的类FakeCrashLibrary
,源码如下
/** Not a real crash reporting library! */
public final class FakeCrashLibrary {
public static void log(int priority, String tag, String message) {
// TODO add log entry to circular buffer.
}
public static void logWarning(Throwable t) {
// TODO report non-fatal warning.
}
public static void logError(Throwable t) {
// TODO report non-fatal error.
}
private FakeCrashLibrary() {
throw new AssertionError("No instances.");
}
}
not a real crash reporting library:不是真正的崩溃报告库
就是说,CrashReportingTree()其实并没有任何意义~
整这半天,Timber是在闹呢!
其实,Timber这么做是为了提供一种思路,告诉咱们开发者Timber可以自己写Tree实例,并且根据不同情境使用自定义Tree.如此可害苦了初次接触Timber的人.
2. 无差错使用
在日常使用中,我们除了开发中需要日志外,其他并没有过硬的需求.
所以,初始化时,我们可以去掉未成熟的CrashReportingTree,仅设置DebugTree:
/**
* 仅在Debug时初始化Timber
*/
if (BuildConfig.DEBUG) {
Timber.plant(new DebugTree());
}
也可以将DebugTree添加到Timber中,通过控制isLoggable
来控制日志的输出:
Timber.plant(new DebugTree() {
@Override
protected boolean isLoggable(@Nullable String tag, int priority) {
return BuildConfig.DEBUG;
}
});
3. 测试日志输出
Timber.v("疯狂输出吧!");
Timber.d("疯狂输出吧!");
Timber.i("疯狂输出吧!");
Timber.w("疯狂输出吧!");
Timber.e("疯狂输出吧!");
Timber.wtf("疯狂输出吧!");