@asce1885
2015-08-12T08:13:53.000000Z
字数 3048
阅读 451
新框架文档
Inspired by orhanobut's logger and JakeWharton's timber
@author ASCE1885的 Github 简书 微博 CSDN
在第三方app的Application类的onCreate函数中进行日志树的初始化操作,可以种植多棵日志树,默认提供了三种类型的日志树:
1. HFLogcatTree:用于把日志打印到logcat中
2. HFFileTree:用于把日志打印到文件中
3. HFHollowTree:空实现,提供给调用者继承,从而实现自己的日志树(例如可用于异常和Crash日志上报等)
初始化代码如下:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 为了防止使用者在应用销毁时漏调用资源释放函数,在初始化时调用一遍
HFLogger.uprootAll();
if (BuildConfig.DEBUG) {
// 实例化logcat日志树,并对其进行自定义设置
HFLogcatTree logcatTree = new HFLogcatTree();
logcatTree.init().setMethodCount(1); // 设置显示的函数调用层级为1
// 种植logcat日志树
HFLogger.plant(logcatTree);
} else {
HFLogger.plant(new CrashReportingTree());
}
}
}
释放的代码可以根据各个app的具体实现,只要保证放在app退出的时候调用即可,例如如果app是在MainActivity的onDesroy函数中退出时,调用代码如下:
@Override
protected void onDestroy() {
super.onDestroy();
if (BuildConfig.DEBUG) {
HFLogger.uproot(new HFLogcatTree());
} else {
HFLogger.uproot(new CrashReportingTree());
}
// 如果种植了多棵日志树,那么可以使用 HFLogger.uprootAll()一次性移除
}
在初始化HFLogger之后,就可以在app全局调用HFLogger提供的各种级别日志记录功能。
注意,查看logcat时要确保不要打开logcat的自动换行功能(use soft wraps),否则会导致Logcat日志记录格式化混乱:
HFLogger.d("我认为全球市场可能需要五台计算机。");
在logcat中输出结果为:
HFLogger.d("我认为全球市场可能需要五台计算机。", 3);
在logcat中输出结果为:
HFLogger.d("PAIC", "我认为全球市场可能需要五台计算机。");
在logcat中输出结果为:
HFLogger.d("PAIC", "我认为全球市场可能需要五台计算机。", 4);
在logcat中输出结果为:
try {
Class clazz = Class.forName("NotExistClassName");
} catch (ClassNotFoundException e) {
HFLogger.e(e);
}
在logcat中输出结果为:
static final String JSON_CONTENT = "{\"widget\": {" +
" \"debug\": \"on\"," +
" \"window\": {" +
" \"title\": \"Sample HFLogger Widget\"," +
" \"name\": \"main_window\"," +
" \"width\": 500," +
" \"height\": 500" +
" },\n" +
" \"image\": { " +
" \"src\": \"Images/Sun.png\"," +
" \"name\": \"sun1\"," +
" \"hOffset\": 250," +
" \"vOffset\": 250," +
" \"alignment\": \"center\"" +
" },\n" +
" \"text\": {" +
" \"data\": \"Click Here\"," +
" \"size\": 36," +
" \"style\": \"bold\"," +
" \"name\": \"text1\"," +
" \"hOffset\": 250," +
" \"vOffset\": 100," +
" \"alignment\": \"center\"," +
" \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"" +
" }" +
"}} ";
HFLogger.json(JSON_CONTENT);
在logcat中输出结果为:
static final String XML_CONTENT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<recipe type=\"dessert\">\n" +
" <recipename cuisine=\"american\" servings=\"1\">Ice Cream Sundae</recipename>\n" +
" <ingredlist>\n" +
" <listitem>\n" +
" <quantity units=\"tablespoons\">1</quantity>\n" +
" <itemdescription>nuts</itemdescription>\n" +
" </listitem>\n" +
" <listitem>\n" +
" <quantity units=\"each\">1</quantity>\n" +
" <itemdescription>cherry</itemdescription>\n" +
" </listitem>\n" +
" </ingredlist>\n" +
" <directions>\n" +
" <step>Using ice cream scoop, place vanilla ice cream into bowl.</step>\n" +
" <step>Drizzle chocolate syrup or chocolate fudge over the ice cream.</step>\n" +
" <step>Sprinkle nuts over the mound of chocolate and ice cream.</step>\n" +
" <step>Place cherry on top of mound with stem pointing upward.</step>\n" +
" <step>Serve.</step>\n" +
" </directions>\n" +
" <variations>\n" +
" <option>Replace nuts with raisins.</option>\n" +
" <option>Use chocolate ice cream instead of vanilla ice cream.</option>\n" +
" </variations>\n" +
" <preptime>5 minutes</preptime>\n" +
"</recipe>";
HFLogger.xml(XML_CONTENT);
在logcat中输出结果为: