日志封装类
1.提供日志开关
2.判断是否是json字符串,如果是格式化json输出。
3.unicode中文解码
/**
* Created by chenjifang on 2017/3/29.* 日志打印类,
* 1.提供日志关闭开关
* 2.Json字符串可格式化成Json字符串输出
*/
public class Logger {
/**
* 日志开关
*/
private static boolean isOpenLog = true;
/**
* 是否开启Json格式化
*/
private static boolean isOpenJsonFormat = true;
//不添加TAG的时候
private static String TAG="Logger";
public static void debug(boolean status) {
isOpenLog = status;
}
public static void d(String tag, String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.d(tag,format(convertUnicode(message)));
} else {
android.util.Log.d(tag, message);
}
}
}
public static void d(String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
Log.d(TAG, "\n"+format(convertUnicode(message)));
} else {
android.util.Log.d(TAG, message);
}
}
}
public static void i(String tag, String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.i(tag, format(convertUnicode(message)));
} else {
android.util.Log.i(tag, message);
}
}
}
public static void i(String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.i(TAG,format(convertUnicode(message)));
} else {
android.util.Log.i(TAG, message);
}
}
}
public static void w(String tag, String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.w(tag,format(convertUnicode(message)));
} else {
android.util.Log.w(tag, message);
}
}
}
public static void w(String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.w(TAG,format(convertUnicode(message)));
} else {
android.util.Log.w(TAG, message);
}
}
}
public static void e(String tag, String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.e(tag,format(convertUnicode(message)));
} else {
android.util.Log.e(tag, message);
}
}
}
public static void e(String message) {
if (isOpenLog) {
if (isOpenJsonFormat&&isJsonFormat(message)) {
android.util.Log.e(TAG,format(convertUnicode(message)));
} else {
android.util.Log.e(TAG,message);
}
}
}
/**
* 格式化json数据
*
* @param jsonStr
* @return
*/
public static String format(String jsonStr) {
int level = 0;
StringBuffer jsonForMatStr = new StringBuffer();
for (int i = 0; i < jsonStr.length(); i++) {
char c = jsonStr.charAt(i);
if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
jsonForMatStr.append(getLevelStr(level));
}
switch (c) {
case '{':
case '[':
jsonForMatStr.append(c + "\n");
level++;
break;
case ',':
jsonForMatStr.append(c + "\n");
break;
case '}':
case ']':
jsonForMatStr.append("\n");
level--;
jsonForMatStr.append(getLevelStr(level));
jsonForMatStr.append(c);
break;
default:
jsonForMatStr.append(c);
break;
}
}
return jsonForMatStr.toString();
}
private static String getLevelStr(int level) {
StringBuffer levelStr = new StringBuffer();
for (int levelI = 0; levelI < level; levelI++) {
levelStr.append("\t");
}
return levelStr.toString();
}
/**
* 判断是否是Json格式
*
* @param json
* @return
*/
public static boolean isJsonFormat(String json) {
try {
if(TextUtils.isEmpty(json)){
return false;
}
new JsonParser().parse(json);
return true;
} catch (JsonParseException e) {
return false;
}
}
/**
* unicode解码
*
* @param ori
* @return
*/
public static String convertUnicode(String ori) {
char aChar;
int len = ori.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len; ) {
aChar = ori.charAt(x++);
if (aChar == '\\') {
aChar = ori.charAt(x++);
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = ori.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
}