默认的Log.e等方法,不显眼,调用底层库的状况下,底层库的打印太多了。本身的日志很难找,虽然可以搜索,可是本身的日志会夹在一堆的别人的日志里面。html
想找一个方便的日志插件。java
参考: Android Log框架推荐,同时在github上搜索“Android Log",选择logger(https://github.com/orhanobut/logger)做为插件。android
基本的使用上面有说明的,我另外想要在Android界面上看到日志,在程序运行中。git
写了个MyLogAdapter继承自AndroidLogAdapter,在重写的log方法中将日志保存下来,而后在界面上显示出来。github
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); } } }
另外为了本身方便使用,封装了一些方法。app
界面上就是显示在一个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,显示不一样颜色,写了个一个LogListAdapteride
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; } }
使用上this
private void showLogs(List<LogInfo> logInfoList){ ListView listView = findViewById(R.id.lvLogList); LogListAdapter logAdapter=new LogListAdapter(logInfoList); listView.setAdapter(logAdapter); }