全局控制log工具类

全局控制log工具类

在android中使用log调试,当发布版本的时候,往往需要将log调试的相关代码注释,通常比较麻烦。
使用全局控制类log就比较方便。

1、在Application中配置log调试信息是否显示;

代码块

/*
 * Author:xwj
 * date:2018/8/25 下午1:22
 * 全局控制log工具类
 */

public class Mlog{
private Mlog()
{
    throw new UnsupportedOperationException("cannot be instantiated");
}
// 是否需要打印bug,可以在application的onCreate函数里面初始化
public static boolean isDebug = true;
private static String TAG = "xwj";

public static void setMlog(String tag,boolean isdebug){
    TAG=tag;
    isDebug=isdebug;
}

// 下面四个是默认tag的函数
public static void i(String msg)
{
    if (isDebug)
        Log.i(TAG, msg);
}

public static void d(String msg)
{
    if (isDebug)
        Log.d(TAG, msg);
}

public static void e(String msg)
{
    if (isDebug)
        Log.e(TAG, msg);
}

public static void v(String msg)
{
    if (isDebug)
        Log.v(TAG, msg);
}

// 下面是传入自定义tag的函数
public static void i(String tag, String msg)
{
    if (isDebug)
        Log.i(tag, msg);
}

public static void d(String tag, String msg)
{
    if (isDebug)
        Log.i(tag, msg);
}

public static void e(String tag, String msg)
{
    if (isDebug)
        Log.i(tag, msg);
}

public static void v(String tag, String msg)
{
    if (isDebug)
        Log.i(tag, msg);
}

/**
 * 截断输出日志
 * @param msg
 */
public static void logi(String tag, String msg) {  //信息太长,分段打印
    //因为String的length是字符数量不是字节数量所以为了防止中文字符过多,
    //  把4*1024的MAX字节打印长度改为2001字符数
    if(isDebug) {
        int max_str_length = 2001 - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            Log.i(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        Log.i(tag, msg);
    }
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiawj8957

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值