工具类 - 日志(LogUtils)

android开发常用的日志工具类 LogUtils

1. 使用方法
// 1.配置
在Application中指定运行环境(测试环境/正式环境)

// 2. Activity中的使用
在基类BaseActivity中创建实例, 在子类中直接调用
protected LogUtils mLog = new LogUtils(this.getClass().getSimpleName());	// 基类中创建
mLog.d("------>> " + this.getClass().getName());	// 在基类的onCreate中调用

// 3.Fragment中的使用
在基类BaseFragment中创建实例, 在子类中直接调用
protected LogUtils mLog = new LogUtils(this.getClass().getSimpleName());	// 基类中创建
mLog.d("------>> " + this.getClass().getName());	// 在基类的onCreate中调用

// 4.网络请求中的使用
在基类BaseNet中创建实例, 在子类中直接调用
protected LogUtils mLog = new LogUtils(this.getClass().getSimpleName());	// 基类中创建
mLog.d("------>> " + this.getClass().getName());	// 在基类的onCreate中调用

2. 输出结果截图

日志截图

2. 源码
import android.util.Log;

/**
 * 日志工具类
 */
public class LogUtils {
    private String tag;
    public static boolean debug = true;

    public LogUtils(String tag) {
        this.tag = " --->> GLog <<--- " + tag;
    }

    public void d(String msg) {
        if (debug) {
            Log.d(tag, msg);
        }
    }

    public void i(String msg) {
        if (debug) {
            Log.i(tag, msg);
        }
    }

    public void w(String msg) {
        if (debug) {
            Log.w(tag, msg);
        }
    }

    public void e(String msg) {
        if (debug) {
            Log.e(tag, msg);
        }
    }

    public void e(Throwable e) {
        if (debug) {
            Log.e(tag, "", e);
        }
    }
}
发布了25 篇原创文章 · 获赞 2 · 访问量 1494

猜你喜欢

转载自blog.csdn.net/geaosu2/article/details/104315152
今日推荐