用了很久的一个自己封装的Log工具类,因为使用过程中遇到打印JSON时,显示不全,数据太长了,一行显示不完,这边就需要自己判断换行了,比较麻烦,AndroidStudio可以直接以格式化JSON来显示Log,但里面有空格,只是能看看而已,不是很方便,以下是源码
/**
* @Function Log统一管理类
* @Auther Lion
* @Date 16/12/15
*/
public class LogUtil {
public static boolean isDebug = false; //是否需要打印bug,可以在application的onCreate函数里面初始化
private static String TAG = "LionYan";
private static final int STRING_MAXLENGTH = 1000; //Log单行的最大长度
private static int STRING_START = 0;
private static int STRING_END = 1000;
private static final int Level_Verbose = 1;
private static final int Level_Info = 2;
private static final int Level_Debug = 3;
private static final int Level_Warn = 4;
private static final int Level_Error = 5;
public static void setDebug(boolean bool) {
isDebug = bool;
}
// 下面四个是默认tag的函数
public static void i(String msg) {
handleMessage(Level_Info, TAG, msg);
}
public static void d(String msg) {
handleMessage(Level_Debug, TAG, msg);
}
public static void e(String msg) {
handleMessage(Level_Error, TAG, msg);
}
public static void v(String msg) {
handleMessage(Level_Verbose, TAG, msg);
}
public static void w(String msg) {
handleMessage(Level_Warn, TAG, msg);
}
// 下面是传入自定义tag的函数
public static void i(String tag, String msg) {
handleMessage(Level_Info, tag, msg);
}
public static void d(String tag, String msg) {
handleMessage(Level_Debug, tag, msg);
}
public static void e(String tag, String msg) {
handleMessage(Level_Error, tag, msg);
}
public static void v(String tag, String msg) {
handleMessage(Level_Verbose, tag, msg);
}
public static void w(String tag, String msg) {
handleMessage(Level_Warn, tag, msg);
}
public static void handleMessage(int level, String tag, String message) {
TAG = tag;
if (isDebug) {
STRING_START = 0;
STRING_END = 1000;
int msg_difference = message.length() - STRING_MAXLENGTH;
if (msg_difference > 0) {
STRING_END = STRING_MAXLENGTH;
for (; ; ) {
showLog(level, tag, message.substring(STRING_START, STRING_END));
STRING_START = STRING_END;
STRING_END += STRING_MAXLENGTH;
if (STRING_END >= message.length()) {
showLog(level, TAG, message.substring(STRING_START, message.length()));
return;
}
}
} else {
showLog(level, tag, message);
}
}
}
private static void showLog(int level, String tag, String message) {
switch (level) {
case Level_Verbose:
Log.v(tag, message);
break;
case Level_Debug:
Log.d(tag, message);
break;
case Level_Info:
Log.i(tag, message);
break;
case Level_Warn:
Log.w(tag, message);
break;
case Level_Error:
Log.e(tag, message);
break;
default:
break;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
之后还有一个自己封装的ToastUtils,可传context,不穿的话调用自定义Application的全局context
/**
* @Function Toast工具类
* @Auther Lion
* @Date 16/12/15
*/
public class ToastUtil {
public static boolean isShow = true;
public static Toast mToast;
public static void showToast(Context context, CharSequence message, int showDate) {
if (isShow) {
if (mToast != null) {
cancel();
}
Toast toast = Toast.makeText(context, message, showDate);
toast.show();
mToast = toast;
}
}
public static void showShort(Context context, CharSequence message) {
if (isShow) {
if (mToast != null) {
cancel();
}
showToast(context, message, Toast.LENGTH_SHORT);
}
}
public static void showShort(CharSequence message) {
if (isShow) {
if (mToast != null) {
cancel();
}
showToast(MainApplication.getGlobalContext(), message, Toast.LENGTH_SHORT);
}
}
public static void showLong(Context context, CharSequence message) {
if (isShow) {
if (mToast != null) {
cancel();
}
showToast(context, message, Toast.LENGTH_LONG);
}
}
public static void showLong(CharSequence message) {
if (isShow) {
if (mToast != null) {
mToast.cancel();
}
showToast(MainApplication.getGlobalContext(), message, Toast.LENGTH_LONG);
}
}
public static void cancel() {
if (mToast != null) {
mToast.cancel();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
最后,我将这两个工具类上传到了CSDN,需要的可以去下载,当然~直接在这篇博客里Copy过去也成~