创建LogUtils工具类
2016-06-08 本文已影响132人
looper1211
在Android开发中,我们常常用打印log的方式来调试我们的应用。但是Android官方提供的Log类在实际项目使用中,也不是非常方便。当程序出现错误时,我们最希望的就是这个Log类能帮我们定位到是哪个类的哪个方法,甚至于是那一行出现了错误。这样就能给我们的调试带来很大的便利。同时我们也应该想到为了应用程序的安全起见,在app正式上线之前,我们应该要把打印日志的功能关闭掉,以防别人通过Log来破解你的应用。
现在附上自己写的Log工具类,先来看测试效果
测试代码:
测试结果:
打印结果注意:工具类中通过获取当前线程堆栈信息来获取所需信息,如果不了解,请参考资料 获取当前线程状态
import android.text.TextUtils;
import android.util.Log;
/**
* LogUtils工具说明:
* 1 只输出等级大于等于LEVEL的日志
* 所以在开发和产品发布后通过修改LEVEL来选择性输出日志.
* 当LEVEL=NOTHING则屏蔽了所有的日志.
* 2 v,d,i,w,e均对应两个方法.
* 若不设置TAG或者TAG为空则为设置默认TAG
*
* Created by Looper on 2016/6/8.
*/
public class LogUtils {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
public static final int INFO = 3;
public static final int WARN = 4;
public static final int ERROR = 5;
public static final int NOTHING = 6;
public static final int LEVEL = VERBOSE;
public static final String SEPARATOR = ", ";
public static void v(String message) {
if (LEVEL <= VERBOSE) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
String tag = getDefaultTag(stackTraceElement);
Log.v(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void v(String tag, String message) {
if (LEVEL <= VERBOSE) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
if (TextUtils.isEmpty(tag)) {
tag = getDefaultTag(stackTraceElement);
}
Log.v(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void d(String message) {
if (LEVEL <= DEBUG) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
String tag = getDefaultTag(stackTraceElement);
Log.d(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void d(String tag, String message) {
if (LEVEL <= DEBUG) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
if (TextUtils.isEmpty(tag)) {
tag = getDefaultTag(stackTraceElement);
}
Log.d(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void i(String message) {
if (LEVEL <= INFO) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
String tag = getDefaultTag(stackTraceElement);
Log.i(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void i(String tag, String message) {
if (LEVEL <= INFO) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
if (TextUtils.isEmpty(tag)) {
tag = getDefaultTag(stackTraceElement);
}
Log.i(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void w(String message) {
if (LEVEL <= WARN) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
String tag = getDefaultTag(stackTraceElement);
Log.w(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void w(String tag, String message) {
if (LEVEL <= WARN) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
if (TextUtils.isEmpty(tag)) {
tag = getDefaultTag(stackTraceElement);
}
Log.w(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void e(String message) {
if (LEVEL <= ERROR) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
String tag = getDefaultTag(stackTraceElement);
Log.e(tag, getLogInfo(stackTraceElement) + message);
}
}
public static void e(String tag, String message) {
if (LEVEL <= ERROR) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
if (TextUtils.isEmpty(tag)) {
tag = getDefaultTag(stackTraceElement);
}
Log.e(tag, getLogInfo(stackTraceElement) + message);
}
}
/**
* 获取默认的TAG名称.
* 比如在MainActivity.java中调用了日志输出.
* 则TAG为MainActivity
*/
public static String getDefaultTag(StackTraceElement stackTraceElement) {
String fileName = stackTraceElement.getFileName();
String stringArray[] = fileName.split("\\.");
String tag = stringArray[0];
return tag;
}
/**
* 输出日志所包含的信息
*/
public static String getLogInfo(StackTraceElement stackTraceElement) {
StringBuilder logInfoStringBuilder = new StringBuilder();
// 获取线程名
String threadName = Thread.currentThread().getName();
// 获取线程ID
long threadID = Thread.currentThread().getId();
// 获取文件名.即xxx.java
String fileName = stackTraceElement.getFileName();
// 获取类名.即包名+类名
String className = stackTraceElement.getClassName();
// 获取方法名称
String methodName = stackTraceElement.getMethodName();
// 获取日志输出行数
int lineNumber = stackTraceElement.getLineNumber();
logInfoStringBuilder.append("[ ");
// logInfoStringBuilder.append("threadID=" + threadID).append(SEPARATOR);
// logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
// logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
logInfoStringBuilder.append("lineNumber=" + lineNumber);
logInfoStringBuilder.append(" ] ");
return logInfoStringBuilder.toString();
}
}