Android输出Log控制

本文介绍了如何在Android应用中进行Log输出的控制,特别是区分测试版本和打包版本的Log。通过自定义LogUtil类,并利用DebugMode来决定是否打印Log,使得在开发过程中能够方便地进行调试,同时在发布时避免Log信息泄露。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先确定测试版本Log与打包版本Log

自定义LogUtil 通过DeBugMode控制是否输出Log

在需要加Log的地方使用LogUtil中的方法

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class LogUtil {
   public static final String TAG = "log";

   public static void log(String tag, String msg, boolean isDebug, int type) {
      if (isDebug) {
         switch (type) {
         case Log.VERBOSE:
            Log.v(tag, msg);
            break;
         case Log.DEBUG:
            Log.d(tag, msg);
            break;
         case Log.INFO:
            Log.i(tag, msg);
            break;
         case Log.WARN:
            Log.w(tag, msg);
            break;
         case Log.ERROR:
            Log.e(tag, msg);
            break;
         default:
            Log.v(tag, msg);
            break;
         }
      }
   }

   public static void log(String tag, String msg, int type) {
      log(tag, msg, Constant.DebugMode, type);
   }

   public static void log(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.VERBOSE);
   }

   public static void e(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.ERROR);
   }

   public static void i(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.INFO);
   }

   public static void w(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.WARN);
   }

   public static void v(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.VERBOSE);
   }

   public static void d(String tag, String msg) {
      log(tag, msg, Constant.DebugMode, Log.DEBUG);
   }

   public static void log(String msg) {
      log(TAG, msg, Constant.DebugMode, Log.VERBOSE);
   }

   public static void xn(String msg) {
      log("xn", msg, Constant.DebugMode, Log.DEBUG);
   }

   /**
    * 显示toast
    * 
    * @param context
    * @param text
    * @param duration
    */
   public static void show(Context context, String text, int duration) {
      if (context == null || StrUtil.isEmpty(text)) {
         log(TAG, "loggerUtil show args is invalid!!!", Log.ERROR);
         return;
      }
      if (duration >= 3000) {
         duration = Toast.LENGTH_LONG;
      } else {
         duration = Toast.LENGTH_SHORT;
      }
      Toast.makeText(context, text, duration).show();
   }

   public static void show(Context context, String text) {
      show(context, text, 0);
   }

   public static void showNetError(Context context) {
      if (context == null) {
         log(TAG, "showNetError context is null!!!", Log.ERROR);
         return;
      }
      try {
         show(context, "网络不好");
      } catch (Exception e) {
         e.printStackTrace();
         log(TAG, "showNetError toast error!!!", Log.ERROR);
      }
   }

   public static void System(String content) {
      if (Constant.DebugMode) {
         System.out.println(content);
      }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值