您的位置:首页 > 其它

Timber

2015-08-20 23:49 197 查看
JakeWharton

Timber

使用方法

编译环境

只有一个400多行的类,可以单独把它复制出来放项目里,也可以通过Gradle引用

compile 'com.jakewharton.timber:timber:3.1.0'


API

定义一个Log树

public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}
}
}


使用,拥有Log的
d
v
i
e
w
方法,但不需要传入tag

Timber.d("Activity Created");


定义临时tag

Timber.tag("MainActivity");


原理

Tree


之前的
d
v
i
e
w
方法汇总

/**
* {@param priority} Log.ERROR Log.WARN
*/
private void prepareLog(int priority, Throwable t, String message, Object... args) {
// 1. 日志级别
if (!isLoggable(priority)) return;

// 2. message校验处理

// 3. 获取tag
String tag = getTag();

// 4. 输出日志
log(priority, tag, message, t);
}

// 1. 自定义优先级
protected boolean isLoggable(int priority) {
return true;
}

// 3. 获取tag:线程内传参
private final ThreadLocal<String> explicitTag = new ThreadLocal<String>();

String getTag() {
String tag = explicitTag.get();
if (tag != null) {
explicitTag.remove();
}
return tag;
}

// 实现真正的日志方法,如默认实现的DebugTree用Log,切割字符串等
protected abstract void log(int priority, String tag, String message, Throwable t);


森林

private static final List<Tree> FOREST = new CopyOnWriteArrayList<Tree>();

public static void plant(Tree tree) {...}
public static void uprootAll() {
FOREST.remove(tree)
}


Hugo

有时候需要打印方法的传参和返回值,甚至方法的执行时间,一个个拼接很麻烦,此时只要在方法上加上@DebugLog 就可以自动实现

其他

WebViewFragment
WebView使用的正确姿势

PhoneNumberUtils
PhoneNumberFormattingTextWatcher
Android 不为人知的API
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: