当前位置: 移动技术网 > IT编程>移动开发>Android > android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法

android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

蒋雯丽国籍,bm跨国企业人力资源共享,爱鹤失众的意思

管理log一般有两种方法,博主推荐大家使用下面的第一种方法:

第一种方法:

第一步:定义一个logtools工具类,相信你能够看懂的,谁的log,可以用谁的名字做方法名,如logli,这就是工程师li打印的日志

复制代码 代码如下:

import android.util.log;

public class logtools {

    public static boolean isshow = true;//上线模式

    //public static boolean isshow = false;//开发模式

    //ye工程师打出来的log
    public static void logye(string msg){
        if(isshow){
            log.i("ye", msg);
        }
    }
    //li工程师打出来的log
    public static void logli(string msg){
        if(isshow){
            log.i("lili", msg);
        }
    }

}

第二步:在程序中应用的方式是:

复制代码 代码如下:

logtools.logye("ontouchevent-----"+event.getaction());


第二种方法:

在开发中经常要打印log,但是在我们发布项目的时候是不能打印。为了方便操作log我们需要自己定义个log类然后在开发阶段将下面log_level 设置为6这样所有的log都能显示,在发布的时候我们将log_level 设置为0.这样log就非常方便管理了

复制代码 代码如下:

public class logger {
 public static int log_level = 0;
 public static int error = 1;
 public static int warn = 2;
 public static int info = 3;
 public static int debug = 4;
 public static int verbos = 5;

 
 public static void e(string tag,string msg){
  if(log_level>error)
  log.e(tag, msg);
 }

 public static void w(string tag,string msg){
  if(log_level>warn)
  log.w(tag, msg);
 }
 public static void i(string tag,string msg){
  if(log_level>info)
  log.i(tag, msg);
 }
 public static void d(string tag,string msg){
  if(log_level>debug)
  log.d(tag, msg);
 }
 public static void v(string tag,string msg){
  if(log_level>verbos)
  log.v(tag, msg);
 }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网