From a480a1d94e83c896d9b8342d9d21f0ef95f8bab1 Mon Sep 17 00:00:00 2001 From: jp1017 Date: Fri, 13 Nov 2015 15:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2Logger=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + 替换Logger类并在mainactivity测试,打印更智能 + 修改.gitignore文件 --- .gitignore | 40 +- .idea/gradle.xml | 5 +- .idea/misc.xml | 52 ++- .idea/vcs.xml | 2 +- MyDaoGenerator/MyDaoGenerator.iml | 205 +++++++++ app/.gitignore | 1 + app/app.iml | 228 +++++++++- .../fastandroid/app/BaseApplication.java | 8 + .../fastandroid/net/AsyncHttpNetCenter.java | 2 +- .../fastandroid/net/OkHttpNetCenter.java | 2 +- .../TransactionAsyncHttpStringHandler.java | 2 +- .../net/TransactionOkhttpStringHandler.java | 3 +- .../ui/activity/CalendarActivity.java | 2 +- .../fastandroid/ui/activity/MainActivity.java | 9 + .../com/hunter/fastandroid/utils/Logger.java | 49 --- .../fastandroid/utils/logger/LogLevel.java | 17 + .../fastandroid/utils/logger/Logger.java | 410 ++++++++++++++++++ fastandroid.iml | 205 +++++++++ 18 files changed, 1166 insertions(+), 76 deletions(-) delete mode 100644 app/src/main/java/com/hunter/fastandroid/utils/Logger.java create mode 100644 app/src/main/java/com/hunter/fastandroid/utils/logger/LogLevel.java create mode 100644 app/src/main/java/com/hunter/fastandroid/utils/logger/Logger.java diff --git a/.gitignore b/.gitignore index 9c4de58..d863bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,35 @@ -.gradle -/local.properties -/.idea/workspace.xml -/.idea/libraries .DS_Store -/build -/captures +.captures + +#built application files +*.apk +*.ap_ + +# files for the dex VM +*.dex + +# Java class files +*.class + +# generated files +bin/ +gen/ + +# Local configuration file (sdk path, etc) +local.properties + +# Windows thumbnail db +Thumbs.db + +# Eclipse project files +.classpath +.project + +# Android Studio +*.iml +.idea +.gradle +build/ + +#NDK +obj/ \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 2f6ba4f..4f08add 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,9 +3,10 @@ + + + + + + + + + Android Lint + + + Java + + + Java language level migration aidsJava + + + Performance issuesJava + + + Portability issuesJava + + + Probable bugsJava + + + + + Android + + + + + @@ -37,10 +71,26 @@ - + + + + + + 1.8 + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 6564d52..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/MyDaoGenerator/MyDaoGenerator.iml b/MyDaoGenerator/MyDaoGenerator.iml index ae21c59..a122f1b 100644 --- a/MyDaoGenerator/MyDaoGenerator.iml +++ b/MyDaoGenerator/MyDaoGenerator.iml @@ -30,4 +30,209 @@ + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore index 796b96d..d0b97c6 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1,2 @@ /build +*.iml \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index a3485dc..324d7ac 100644 --- a/app/app.iml +++ b/app/app.iml @@ -94,21 +94,227 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/hunter/fastandroid/app/BaseApplication.java b/app/src/main/java/com/hunter/fastandroid/app/BaseApplication.java index ab5606f..dac27bf 100644 --- a/app/src/main/java/com/hunter/fastandroid/app/BaseApplication.java +++ b/app/src/main/java/com/hunter/fastandroid/app/BaseApplication.java @@ -5,6 +5,10 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.util.DisplayMetrics; + +import com.hunter.fastandroid.BuildConfig; +import com.hunter.fastandroid.utils.logger.LogLevel; +import com.hunter.fastandroid.utils.logger.Logger; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; @@ -49,6 +53,10 @@ public void onCreate() { initImageLoader(); initScreenSize(); + + if (BuildConfig.DEBUG) { + Logger.init().hideThreadInfo().setLogLevel(LogLevel.FULL); + } } /** diff --git a/app/src/main/java/com/hunter/fastandroid/net/AsyncHttpNetCenter.java b/app/src/main/java/com/hunter/fastandroid/net/AsyncHttpNetCenter.java index e21b734..6ffd6a1 100644 --- a/app/src/main/java/com/hunter/fastandroid/net/AsyncHttpNetCenter.java +++ b/app/src/main/java/com/hunter/fastandroid/net/AsyncHttpNetCenter.java @@ -4,8 +4,8 @@ import com.hunter.fastandroid.app.AppManager; import com.hunter.fastandroid.base.BaseRequest; -import com.hunter.fastandroid.utils.Logger; import com.hunter.fastandroid.utils.NetUtils; +import com.hunter.fastandroid.utils.logger.Logger; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; diff --git a/app/src/main/java/com/hunter/fastandroid/net/OkHttpNetCenter.java b/app/src/main/java/com/hunter/fastandroid/net/OkHttpNetCenter.java index 19c2531..e1aea1b 100644 --- a/app/src/main/java/com/hunter/fastandroid/net/OkHttpNetCenter.java +++ b/app/src/main/java/com/hunter/fastandroid/net/OkHttpNetCenter.java @@ -4,8 +4,8 @@ import com.hunter.fastandroid.app.AppManager; import com.hunter.fastandroid.base.BaseRequest; -import com.hunter.fastandroid.utils.Logger; import com.hunter.fastandroid.utils.NetUtils; +import com.hunter.fastandroid.utils.logger.Logger; import com.squareup.okhttp.Callback; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.Headers; diff --git a/app/src/main/java/com/hunter/fastandroid/net/TransactionAsyncHttpStringHandler.java b/app/src/main/java/com/hunter/fastandroid/net/TransactionAsyncHttpStringHandler.java index bc60071..7124578 100644 --- a/app/src/main/java/com/hunter/fastandroid/net/TransactionAsyncHttpStringHandler.java +++ b/app/src/main/java/com/hunter/fastandroid/net/TransactionAsyncHttpStringHandler.java @@ -1,6 +1,6 @@ package com.hunter.fastandroid.net; -import com.hunter.fastandroid.utils.Logger; +import com.hunter.fastandroid.utils.logger.Logger; import com.loopj.android.http.TextHttpResponseHandler; import org.apache.http.Header; diff --git a/app/src/main/java/com/hunter/fastandroid/net/TransactionOkhttpStringHandler.java b/app/src/main/java/com/hunter/fastandroid/net/TransactionOkhttpStringHandler.java index f065c51..572f8f3 100644 --- a/app/src/main/java/com/hunter/fastandroid/net/TransactionOkhttpStringHandler.java +++ b/app/src/main/java/com/hunter/fastandroid/net/TransactionOkhttpStringHandler.java @@ -3,8 +3,7 @@ import android.os.Handler; import android.os.Message; -import com.hunter.fastandroid.base.JsonResponse; -import com.hunter.fastandroid.utils.Logger; +import com.hunter.fastandroid.utils.logger.Logger; import com.squareup.okhttp.Callback; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; diff --git a/app/src/main/java/com/hunter/fastandroid/ui/activity/CalendarActivity.java b/app/src/main/java/com/hunter/fastandroid/ui/activity/CalendarActivity.java index 310ebb2..a23ffc0 100644 --- a/app/src/main/java/com/hunter/fastandroid/ui/activity/CalendarActivity.java +++ b/app/src/main/java/com/hunter/fastandroid/ui/activity/CalendarActivity.java @@ -16,7 +16,7 @@ import com.hunter.fastandroid.adapter.CalendarAdapter; import com.hunter.fastandroid.base.BaseActivity; import com.hunter.fastandroid.utils.CalendarUtils; -import com.hunter.fastandroid.utils.Logger; +import com.hunter.fastandroid.utils.logger.Logger; import com.hunter.fastandroid.vo.Calendar; import java.util.Date; diff --git a/app/src/main/java/com/hunter/fastandroid/ui/activity/MainActivity.java b/app/src/main/java/com/hunter/fastandroid/ui/activity/MainActivity.java index aade639..9996e2b 100644 --- a/app/src/main/java/com/hunter/fastandroid/ui/activity/MainActivity.java +++ b/app/src/main/java/com/hunter/fastandroid/ui/activity/MainActivity.java @@ -8,6 +8,7 @@ import com.hunter.fastandroid.presenter.interfaces.ILoginPresenter; import com.hunter.fastandroid.ui.custom.TitleBar; import com.hunter.fastandroid.ui.view.interfaces.ILoginView; +import com.hunter.fastandroid.utils.logger.Logger; import com.hunter.fastandroid.vo.request.LoginRequest; import com.hunter.fastandroid.vo.response.UserInfo; @@ -40,6 +41,14 @@ public void initView() { @Override public void initPresenter() { loginPresenter = new LoginPresenterImpl(this); + + //测试logger + Logger.v("sexy logcat"); + Logger.d("sexy logcat"); + Logger.i("sexy logcat"); + Logger.w("sexy logcat"); + Logger.e("sexy logcat"); + Logger.json("sexy logcat"); } @Override diff --git a/app/src/main/java/com/hunter/fastandroid/utils/Logger.java b/app/src/main/java/com/hunter/fastandroid/utils/Logger.java deleted file mode 100644 index bb91f45..0000000 --- a/app/src/main/java/com/hunter/fastandroid/utils/Logger.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.hunter.fastandroid.utils; - -import android.app.Activity; -import android.util.Log; - -import com.hunter.fastandroid.app.AppManager; - - -/** - * Author:Ht - * Create time:2015/8/19 9:03 - * Email:hunter_android@163.com - * Description:日志管理器 - */ - -public class Logger { - - /** - * 是否为开发者模式(开发模式打印LOG,非开发模式不打印LOG) - */ - private static boolean mDebug = true; - - private Logger() { - } - - /** - * 打印info级别的log - * - * @param msg - */ - public static void i(String msg) { - if (mDebug) { - Activity activity = AppManager.getAppManager().currentActivity(); - Log.i("*** CurrentPage:" + activity.getClass().getSimpleName() + " Log", msg); - } - } - - /** - * 打印error级别的log - * - * @param msg - */ - public static void e(String msg) { - if (mDebug) { - Activity activity = AppManager.getAppManager().currentActivity(); - Log.e("*** CurrentPage:" + activity.getClass().getSimpleName() + " Log", msg); - } - } -} diff --git a/app/src/main/java/com/hunter/fastandroid/utils/logger/LogLevel.java b/app/src/main/java/com/hunter/fastandroid/utils/logger/LogLevel.java new file mode 100644 index 0000000..6c39d8c --- /dev/null +++ b/app/src/main/java/com/hunter/fastandroid/utils/logger/LogLevel.java @@ -0,0 +1,17 @@ +package com.hunter.fastandroid.utils.logger; + +/** + * @author Orhan Obut + */ +public enum LogLevel { + + /** + * Prints all logs + */ + FULL, + + /** + * No log will be printed + */ + NONE +} diff --git a/app/src/main/java/com/hunter/fastandroid/utils/logger/Logger.java b/app/src/main/java/com/hunter/fastandroid/utils/logger/Logger.java new file mode 100644 index 0000000..4a11760 --- /dev/null +++ b/app/src/main/java/com/hunter/fastandroid/utils/logger/Logger.java @@ -0,0 +1,410 @@ +package com.hunter.fastandroid.utils.logger; + +import android.text.TextUtils; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Logger is a wrapper of {@link Log} + * But more pretty, simple and powerful + * + * @author Orhan Obut + */ +public final class Logger { + + /** + * Android's max limit for a log entry is ~4076 bytes, + * so 4000 bytes is used as chunk size since default charset + * is UTF-8 + */ + private static final int CHUNK_SIZE = 4000; + + /** + * It is used for json pretty print + */ + private static final int JSON_INDENT = 4; + + /** + * In order to prevent readability, max method count is restricted with 5 + */ + private static final int MAX_METHOD_COUNT = 5; + + /** + * It is used to determine log settings such as method count, thread info visibility + */ + private static final Settings settings = new Settings(); + + /** + * Drawing toolbox + */ + private static final char TOP_LEFT_CORNER = '╔'; + private static final char BOTTOM_LEFT_CORNER = '╚'; + private static final char MIDDLE_CORNER = '╟'; + private static final char HORIZONTAL_DOUBLE_LINE = '║'; + private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════"; + private static final String SINGLE_DIVIDER = "────────────────────────────────────────────"; + private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; + private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER; + private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER; + + /** + * TAG is used for the Log, the name is a little different + * in order to differentiate the logs easily with the filter + */ + private static String TAG = "logger"; + + //no instance + private Logger() { + } + + /** + * It is used to get the settings object in order to change settings + * + * @return the settings object + */ + public static Settings init() { + return settings; + } + + /** + * It is used to change the tag + * + * @param tag is the given string which will be used in Logger + */ + public static Settings init(String tag) { + if (tag == null) { + throw new NullPointerException("tag may not be null"); + } + if (tag.trim().length() == 0) { + throw new IllegalStateException("tag may not be empty"); + } + Logger.TAG = tag; + return settings; + } + + public static void d(String message) { + d(TAG, message); + } + + public static void d(String tag, String message) { + d(tag, message, settings.methodCount); + } + + public static void d(String message, int methodCount) { + d(TAG, message, methodCount); + } + + public static void d(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + log(Log.DEBUG, tag, message, methodCount); + } + + public static void e(String message) { + e(TAG, message); + } + + public static void e(String tag, String message) { + e(tag, message, null, settings.methodCount); + } + + public static void e(Exception e) { + e(TAG, null, e, settings.methodCount); + } + + public static void e(String tag, Exception e) { + e(tag, null, e, settings.methodCount); + } + + public static void e(String message, int methodCount) { + validateMethodCount(methodCount); + e(message, null, methodCount); + } + + public static void e(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + e(tag, message, null, methodCount); + } + + public static void e(String tag, String message, Exception e) { + e(tag, message, e, settings.methodCount); + } + + public static void e(String tag, String message, Exception e, int methodCount) { + validateMethodCount(methodCount); + if (e != null && message != null) { + message += " : " + e.toString(); + } + if (e != null && message == null) { + message = e.toString(); + } + if (message == null) { + message = "No message/exception is set"; + } + log(Log.ERROR, tag, message, methodCount); + } + + public static void w(String message) { + w(TAG, message); + } + + public static void w(String tag, String message) { + w(tag, message, settings.methodCount); + } + + public static void w(String message, int methodCount) { + w(TAG, message, methodCount); + } + + public static void w(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + log(Log.WARN, tag, message, methodCount); + } + + public static void i(String message) { + i(TAG, message); + } + + public static void i(String tag, String message) { + i(tag, message, settings.methodCount); + } + + public static void i(String message, int methodCount) { + i(TAG, message, methodCount); + } + + public static void i(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + log(Log.INFO, tag, message, methodCount); + } + + public static void v(String message) { + v(TAG, message); + } + + public static void v(String tag, String message) { + v(tag, message, settings.methodCount); + } + + public static void v(String message, int methodCount) { + v(TAG, message, methodCount); + } + + public static void v(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + log(Log.VERBOSE, tag, message, methodCount); + } + + public static void wtf(String message) { + wtf(TAG, message); + } + + public static void wtf(String tag, String message) { + wtf(tag, message, settings.methodCount); + } + + public static void wtf(String message, int methodCount) { + wtf(TAG, message, methodCount); + } + + public static void wtf(String tag, String message, int methodCount) { + validateMethodCount(methodCount); + log(Log.ASSERT, tag, message, methodCount); + } + + /** + * Formats the json content and print it + * + * @param json the json content + */ + public static void json(String json) { + json(TAG, json); + } + + public static void json(String tag, String json) { + json(tag, json, settings.methodCount); + } + + public static void json(String json, int methodCount) { + json(TAG, json, methodCount); + } + + /** + * Formats the json content and print it + * + * @param json the json content + * @param methodCount number of the method that will be printed + */ + public static void json(String tag, String json, int methodCount) { + validateMethodCount(methodCount); + if (TextUtils.isEmpty(json)) { + d(tag, "Empty/Null json content", methodCount); + return; + } + try { + if (json.startsWith("{")) { + JSONObject jsonObject = new JSONObject(json); + String message = jsonObject.toString(JSON_INDENT); + d(tag, message, methodCount); + return; + } + if (json.startsWith("[")) { + JSONArray jsonArray = new JSONArray(json); + String message = jsonArray.toString(JSON_INDENT); + d(tag, message, methodCount); + } + } catch (JSONException e) { + d(tag, e.getCause().getMessage() + "\n" + json, methodCount); + } + } + + /** + * This method is synchronized in order to avoid messy of logs' order. + */ + private synchronized static void log(int logType, String tag, String message, int methodCount) { + if (settings.logLevel == LogLevel.NONE) { + return; + } + logTopBorder(logType, tag); + logHeaderContent(logType, tag, methodCount); + + //get bytes of message with system's default charset (which is UTF-8 for Android) + byte[] bytes = message.getBytes(); + int length = bytes.length; + if (length <= CHUNK_SIZE) { + if (methodCount > 0) { + logDivider(logType, tag); + } + logContent(logType, tag, message); + logBottomBorder(logType, tag); + return; + } + if (methodCount > 0) { + logDivider(logType, tag); + } + for (int i = 0; i < length; i += CHUNK_SIZE) { + int count = Math.min(length - i, CHUNK_SIZE); + //create a new String with system's default charset (which is UTF-8 for Android) + logContent(logType, tag, new String(bytes, i, count)); + } + logBottomBorder(logType, tag); + } + + private static void logTopBorder(int logType, String tag) { + logChunk(logType, tag, TOP_BORDER); + } + + private static void logHeaderContent(int logType, String tag, int methodCount) { + StackTraceElement[] trace = Thread.currentThread().getStackTrace(); + + if (settings.showThreadInfo) { + logChunk(logType, tag, HORIZONTAL_DOUBLE_LINE + " Thread: " + Thread.currentThread().getName()); + logDivider(logType, tag); + } + String level = ""; + for (int i = methodCount; i > 0; i--) { + int stackIndex = i + 6;//之前是5 + StringBuilder builder = new StringBuilder(); + builder.append("║ ") + .append(level) + .append(getSimpleClassName(trace[stackIndex].getClassName())) + .append(".") + .append(trace[stackIndex].getMethodName()) + .append(" ") + .append(" (") + .append(trace[stackIndex].getFileName()) + .append(":") + .append(trace[stackIndex].getLineNumber()) + .append(")"); + level += " "; + logChunk(logType, tag, builder.toString()); + } + } + + private static void logBottomBorder(int logType, String tag) { + logChunk(logType, tag, BOTTOM_BORDER); + } + + private static void logDivider(int logType, String tag) { + logChunk(logType, tag, MIDDLE_BORDER); + } + + private static void logContent(int logType, String tag, String chunk) { + String[] lines = chunk.split(System.getProperty("line.separator")); + for (String line : lines) { + logChunk(logType, tag, HORIZONTAL_DOUBLE_LINE + " " + line); + } + } + + private static void logChunk(int logType, String tag, String chunk) { + String finalTag = formatTag(tag); + switch (logType) { + case Log.ERROR: + Log.e(finalTag, chunk); + break; + case Log.INFO: + Log.i(finalTag, chunk); + break; + case Log.VERBOSE: + Log.v(finalTag, chunk); + break; + case Log.WARN: + Log.w(finalTag, chunk); + break; + case Log.ASSERT: + Log.wtf(finalTag, chunk); + break; + case Log.DEBUG: + // Fall through, log debug by default + default: + Log.d(finalTag, chunk); + break; + } + } + + private static String getSimpleClassName(String name) { + int lastIndex = name.lastIndexOf("."); + return name.substring(lastIndex + 1); + } + + private static void validateMethodCount(int methodCount) { + if (methodCount < 0 || methodCount > MAX_METHOD_COUNT) { + throw new IllegalStateException("methodCount must be > 0 and < 5"); + } + } + + private static String formatTag(String tag) { + if (!TextUtils.isEmpty(tag) && !TextUtils.equals(TAG, tag)) { + return TAG + "-" + tag; + } + return TAG; + } + + public static class Settings { + int methodCount = 1;//之前是2 + boolean showThreadInfo = true; + + /** + * Determines how logs will printed + */ + LogLevel logLevel = LogLevel.FULL; + + public Settings hideThreadInfo() { + showThreadInfo = false; + return this; + } + + public Settings setMethodCount(int methodCount) { + validateMethodCount(methodCount); + this.methodCount = methodCount; + return this; + } + + public Settings setLogLevel(LogLevel logLevel) { + this.logLevel = logLevel; + return this; + } + } +} diff --git a/fastandroid.iml b/fastandroid.iml index c7abe6b..849d7db 100644 --- a/fastandroid.iml +++ b/fastandroid.iml @@ -16,4 +16,209 @@ + + + + + \ No newline at end of file