[关闭]
@asce1885 2015-08-12T08:13:53.000000Z 字数 3048 阅读 451

HFLogger日志记录函数库

新框架文档


Inspired by orhanobut's logger and JakeWharton's timber
@author ASCE1885的 Github 简书 微博 CSDN

1)初始化和释放

在第三方app的Application类的onCreate函数中进行日志树的初始化操作,可以种植多棵日志树,默认提供了三种类型的日志树:
1. HFLogcatTree:用于把日志打印到logcat中
2. HFFileTree:用于把日志打印到文件中
3. HFHollowTree:空实现,提供给调用者继承,从而实现自己的日志树(例如可用于异常和Crash日志上报等)

初始化代码如下:

  1. public class MyApplication extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. // 为了防止使用者在应用销毁时漏调用资源释放函数,在初始化时调用一遍
  6. HFLogger.uprootAll();
  7. if (BuildConfig.DEBUG) {
  8. // 实例化logcat日志树,并对其进行自定义设置
  9. HFLogcatTree logcatTree = new HFLogcatTree();
  10. logcatTree.init().setMethodCount(1); // 设置显示的函数调用层级为1
  11. // 种植logcat日志树
  12. HFLogger.plant(logcatTree);
  13. } else {
  14. HFLogger.plant(new CrashReportingTree());
  15. }
  16. }
  17. }

释放的代码可以根据各个app的具体实现,只要保证放在app退出的时候调用即可,例如如果app是在MainActivity的onDesroy函数中退出时,调用代码如下:

  1. @Override
  2. protected void onDestroy() {
  3. super.onDestroy();
  4. if (BuildConfig.DEBUG) {
  5. HFLogger.uproot(new HFLogcatTree());
  6. } else {
  7. HFLogger.uproot(new CrashReportingTree());
  8. }
  9. // 如果种植了多棵日志树,那么可以使用 HFLogger.uprootAll()一次性移除
  10. }

2)日志记录

在初始化HFLogger之后,就可以在app全局调用HFLogger提供的各种级别日志记录功能。

注意,查看logcat时要确保不要打开logcat的自动换行功能(use soft wraps),否则会导致Logcat日志记录格式化混乱:

2.1)格式1

  1. HFLogger.d("我认为全球市场可能需要五台计算机。");

在logcat中输出结果为:

2.2)格式2

  1. HFLogger.d("我认为全球市场可能需要五台计算机。", 3);

在logcat中输出结果为:

2.3)格式3

  1. HFLogger.d("PAIC", "我认为全球市场可能需要五台计算机。");

在logcat中输出结果为:

2.4)格式4

  1. HFLogger.d("PAIC", "我认为全球市场可能需要五台计算机。", 4);

在logcat中输出结果为:

2.5)格式5

  1. try {
  2. Class clazz = Class.forName("NotExistClassName");
  3. } catch (ClassNotFoundException e) {
  4. HFLogger.e(e);
  5. }

在logcat中输出结果为:

2.6)格式6

  1. static final String JSON_CONTENT = "{\"widget\": {" +
  2. " \"debug\": \"on\"," +
  3. " \"window\": {" +
  4. " \"title\": \"Sample HFLogger Widget\"," +
  5. " \"name\": \"main_window\"," +
  6. " \"width\": 500," +
  7. " \"height\": 500" +
  8. " },\n" +
  9. " \"image\": { " +
  10. " \"src\": \"Images/Sun.png\"," +
  11. " \"name\": \"sun1\"," +
  12. " \"hOffset\": 250," +
  13. " \"vOffset\": 250," +
  14. " \"alignment\": \"center\"" +
  15. " },\n" +
  16. " \"text\": {" +
  17. " \"data\": \"Click Here\"," +
  18. " \"size\": 36," +
  19. " \"style\": \"bold\"," +
  20. " \"name\": \"text1\"," +
  21. " \"hOffset\": 250," +
  22. " \"vOffset\": 100," +
  23. " \"alignment\": \"center\"," +
  24. " \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"" +
  25. " }" +
  26. "}} ";
  27. HFLogger.json(JSON_CONTENT);

在logcat中输出结果为:

2.7)格式7

  1. static final String XML_CONTENT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  2. "<recipe type=\"dessert\">\n" +
  3. " <recipename cuisine=\"american\" servings=\"1\">Ice Cream Sundae</recipename>\n" +
  4. " <ingredlist>\n" +
  5. " <listitem>\n" +
  6. " <quantity units=\"tablespoons\">1</quantity>\n" +
  7. " <itemdescription>nuts</itemdescription>\n" +
  8. " </listitem>\n" +
  9. " <listitem>\n" +
  10. " <quantity units=\"each\">1</quantity>\n" +
  11. " <itemdescription>cherry</itemdescription>\n" +
  12. " </listitem>\n" +
  13. " </ingredlist>\n" +
  14. " <directions>\n" +
  15. " <step>Using ice cream scoop, place vanilla ice cream into bowl.</step>\n" +
  16. " <step>Drizzle chocolate syrup or chocolate fudge over the ice cream.</step>\n" +
  17. " <step>Sprinkle nuts over the mound of chocolate and ice cream.</step>\n" +
  18. " <step>Place cherry on top of mound with stem pointing upward.</step>\n" +
  19. " <step>Serve.</step>\n" +
  20. " </directions>\n" +
  21. " <variations>\n" +
  22. " <option>Replace nuts with raisins.</option>\n" +
  23. " <option>Use chocolate ice cream instead of vanilla ice cream.</option>\n" +
  24. " </variations>\n" +
  25. " <preptime>5 minutes</preptime>\n" +
  26. "</recipe>";
  27. HFLogger.xml(XML_CONTENT);

在logcat中输出结果为:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注