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): ╚════════════════════════════════════════════════════════════════════════════════════════

●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,日後追加。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章