前言
Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log更好的帮助我们定位问题,帮助调试,一直是我关注的问题。
今天就讲讲我封装的LogUtil吧,此篇文章参考以下连接:
高逼格Log日志,可以导航跳转的log
StackTraceElement用法
在此表示感谢,由于我”高逼格“已经被上文所用,我只能给这篇文章起个”超高逼格“的文章名了,但我这篇文章真不是为了装逼,而是为了更好的服务我们的开发
今天内容:
- StackTraceElement的用法
- LogUtil的优势
- LogUtil的封装
- LogUtil的使用
- 效果图
一. StackTraceElement的用法
StackTraceElement表示StackTrace(堆栈轨迹)中的一个元素,属性包括方法调用者的类名、方法名、文件名以及调用的行数,它是一个final类
下面给出一个使用范例:
public class Main {
public static void main(String[] args) {
new Main().methodA();
}
private void methodA() {
System.out.println("------进入methodA----------");
methodB();
}
private void methodB() {
System.out.println("------进入methodB----------");
StackTraceElement elements[] = Thread.currentThread().getStackTrace();
for (int i = 0; i < elements.length; i++) {
StackTraceElement stackTraceElement = elements[i];
String className = stackTraceElement.getClassName();
String methodName = stackTraceElement.getMethodName();
String fileName = stackTraceElement.getFileName();
int lineNumber = stackTraceElement.getLineNumber();
System.out.println("StackTraceElement数组下标 i=" + i + "----------fileName="
+ fileName + "----------className=" + className + "----------methodName=" + methodName + "----------lineNumber=" + lineNumber);
}
}
}
二. LogUtil的优势
我封装了一个LogUtil的类用于打印,它具备以下特色
- 查看包名,类名,方法名
- 可以定位到log的行数,点击进去可以定位到那一行
三. LogUtil的封装
下面给出LogUtil的代码:
package com.android.base;
/**
* Created by Administrator on 2018/03/10.
*/
public class LogUtil {
public static boolean LOG = true;
private static final String TAG = "pei";
private static final String LEVEL_I = "i";
private static final String LEVEL_D = "d";
private static final String LEVEL_W = "w";
private static final String LEVEL_V = "v";
private static final String LEVEL_E = "e";
private LogUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
}
public static void i(String tag, String msg) {
if (LOG) {
printLog(formatLog(tag), msg, LogUtil.LEVEL_I);
}
}
public static void i(String msg) {
if (LOG) {
printLog(formatLog(TAG), msg, LogUtil.LEVEL_I);
}
}
public static void d(String tag, String msg) {
if (LOG) {
printLog(formatLog(tag), msg, LogUtil.LEVEL_D);
}
}
public static void d(String msg){
if (LOG) {
printLog(formatLog(TAG), msg, LogUtil.LEVEL_D);
}
}
public static void w(String tag, String msg) {
if (LOG) {
printLog(formatLog(tag), msg, LogUtil.LEVEL_W);
}
}
public static void w(String msg){
if (LOG) {
printLog(formatLog(TAG), msg, LogUtil.LEVEL_W);
}
}
public static void v(String tag, String msg) {
if (LOG) {
printLog(formatLog(tag), msg, LogUtil.LEVEL_V);
}
}
public static void v(String msg) {
if (LOG) {
printLog(formatLog(TAG), msg, LogUtil.LEVEL_V);
}
}
public static void e(String tag, String msg) {
if (LOG) {
printLog(formatLog(tag), msg, LogUtil.LEVEL_E);
}
}
public static void e(String msg) {
if (LOG) {
printLog(formatLog(TAG), msg, LogUtil.LEVEL_E);
}
}
private static String formatLog(String tag) {
StackTraceElement traceElements[] = Thread.currentThread().getStackTrace();
StackTraceElement element = traceElements[4];
String className = element.getClassName();
String methodName = element.getMethodName();
String fileName = element.getFileName();
int lineNumber = element.getLineNumber();
if(className!=null&&className.contains(".")){
className=className.substring(className.lastIndexOf(".")+1,className.length());
}
StringBuffer buffer = new StringBuffer();
buffer.append(tag + ":");
buffer.append(className + ".");
buffer.append(methodName + "(");
buffer.append(fileName + ":");
buffer.append(lineNumber + ")");
return buffer.toString();
}
private static void printLog(String tag, String msg, String type) {
int count = msg.length();
if (count > 4000) {
for (int i = 0; i < count; i += 4000) {
if (i + 4000 < count) {
printByLogType(tag, msg.substring(i, i + 4000), type);
} else {
printByLogType(tag, msg.substring(i, msg.length()), type);
}
}
} else {
printByLogType(tag, msg, type);
}
}
private static void printByLogType(String tag, String msg, String type) {
switch (type) {
case LogUtil.LEVEL_I:
android.util.Log.i(tag, msg);
break;
case LogUtil.LEVEL_D:
android.util.Log.d(tag, msg);
break;
case LogUtil.LEVEL_W:
android.util.Log.w(tag, msg);
break;
case LogUtil.LEVEL_V:
android.util.Log.v(tag, msg);
break;
case LogUtil.LEVEL_E:
android.util.Log.e(tag, msg);
break;
default:
break;
}
}
}
四. LogUtil的使用
4.1 快速打印log
想快速打印log,你可以这样调用(tag为pei):
LogUtil.w("=====測試按鈕被點擊了====");
4.2 定义自己的tag日志
如果你想定义一个tag=”abc“的log方便自己调试,你可以这样:
LogUtil.w("abc","=====測試按鈕被點擊了====");
打印出的log类似下面这样:
image.png
里面包含包名,日志级别,tag,类名,方法名和log的行数
五. 效果图
下面给出效果图
2.gif
ok,今天就讲到这里了,谢谢诶。
网友评论