# Logger使用
APP開發經常需要查看輸出,比如對象的值、服務端返回的Json數據結構等,我們需要實時的再控制臺看到輸出,這時就必須使用日志來解決了。
如何使用Logger呢?
第一步 配置build文件
`compile 'com.orhanobut:logger:2.1.1'`
第二步 在APP的Application中初始化Looger
第三步 初始化Looger的樣式
~~~
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(7) // (Optional) Hides internal method calls up to offset. Default 5
.logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("My custom tag") // (Optional) Global tag for every log. Default PRETTY_LOGGER
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
~~~
第四步,打印你想打印的一切
比如
~~~
Logger.d("debug");
Logger.e("error");
Logger.w("warning");
Logger.v("verbose");
Logger.i("information");
Logger.wtf("wtf!!!!");
~~~
打印結果如下:

# 在點餐系統中是如何使用日志輸出的呢?
首先,做了一層簡單的封裝
~~~
public class AppLog {
public static final String TAG="ycf";
static {
Logger.addLogAdapter(new AndroidLogAdapter());
}
public static void debug(Object objets){
Logger.d(objets);
}
public static void json(String json){
Logger.json(json);
}
}
~~~
在APP的點餐列表里,是這樣使用Log日志的
~~~
requestParams.add("items", jsonArray.toString());
AppLog.debug(jsonArray.toString());
client.post(Constants.server_url + Constants.submit_order, requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
AppLog.debug(new String(responseBody));
JSONObject jsonObject = JSON.parseObject(new String(responseBody));
if (jsonObject.get("code").equals(0)) {
AppLog.debug(jsonObject.get("msg"));
Intent intent = new Intent(HomeActivity.this, OrderResultActivity.class);
intent.putExtra("orderid", jsonObject.getJSONObject("data").getString("orderId"));
intent.putExtra("openid", openid);
startActivity(intent);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
~~~