美文网首页androidAndroid应用开发连载Android开发
羊皮书APP (Android版)开发系列(二)日志工具类

羊皮书APP (Android版)开发系列(二)日志工具类

作者: JeenWang | 来源:发表于2016-03-15 12:54 被阅读786次

    在App开发过程中,很重要的一个调试工具就是日志的打印,Android系统自带的日志打印文件,看起来并不是很直观。这里我们自己对原生Android 日志做一个封装,方便我们使用。

    • 为了更方便,更简洁,将日志文件工具命名为L.java,代码如下:
    package cn.studyou.parchment.log;
    
    import android.text.TextUtils;
    import android.util.Log;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.io.File;
    /**
     * 基本功能:记录日志
     * 创建:王杰
     * 创建时间:16/3/7
     * 邮箱:w489657152@gmail.com
     */
    public class L {
    
        private static boolean IS_SHOW_LOG = true;
    
        private static final String DEFAULT_MESSAGE = "execute";
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
        private static final int JSON_INDENT = 4;
    
        private static final int V = 0x1;
        private static final int D = 0x2;
        private static final int I = 0x3;
        private static final int W = 0x4;
        private static final int E = 0x5;
        private static final int A = 0x6;
        private static final int JSON = 0x7;
        private static final int FILE = 0x8;
    
        public static void init(boolean isShowLog) {
            IS_SHOW_LOG = isShowLog;
        }
    
        public static void v() {
            printLog(V, null, DEFAULT_MESSAGE);
        }
    
        public static void v(Object msg) {
            printLog(V, null, msg);
        }
    
        public static void v(String tag, String msg) {
            printLog(V, tag, msg);
        }
    
        public static void d() {
            printLog(D, null, DEFAULT_MESSAGE);
        }
    
        public static void d(Object msg) {
            printLog(D, null, msg);
        }
    
        public static void d(String tag, Object msg) {
            printLog(D, tag, msg);
        }
    
        public static void i() {
            printLog(I, null, DEFAULT_MESSAGE);
        }
    
        public static void i(Object msg) {
            printLog(I, null, msg);
        }
    
        public static void i(String tag, Object msg) {
            printLog(I, tag, msg);
        }
    
        public static void w() {
            printLog(W, null, DEFAULT_MESSAGE);
        }
    
        public static void w(Object msg) {
            printLog(W, null, msg);
        }
    
        public static void w(String tag, Object msg) {
            printLog(W, tag, msg);
        }
    
        public static void e() {
            printLog(E, null, DEFAULT_MESSAGE);
        }
    
        public static void e(Object msg) {
            printLog(E, null, msg);
        }
    
        public static void e(String tag, Object msg) {
            printLog(E, tag, msg);
        }
    
        public static void a() {
            printLog(A, null, DEFAULT_MESSAGE);
        }
    
        public static void a(Object msg) {
            printLog(A, null, msg);
        }
    
        public static void a(String tag, Object msg) {
            printLog(A, tag, msg);
        }
    
        public static void json(String jsonFormat) {
            printLog(JSON, null, jsonFormat);
        }
    
        public static void json(String tag, String jsonFormat) {
            printLog(JSON, tag, jsonFormat);
        }
    
        public static void file(File targetDirectory, Object msg) {
            printFile(null, targetDirectory, null, msg);
        }
    
        public static void file(String tag, File targetDirectory, Object msg) {
            printFile(tag, targetDirectory, null, msg);
        }
    
        public static void file(String tag, File targetDirectory, String fileName, Object msg) {
            printFile(tag, targetDirectory, fileName, msg);
        }
    
        private static void printLog(int type, String tagStr, Object objectMsg) {
    
            if (!IS_SHOW_LOG) {
                return;
            }
    
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    
            int index = 4;
            String className = stackTrace[index].getFileName();
            String methodName = stackTrace[index].getMethodName();
            int lineNumber = stackTrace[index].getLineNumber();
    
            String tag = (tagStr == null ? className : tagStr);
    
            String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodNameShort).append(" ] ");
            String msg = (objectMsg == null) ? "Log with null Object" : objectMsg.toString();
    
            if (msg != null && type != JSON) {
                stringBuilder.append(msg);
            }
    
            String logStr = stringBuilder.toString();
    
            switch (type) {
                case V:
                case D:
                case I:
                case W:
                case E:
                case A:
                    printLog(type, tag, logStr);
                    break;
                case JSON: {
                    if (TextUtils.isEmpty(msg)) {
                        Log.e(tag, "Empty or Null json content");
                        return;
                    }
                    printJson(tag, msg, logStr);
                }
                break;
            }
    
        }
    
    
        private static void printLog(int type, String tag, String logStr) {
            switch (type) {
                case V:
                    Log.v(tag, logStr);
                    break;
                case D:
                    Log.d(tag, logStr);
                    break;
                case I:
                    Log.i(tag, logStr);
                    break;
                case W:
                    Log.w(tag, logStr);
                    break;
                case E:
                    Log.e(tag, logStr);
                    break;
                case A:
                    Log.wtf(tag, logStr);
                    break;
            }
        }
    
        private static void printJson(String tag, String msg, String logStr) {
    
            String message = null;
    
            try {
                if (msg.startsWith("{")) {
                    JSONObject jsonObject = new JSONObject(msg);
                    message = jsonObject.toString(JSON_INDENT);
                } else if (msg.startsWith("[")) {
                    JSONArray jsonArray = new JSONArray(msg);
                    message = jsonArray.toString(JSON_INDENT);
                }
            } catch (JSONException e) {
                e(tag, e.getCause().getMessage() + "\n" + msg);
                return;
            }
    
            printLine(tag, true);
            message = logStr + LINE_SEPARATOR + message;
            String[] lines = message.split(LINE_SEPARATOR);
            StringBuilder jsonContent = new StringBuilder();
            for (String line : lines) {
                jsonContent.append("║ ").append(line).append(LINE_SEPARATOR);
            }
            Log.d(tag, jsonContent.toString());
            printLine(tag, false);
        }
    
        private static void printFile(String tag, File targetDirectory, String fileName, Object objectMsg) {
    
            if (!IS_SHOW_LOG) {
                return;
            }
    
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    
            int index = 4;
            String className = stackTrace[index].getFileName();
            String methodName = stackTrace[index].getMethodName();
            int lineNumber = stackTrace[index].getLineNumber();
    
            tag = (tag == null ? className : tag);
    
            String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodNameShort).append(" ] ");
            String msg = (objectMsg == null) ? "Log with null Object" : objectMsg.toString();
    
            String headString = stringBuilder.toString();
    
            if (msg != null) {
                msg = headString + msg;
            }
    
            fileName = (fileName == null) ? FileHelper.getFileName() : fileName;
            if (FileHelper.save(targetDirectory, fileName, msg)) {
                Log.d(tag, headString + " save log success ! location is >>>" + targetDirectory.getAbsolutePath() + "/" + fileName);
            } else {
                Log.e(tag, headString + "save log fails !");
            }
        }
    
        private static void printLine(String tag, boolean isTop) {
            if (isTop) {
                Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
            } else {
                Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
            }
        }
    
    }
    
    • FileHelper.java 代码如下:
    package cn.studyou.parchment.log;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Random;
    
    /**
     * 基本功能:FileHelper
     * 创建:王杰
     * 创建时间:16/3/7
     * 邮箱:w489657152@gmail.com
     */
    public class FileHelper {
    
        public static boolean save(File dic, String fileName, String msg) {
    
            File file = new File(dic, fileName);
    
            try {
                OutputStream outputStream = new FileOutputStream(file);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
                outputStreamWriter.write(msg);
                outputStreamWriter.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
    
            return true;
        }
    
        public static String getFileName() {
            Random random = new Random();
            StringBuilder stringBuilder = new StringBuilder("KLog_");
            stringBuilder.append(Long.toString(System.currentTimeMillis()+random.nextInt(10000)).substring(4));
            stringBuilder.append(".txt");
            return stringBuilder.toString();
        }
    
    }
    

    这个日志工具只有以上两个文件,那么我们如何使用这个日志工具呢?很简答。

    • 首先在APP的Application文件中设置debug开关:
    public static final boolean DEVELOPER_MODE = true;
    
    • 其次在OnCreate方法中进行初始化:
     @Override
        public void onCreate() {
            super.onCreate();
            //初始化日志工具
            L.init(DEVELOPER_MODE);
        }
    
    • 到这里初始化就完成了,在我们的java文件中使用这个日志工具L:
    L.e("First start!");
    L.i("First start!");
    L.a("First start!");
    
    • 控制台显示结果:
    03-07 13:37:59.470 14633-14633/cn.studyou.parchment E/StartUpActivity.java: [ (StartUpActivity.java:24)#OnCreate ] First start!
    03-07 13:37:59.470 14633-14633/cn.studyou.parchment I/StartUpActivity.java: [ (StartUpActivity.java:26)#OnCreate ] First start!
    03-07 13:37:59.470 14633-14633/cn.studyou.parchment A/StartUpActivity.java: [ (StartUpActivity.java:27)#OnCreate ] First start!
    
    • 点击控制台超链接StartUpActivity.java:24即可直接定位到日志的具体位置。是不是很简单呢?赶紧试试吧!

    相关文章

      网友评论

      • 饕餮潴:还是没看懂是什么意思 :joy:
        JeenWang:@饕餮潴 这只是一个工具,作用是对Android 的Log进行了封装,目的是为了代码简洁和调试方便。举个例子:之前你在打印日志的时候,使用的是Log.e("MainActivity","onCreate()"); ,现在你就可以直接使用 L.e("onCreate()"); ,同时呢,使用L.e时,在logcat中显示的日志时候,会出现超链接,你点击超链接,直接就能跳转到代码L.e("onCreate()")所在的位置,很方便。你可以放在项目里面试试看。
        饕餮潴:@JeenWang android studio工具? 这个具体作用是这个报错和locat有什么区别能说说么 我小白
        JeenWang:@饕餮潴 什么地方不懂呢?

      本文标题:羊皮书APP (Android版)开发系列(二)日志工具类

      本文链接:https://www.haomeiwen.com/subject/jkfqlttx.html