默認的Log.e等方法,不顯眼,調用底層庫的情況下,底層庫的打印太多了。自己的日誌很難找,雖然能夠搜索,但是自己的日誌會夾在一堆的別人的日誌裏面。
想找一個方便的日誌插件。
參考: Android Log框架推薦,同時在github上搜索“Android Log",選擇logger(https://github.com/orhanobut/logger)作爲插件。
基本的使用上面有說明的,我另外想要在Android界面上看到日誌,在程序運行中。
寫了個MyLogAdapter繼承自AndroidLogAdapter,在重寫的log方法中將日誌保存下來,然後在界面上顯示出來。
private static List<LogInfo> logs=new LinkedList<LogInfo>();
public static List<String> logTags =new LinkedList<>();
@Override
public void log(int priority, @Nullable String tag, @NonNull String message) {
if(tag!=null && !logTags.contains(tag)){
logTags.add(tag);
}
SimpleDateFormat formatter=new SimpleDateFormat("[HH:mm:ss.SSS]");
String t=formatter.format(new Date());
super.log(priority, tag, t+message);
logs.add(new LogInfo(priority,this.tag,tag,message));
}
public class LogInfo {
public static String[] priorityNames={"0","1","v","d","i","w","e","7","8","9","10"};
public static int ID=0;
private int id=0;
private int priority;
private String adapterTag;
private String logTag;
private String message;
public int getPriority() {
return priority;
}
public String getLogTag() {
return logTag;
}
public String getAdapterTag() {
return adapterTag;
}
public LogInfo(int priority, @Nullable String adapterTag,@Nullable String logTag, @NonNull String message){
ID++;
id=ID;
this.priority=priority;
this.adapterTag =adapterTag;
this.logTag =logTag;
this.message=message;
}
@NonNull
@Override
public String toString() {
SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss.SSS");
String t=formatter.format(new Date());
if(logTag ==null){
return String.format("[%s][%s][%s][%s]\n%s",id,t,priorityNames[priority],adapterTag,message);
}
else{
return String.format("[%s][%s][%s][%s][%s]\n%s",id,t,priorityNames[priority],adapterTag,logTag,message);
}
}
}
另外爲了自己方便使用,封裝了一些方法。
界面上就是顯示在一個ListView裏面,提供了根據Level和Tag(AdapterTag合LogTag)過濾的功能。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LogActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/btnShowLog"
android:text="Load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:id="@+id/btnClearLog"
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cbLogLevelV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="VERBOSE"
android:textColor="@color/black"
android:checked="true"
android:tag="2"/>
<CheckBox
android:id="@+id/cbLogLevelD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DEBUG"
android:textColor="@color/blue"
android:checked="true"
android:tag="3"/>
<CheckBox
android:id="@+id/cbLogLevelI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="INFO"
android:textColor="@color/green"
android:checked="true"
android:tag="4"/>
<CheckBox
android:id="@+id/cbLogLevelW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WARN"
android:textColor="@color/orange"
android:checked="true"
android:tag="5"/>
<CheckBox
android:id="@+id/cbLogLevelE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ERROR"
android:textColor="@color/red"
android:checked="true"
android:tag="6"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="AdapterTag"/>
<Spinner
android:id="@+id/spinnerActivityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="LogTag"/>
<Spinner
android:id="@+id/spinnerTagList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/btnClearFilterLog"
android:text="Reset Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/lvLogList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
爲了在ListView中能夠區分不同Level,顯示不同顏色,寫了個一個LogListAdapter
public class LogListAdapter extends BaseAdapter {
private List<LogInfo> items;
public LogListAdapter(List<LogInfo> items){
this.items=items;
}
@Override
public int getCount() {
if(items==null)return 0;
return items.size();
}
@Override
public Object getItem(int position) {
if(items==null)return null;
return items.get(position);
}
@Override
public long getItemId(int position) {
LogInfo item=items.get(position);
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Log.d("LogListAdapter",convertView+"|"+parent);
LogInfo item=items.get(position);
TextView mTextView = new TextView(parent.getContext());
mTextView.setText(item.toString());
mTextView.setTextSize(15);
int p=item.getPriority();
if(p==2){
mTextView.setTextColor(Color.BLACK);
}
else if(p==3){
mTextView.setTextColor(Color.BLUE);
}
else if(p==4){
mTextView.setTextColor(0xFF008000);// 綠色
}
else if(p==5){
mTextView.setTextColor(0xFFFF6100);//橙色
}else if(p==6){
mTextView.setTextColor(Color.RED);
}
else{
mTextView.setTextColor(Color.GRAY);
}
return mTextView;
}
}
使用上
private void showLogs(List<LogInfo> logInfoList){
ListView listView = findViewById(R.id.lvLogList);
LogListAdapter logAdapter=new LogListAdapter(logInfoList);
listView.setAdapter(logAdapter);
}