本文参考自:感谢 Android 开源日志库 Logger 使用教程
这个Logger的使用极其简单,要使用Logger需要添加的依赖库如下
dependencies {compile 'com.orhanobut:logger:1.15'
}
我们之所以用Logger工具就是为了能方便的统一管理日志,最起码的要求是测试阶段开启输出日志,上线时不输出日志,这个我们可以在application中做统一的设置,代码如下
/**这样设置以后当然还需要把自定义的application配置在xml配置文件中
* 自定义Application
*/
public class App extends Application{
final static String MY_TAG = "LoggerTest";
@Override
public void onCreate() {
super.onCreate();
Logger.init(MY_TAG)//自定义日志TAG
.logLevel(LogLevel.FULL);//测试阶段设置日志输出
// .logLevel(LogLevel.NONE);//产品上线设置日志不输出
}
}
现在可以开始使用了,很简单,直接贴示例代码:
//以下为Logger的适用之法
Logger.i("我是Logger.i输出", 0);
Logger.e("我是Logger.e输出", 0);
Logger.d("我是Logger.d输出");
String XML_CONTENT = "<china dn=\"nay\"><city quName=\"黑龙江\" pyName=\"heilongjiang\" " +
"cityname=\"哈尔滨\" state1=\"1\" state2=\"1\" stateDetailed=\"多云\"/><city quName=\"吉林\"" +
" pyName=\"jilin\" " +
"cityname=\"长春\" state1=\"0\" state2=\"0\" stateDetailed=\"晴\"/><city quName=\"辽宁\" " +
"pyName=\"liaoning\" " +
"cityname=\"沈阳\" state1=\"1\" state2=\"0\" stateDetailed=\"多云转晴\"/><city " +
"quName=\"海南\" pyName=\"hainan\" " +
"cityname=\"海口\" state1=\"22\" state2=\"21\" stateDetailed=\"中到大雨转小到中雨\"/></china>";
//输出上面的xml格式的数据
Logger.xml(XML_CONTENT);
String JSON_CONTENT = "{\"weatherinfo\":{\"city\":\"北京\",\"cityid\":\"101010100\"," +
"\"temp\":\"18\",\"WD\":\"东南风\",\"WS\":\"1级\",\"SD\":\"17%\",\"WSE\":\"1\"," +
"\"time\":\"17:05\",\"isRadar\":\"1\",\"Radar\":\"JC_RADAR_AZ9010_JB\"," +
"\"njd\":\"暂无实况\",\"qy\":\"1011\",\"rain\":\"0\"}}";
//输出上面的json格式的数据
Logger.json(JSON_CONTENT);
List<String> strList = new ArrayList<>();
strList.add("list 1");
strList.add("list 2");
strList.add("list 3");
//输出上面List数据
Logger.d(strList);
Map<String, String> map = new HashMap<>();
map.put("key_hello", "hello");
map.put("key_world", "world");
//输出上面Map数据
Logger.d(map);
//字符串格式化
Logger.d("hello %s %d", "world", 5);
//使用t()设置临时Log名,临时的 TAG 名被追加到自定义 TAG 的后面了
Logger.t("MyTag").d("hello world");
int[] num = new int[3];
try {
num[4] = 5;
} catch (Exception e){
Logger.e(e,"错误信息");
}
效果请看下面,按顺序比对输出结果
11-01 11:22:30.290 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ║ Thread: main
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:40)
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ║ 我是Logger.i输出
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui I/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.291 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ Thread: main
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:41)
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ 我是Logger.e输出
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:42)
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ 我是Logger.d输出
11-01 11:22:30.292 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.387 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:52)
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.388 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <?xml version="1.0" encoding="UTF-8"?>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <china dn="nay">
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <city quName="黑龙江" pyName="heilongjiang" cityname="哈尔滨" state1="1" state2="1" stateDetailed="多云"/>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <city quName="吉林" pyName="jilin" cityname="长春" state1="0" state2="0" stateDetailed="晴"/>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <city quName="辽宁" pyName="liaoning" cityname="沈阳" state1="1" state2="0" stateDetailed="多云转晴"/>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ <city quName="海南" pyName="hainan" cityname="海口" state1="22" state2="21" stateDetailed="中到大雨转小到中雨"/>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ </china>
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.389 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:58)
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ {
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "weatherinfo": {
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "city": "北京",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "cityid": "101010100",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "temp": "18",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "WD": "东南风",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "WS": "1级",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "SD": "17%",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "WSE": "1",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "time": "17:05",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "isRadar": "1",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "Radar": "JC_RADAR_AZ9010_JB",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "njd": "暂无实况",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "qy": "1011",
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ "rain": "0"
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ }
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ }
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.390 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:64)
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ [list 1, list 2, list 3]
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.391 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.392 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.392 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.392 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:69)
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ {key_hello=hello, key_world=world}
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Thread: main
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.393 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:71)
11-01 11:22:30.394 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.394 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ║ hello world 5
11-01 11:22:30.394 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.394 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ║ Thread: main
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ║ StickyNavActivity.onCreate (StickyNavActivity.java:73)
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ║ hello world
11-01 11:22:30.395 19890-19890/com.zgy.ldx.immersiveui D/LoggerTest-MyTag: ╚════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.396 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╔════════════════════════════════════════════════════════════════════════════════════════
11-01 11:22:30.396 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ Thread: main
11-01 11:22:30.396 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.396 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ Activity.performCreate (Activity.java:5998)
11-01 11:22:30.396 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ StickyNavActivity.onCreate (StickyNavActivity.java:79)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╟────────────────────────────────────────────────────────────────────────────────────────
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ 错误信息 : java.lang.ArrayIndexOutOfBoundsException: length=3; index=4
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at com.zgy.ldx.immersiveui.StickyNavActivity.onCreate(StickyNavActivity.java:77)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.Activity.performCreate(Activity.java:5998)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2447)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.ActivityThread.access$800(ActivityThread.java:176)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.os.Handler.dispatchMessage(Handler.java:111)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.os.Looper.loop(Looper.java:194)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at android.app.ActivityThread.main(ActivityThread.java:5578)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at java.lang.reflect.Method.invoke(Native Method)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at java.lang.reflect.Method.invoke(Method.java:372)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ║ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
11-01 11:22:30.397 19890-19890/com.zgy.ldx.immersiveui E/LoggerTest: ╚════════════════════════════════════════════════════════════════════════════════════════