日志封装

目前使用的手机不支持显示d和v的log,只能支持显示e、w、i日志,以下是我对log的封装,使用更加方便。

log当中增加当前时间显示,增加开关显示或者隐藏整个log,根据标签或者类别来设置显示或者隐藏log。

public class MyLogcatUtil {

    private static MyLogcatUtil instance = new MyLogcatUtil();

    private String myTag = "myTag";

    private boolean isShowE = true;

    private boolean isShowI = true;

    private boolean isShowW = true;

    private boolean isShow = true;

    private Map<String,String> notShowTagList;

    private MyLogcatUtil(){
        notShowTagList = new HashMap<String,String>();
    }

    public static MyLogcatUtil getInstance(){
        return instance;
    }

    public void elog(String tag , String content){
        if(!isShow){
            return ;
        }
        if(notShowTagList.get(tag)!=null){
            return;
        }
        if(isShowE){
            Log.e(myTag+tag,getTime()+content);
        }
    }

    public void ilog(String tag , String content){
        if(!isShow){
            return ;
        }
        if(notShowTagList.get(tag)!=null){
            return;
        }
        if(isShowI){
            Log.i(myTag+tag,getTime()+content);
        }
    }

    public void wlog(String tag , String content){
        if(!isShow){
            return ;
        }
        if(notShowTagList.get(tag)!=null){
            return;
        }
        if(isShowW){
            Log.w(myTag+tag,getTime()+content);
        }
    }

    public boolean isShowE() {
        return isShowE;
    }

    public void setShowE(boolean showE) {
        isShowE = showE;
    }

    public boolean isShowI() {
        return isShowI;
    }

    public void setShowI(boolean showI) {
        isShowI = showI;
    }

    public String getMyTag() {
        return myTag;
    }

    public void setMyTag(String myTag) {
        this.myTag = myTag;
    }

    public boolean isShowW() {
        return isShowW;
    }

    public void setShowW(boolean showW) {
        isShowW = showW;
    }

    public boolean isShow() {
        return isShow;
    }

    public void setShow(boolean show) {
        isShow = show;
    }

    public void addNotShowTag(String tag){
        this.notShowTagList.put(tag,tag);
    }

    public String getTime(){
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        return time;
    }

}

采用单例模式实现,确保设置显示或者隐藏对于整个日志输出有效。

posted @ 2016-10-27 18:24  黄大仙爱编程  阅读(187)  评论(0编辑  收藏  举报