Android 开源框架Logger的使用

简介: Android 开源框架Logger的使用

日志开源框架的话,Logger不可不谈。简单的调用就能得到清晰的Log输出,对于日后调试和修改bug都是不错的选择。

 

◆Logger的官方地址

https://github.com/orhanobut/logger

◆Logger的使用

Eclipse:直接下载源码导入项目中

Android studio:compile 'com.orhanobut:logger:1.15'

◆使用方法

    import com.orhanobut.Logger.LogLevel;
    import com.orhanobut.Logger.Logger;
    import com.orhanobut.Logger.Settings;
    public class MainActivity extends Activity {
        ...
  protected void onCreate(Bundle savedInstanceState) {
    ...
    JSONObject json = createJson();
                Settings settings = Logger.init("EllisonLog"); // 配置tag
    settings.methodCount(3); // 配置Log中调用堆栈的函数行数
    // settings.hideThreadInfo(); // 隐藏Log中的线程信息
    settings.methodOffset(0); // 设置调用堆栈的函数偏移值,0的话则从打印该Log的函数开始输出堆栈信息
    settings.logLevel(LogLevel.FULL); // 设置Log的是否输出,LogLevel.NONE即无Log输出
    Logger.i("activity created"); // 打印info
    Logger.e(new Exception("test exception"), "error occured"); // 打印exception
    Logger.json(json.toString()); // 打印json
        }
  private JSONObject createJson() {
    try {
        JSONObject person = new JSONObject();
            person.put("phone", "12315");
            JSONObject address = new JSONObject();  
        address.put("country", "china");  
        address.put("province", "jiangsu");
        address.put("city", "nanjing");
        person.put("address", address);
        person.put("married", true);
        return person;
    } catch (JSONException e) {
      Logger.e(e, "create json error occured");
    }
    return null;
  }

◆ログ输出

 

源码插入的方式显示的log有点混乱,看不出来Logger漂亮的格式。

索性将原Log截图贴出来。

●info
07-14 17:58:38.246: I/EllisonLog(26917): ╔════════════════════════════════════════════════════════════════════════════════════════
07-14 17:58:38.247: I/EllisonLog(26917): ║ Thread: main
07-14 17:58:38.247: I/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.247: I/EllisonLog(26917): ║ Instrumentation.callActivityOnCreate  (Instrumentation.java:1111)
07-14 17:58:38.247: I/EllisonLog(26917): ║    Activity.performCreate  (Activity.java:6315)
07-14 17:58:38.248: I/EllisonLog(26917): ║       MainActivity.onCreate  (MainActivity.java:38)
07-14 17:58:38.248: I/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.248: I/EllisonLog(26917): ║ activity created
07-14 17:58:38.248: I/EllisonLog(26917): ╚═══════════════════════════════════════════════════════════════════════════════════════

1832b220aa754cd18c504acc7686a560.png

●exception
07-14 17:58:38.249: E/EllisonLog(26917): ╔════════════════════════════════════════════════════════════════════════════════════════
07-14 17:58:38.249: E/EllisonLog(26917): ║ Thread: main
07-14 17:58:38.249: E/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.249: E/EllisonLog(26917): ║ Instrumentation.callActivityOnCreate  (Instrumentation.java:1111)
07-14 17:58:38.249: E/EllisonLog(26917): ║    Activity.performCreate  (Activity.java:6315)
07-14 17:58:38.250: E/EllisonLog(26917): ║       MainActivity.onCreate  (MainActivity.java:39)
07-14 17:58:38.250: E/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.250: E/EllisonLog(26917): ║ error occured : java.lang.Exception: test exception
07-14 17:58:38.250: E/EllisonLog(26917): ║  at com.example.timeapidemo.MainActivity.onCreate(MainActivity.java:39)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.Activity.performCreate(Activity.java:6315)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2431)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2541)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.ActivityThread.access$900(ActivityThread.java:182)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1406)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.os.Looper.loop(Looper.java:148)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at android.app.ActivityThread.main(ActivityThread.java:5613)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at java.lang.reflect.Method.invoke(Native Method)
07-14 17:58:38.250: E/EllisonLog(26917): ║  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
07-14 17:58:38.251: E/EllisonLog(26917): ║  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
07-14 17:58:38.251: E/EllisonLog(26917): ╚═══════════════════════════════════════════════════════════════════════════════════════

1832b220aa754cd18c504acc7686a560.png

●json
07-14 17:58:38.251: D/EllisonLog(26917): ╔════════════════════════════════════════════════════════════════════════════════════════
07-14 17:58:38.252: D/EllisonLog(26917): ║ Thread: main
07-14 17:58:38.252: D/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.252: D/EllisonLog(26917): ║ Instrumentation.callActivityOnCreate  (Instrumentation.java:1111)
07-14 17:58:38.252: D/EllisonLog(26917): ║    Activity.performCreate  (Activity.java:6315)
07-14 17:58:38.252: D/EllisonLog(26917): ║       MainActivity.onCreate  (MainActivity.java:40)
07-14 17:58:38.252: D/EllisonLog(26917): ╟───────────────────────────────────────────────
07-14 17:58:38.252: D/EllisonLog(26917): ║ {
07-14 17:58:38.252: D/EllisonLog(26917): ║   "phone": "12315",
07-14 17:58:38.252: D/EllisonLog(26917): ║   "address": {
07-14 17:58:38.252: D/EllisonLog(26917): ║     "country": "china",
07-14 17:58:38.252: D/EllisonLog(26917): ║     "province": "jiangsu",
07-14 17:58:38.252: D/EllisonLog(26917): ║     "city": "nanjing"
07-14 17:58:38.252: D/EllisonLog(26917): ║   },
07-14 17:58:38.253: D/EllisonLog(26917): ║   "married": true
07-14 17:58:38.253: D/EllisonLog(26917): ║ }
07-14 17:58:38.253: D/EllisonLog(26917): ╚═══════════════════════════════════════════════════════════════════════════════════════

1832b220aa754cd18c504acc7686a560.png

Logger还可以打印xml,日后追加。

相关文章
|
存储 缓存 Java
Android体系课-开源框架-这是一份详细的Glide源码分析文章
最近在`组件化`开发中准备封装一个`图片加载库`,于是乎就有了这篇文章 本篇文章对`Glide`源码过程做了一个详细的讲解,也是为了记录下自己对`Glide`的理解,以后忘记还可以从这里查找。
|
XML Java API
『Android开源框架』用XXPermissions请求相机权限实现手电筒
今天实现一个小功能,调用相机权限实现手电筒,顺带学习一下CameraManager系统服务和两个好用的权限请求框架,主要推荐使用XXPermissions
1278 0
|
XML 缓存 JSON
Android开发常用开源框架
Android开发常用开源框架
Android开发常用开源框架
|
ARouter Java Maven
CC:基于组件总线的Android组件化开源框架
本文详细介绍了CC框架的设计思路与实现原理。通过阅读本文,你可以了解到设计一个基于组件总线方案的组件化框架所涉及到的相关技术点,甚至可以自己设计一套组件化框架出来。
9583 0