Android日志简单封装

日志封装类

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值