11. Android框架和工具之 Logger(调试代码)

1. Logger

Logger是android是一个简单、漂亮、功能强大的Android日志程序。

日志程序提供了 :

  • 线程信息Thread information
  • 类信息Class information
  • 方法信息Method information
  • 漂亮打印的JSON内容Pretty-print for json content
  • Clean output
  • Jump to source feature

Gradle

  compile 'com.orhanobut:logger:1.3'

2. 使用Logger,如下:

 package com.himi.loggerdemo;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer; import com.orhanobut.logger.Logger; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Log.d(TAG, "from Log"); // 必须初始化Logger,不然会空指针
Logger.init("myLogger"); Logger.d("from Logger");
Logger.e("hello");
Logger.w("hello");
Logger.v("hello"); Logger.wtf("hello"); // 生成一个JSON数据
JSONObject json = new JSONObject();
try {
json.put("title", "book1").put("price", "$11");
Logger.json(json.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 生成一个xml数据
try {
File file = new File(getFilesDir(), "data.xml");
FileOutputStream os = new FileOutputStream(file); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(os, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "student");
serializer.startTag(null, "name");
serializer.text("hebao");
serializer.endTag(null, "name");
serializer.startTag(null, "num");
serializer.text("24");
serializer.endTag(null, "num"); serializer.endTag(null, "student");
serializer.endDocument();
os.close(); XmlPullParser parser = Xml.newPullParser();
// 2.设置解析器的参数
InputStream inputStream = new FileInputStream(file);
parser.setInput(inputStream, "utf-8");
int type = parser.getEventType();
StringBuilder sb = new StringBuilder();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("name".equals(parser.getName())) {
// 这是name节点
String name = parser.nextText();
sb.append("--name:" + name);
} else if ("num".equals(parser.getName())) {
// 这是name节点
String num = parser.nextText();
sb.append("--num:" + num);
}
break;
}
type = parser.next();// 让解析器解析下一个元素
}
inputStream.close();
String text = sb.toString(); Logger.xml(text); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

查看Logcat,如下:

11. Android框架和工具之 Logger(调试代码)

上一篇:[转]把项目从VS2005升级到VS2013


下一篇:iOS开发--打印NSRange,CGRect等结构体