日志开源框架的话,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): ╚═══════════════════════════════════════════════════════════════════════════════════════
●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): ╚═══════════════════════════════════════════════════════════════════════════════════════
●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): ╚═══════════════════════════════════════════════════════════════════════════════════════
Logger还可以打印xml,日后追加。