欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-18 11:59:34
管理log一般有两种方法,博主推荐大家使用下面的第一种方法: 第一种方法: 第一步:定义一个logtools工具类,相信你能够看懂的,谁的log,可以用谁的名字做方法名...

管理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());


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

第二种方法:

在开发中经常要打印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);
 }
}