Android常用工具类之 Log工具类

Log工具类推荐一个开源的工具:
https://github.com/MustafaFerhan/DebugLog
源码只有一个类DebugLog.java
打印出来的内容也很简洁,但是很方便:
使用方法:

DebugLog.e("I am an error log");

打印效果:

4959-4959/com.example.pc.myapplication E/MainActivity.java﹕ [onClick:45]I am an error log

可以看出:
1. 自动打印Tag,不需要自己定义Tag,会打印出该log所在的类名
2. 自动获取打印的函数。本例中该log在onClick函数重打印
3. 自动获取打印log的位置的代码行数,便于快速定位。

其他好处:
源码内通过

public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

来确定是否是debug模式,在 build variant 是 ‘release’的时候,会自动隐藏log

下面贴出源码,感兴趣的可以去github中查看更多详细内容。


/***
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

For more information, please refer to <http://unlicense.org/>
*/

package com.example.pc.myapplication;

import android.util.Log;


/**
 * @date 21.06.2012
 * @author Mustafa Ferhan Akman
 * 
 * Create a simple and more understandable Android logs. 
 * */

public class DebugLog{

    static String className;
    static String methodName;
    static int lineNumber;

    private DebugLog(){
        /* Protect from instantiations */
    }

    public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

    private static String createLog( String log ) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        buffer.append(methodName);
        buffer.append(":");
        buffer.append(lineNumber);
        buffer.append("]");
        buffer.append(log);

        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements){
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    public static void e(String message){
        if (!isDebuggable())
            return;

        // Throwable instance must be created before any methods  
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }

    public static void i(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }

    public static void d(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }

    public static void v(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }

    public static void w(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }

    public static void wtf(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.wtf(className, createLog(message));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值