android上一个可追踪代码具体到函数某行的日志类

时间:2021-05-20

代码如下:
复制代码 代码如下:
package xiaogang.enif.utils;

/**
* The Class LogUtils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class LogUtils {

private final static int VERBOSE = 0;
private final static int DEBUG = 1;
private final static int INFO = 2;
private final static int WARN = 3;
private final static int ERROR = 4;
private final static int DEFAULT_LEVEL = -1;

private int level;

private final String clazz;

private static final String TAG = "LogUtils";

public static LogUtils getDebugLog(Class<?> clazz, int l) {
LogUtils log = new LogUtils(clazz);
log.level = l;
return log;
}

public static LogUtils getLog(Class<?> clazz) {
return new LogUtils(clazz);
}

public LogUtils(Class<?> clazz) {
this.clazz = "[" + clazz.getSimpleName() + "] ";
level = DEFAULT_LEVEL;
}

public void verbose(String message) {
verbose(message, null);
}

public void debug(String message) {
debug(message, null);
}

public void info(String message) {
info(message, null);
}

public void warn(String message) {
warn(message, null);
}

public void error(String message) {
error(message, null);
}

public void verbose(String message, Throwable t) {
if (VERBOSE < level)
return;
if (message != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void debug(String message, Throwable t) {
if (DEBUG < level)
return;
if (message != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void info(String message, Throwable t) {
if (INFO < level)
return;
if (message != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void warn(String message, Throwable t) {
if (WARN < level)
return;
if (message != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void error(String message, Throwable t) {
if (ERROR < level)
return;
if (message != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

private static int getLineNumber() {
return Thread.currentThread().getStackTrace()[5].getLineNumber();
}
}

好用的话,记得给好评,嘿嘿!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章