From c8be3132741ed21022356be5312f544c3585a1cd Mon Sep 17 00:00:00 2001 From: Blankj <625783482@qq.com> Date: Fri, 11 Aug 2017 17:46:29 +0800 Subject: [PATCH] see 08/11 log --- README-CN.md | 41 +++-- README.md | 23 +-- app/build.gradle | 8 +- .../com/blankj/androidutilcode/UtilsApp.java | 22 ++- .../androidutilcode/core/log/LogActivity.java | 8 +- subutil/build.gradle | 4 +- update_log.md | 1 + .../com/blankj/utilcode/util/LogUtils.java | 171 +++++++++--------- 8 files changed, 146 insertions(+), 132 deletions(-) diff --git a/README-CN.md b/README-CN.md index dfe6a949..3c71019c 100644 --- a/README-CN.md +++ b/README-CN.md @@ -314,24 +314,25 @@ clickBlankArea2HideSoftInput: 点击屏幕空白区域隐藏软键盘 * ### 日志相关→[LogUtils.java][log.java]→[Demo][log.demo] ``` -Builder.setLogSwitch : 设置log总开关 -Builder.setConsoleSwitch : 设置log控制台开关 -Builder.setGlobalTag : 设置log全局tag -Builder.setLogHeadSwitch : 设置log头部信息开关 -Builder.setLog2FileSwitch: 设置log文件开关 -Builder.setDir : 设置log文件存储目录 -Builder.setBorderSwitch : 设置log边框开关 -Builder.setConsoleFilter : 设置log控制台过滤器 -Builder.setFileFilter : 设置log文件过滤器 -v : Verbose日志 -d : Debug日志 -i : Info日志 -w : Warn日志 -e : Error日志 -a : Assert日志 -file : log到文件 -json : log字符串之json -xml : log字符串之xml +getConfig : 获取log配置 +Config.setLogSwitch : 设置log总开关 +Config.setConsoleSwitch : 设置log控制台开关 +Config.setGlobalTag : 设置log全局tag +Config.setLogHeadSwitch : 设置log头部信息开关 +Config.setLog2FileSwitch: 设置log文件开关 +Config.setDir : 设置log文件存储目录 +Config.setBorderSwitch : 设置log边框开关 +Config.setConsoleFilter : 设置log控制台过滤器 +Config.setFileFilter : 设置log文件过滤器 +v : Verbose日志 +d : Debug日志 +i : Info日志 +w : Warn日志 +e : Error日志 +a : Assert日志 +file : log到文件 +json : log字符串之json +xml : log字符串之xml ``` * ### 网络相关→[NetworkUtils.java][network.java]→[Demo][network.demo] @@ -615,7 +616,7 @@ getEntries : 获取压缩文件中的文件对象 Gradle: ``` groovy -compile 'com.blankj:utilcode:1.8.1' +compile 'com.blankj:utilcode:1.8.2' ``` @@ -638,7 +639,7 @@ Utils.init(context); [logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png -[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.1-brightgreen.svg +[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.2-brightgreen.svg [auc]: https://github.com/Blankj/AndroidUtilCode [apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg diff --git a/README.md b/README.md index 68c3ba5c..c396c9bf 100644 --- a/README.md +++ b/README.md @@ -314,15 +314,16 @@ clickBlankArea2HideSoftInput * ### About Log→[LogUtils.java][log.java]→[Demo][log.demo] ``` -Builder.setLogSwitch -Builder.setConsoleSwitch -Builder.setGlobalTag -Builder.setLogHeadSwitch -Builder.setLog2FileSwitch -Builder.setDir -Builder.setBorderSwitch -Builder.setConsoleFilter -Builder.setFileFilter +getConfig +Config.setLogSwitch +Config.setConsoleSwitch +Config.setGlobalTag +Config.setLogHeadSwitch +Config.setLog2FileSwitch +Config.setDir +Config.setBorderSwitch +Config.setConsoleFilter +Config.setFileFilter v d i @@ -615,7 +616,7 @@ getEntries Gradle: ``` groovy -compile 'com.blankj:utilcode:1.8.1' +compile 'com.blankj:utilcode:1.8.2' ``` @@ -638,7 +639,7 @@ Utils.init(context); [logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png -[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.1-brightgreen.svg +[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.2-brightgreen.svg [auc]: https://github.com/Blankj/AndroidUtilCode [apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg diff --git a/app/build.gradle b/app/build.gradle index 7798e6e3..806e9e0f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.blankj.androidutilcode" minSdkVersion 14 targetSdkVersion 25 - versionCode 100800100 - versionName "1.8.1" + versionCode 100800200 + versionName "1.8.2" } if (signPropertiesFile.exists()) { @@ -64,7 +64,7 @@ dependencies { def leakCanary = "com.squareup.leakcanary:leakcanary-android-no-op:$LEAKCANARY_VERSION" - compile project(':utilcode') +// compile project(':utilcode') compile project(':subutil') compile "com.android.support:appcompat-v7:$SUPPORT_VERSION" compile "com.android.support:support-v4:$SUPPORT_VERSION" @@ -76,6 +76,8 @@ dependencies { debugCompile "com.squareup.leakcanary:leakcanary-android:$LEAKCANARY_VERSION" releaseCompile leakCanary testCompile leakCanary + + compile 'com.blankj:utilcode:1.8.2' } diff --git a/app/src/main/java/com/blankj/androidutilcode/UtilsApp.java b/app/src/main/java/com/blankj/androidutilcode/UtilsApp.java index 26da9a06..a0e6e608 100644 --- a/app/src/main/java/com/blankj/androidutilcode/UtilsApp.java +++ b/app/src/main/java/com/blankj/androidutilcode/UtilsApp.java @@ -22,6 +22,15 @@ public class UtilsApp extends BaseApplication { @Override public void onCreate() { super.onCreate(); + com.blankj.utilcode.util.Utils.init(this); + com.blankj.subutil.util.Utils.init(this); + initLeakCanary(); + initLog(); + initCrash(); + initAssets(); + } + + private void initLeakCanary() { // 内存泄露检查工具 if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. @@ -29,15 +38,10 @@ public class UtilsApp extends BaseApplication { return; } LeakCanary.install(this); - com.blankj.utilcode.util.Utils.init(this); - com.blankj.subutil.util.Utils.init(this); - initLog(); - initCrash(); - initAssets(); } public static void initLog() { - LogUtils.Builder builder = new LogUtils.Builder() + LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(BuildConfig.DEBUG)// 设置log总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(BuildConfig.DEBUG)// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置log全局标签,默认为空 @@ -49,7 +53,7 @@ public class UtilsApp extends BaseApplication { .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setConsoleFilter(LogUtils.V)// log的控制台过滤器,和logcat过滤器同理,默认Verbose .setFileFilter(LogUtils.V);// log文件过滤器,和logcat过滤器同理,默认Verbose - LogUtils.d(builder.toString()); + LogUtils.d(config.toString()); } private void initCrash() { @@ -57,9 +61,9 @@ public class UtilsApp extends BaseApplication { } private void initAssets() { - if (!FileUtils.isFileExists(Config.getTestApkPath())) { + if (!FileUtils.isFileExists(com.blankj.androidutilcode.Config.getTestApkPath())) { try { - FileIOUtils.writeFileFromIS(Config.getTestApkPath(), getAssets().open("test_install"), false); + FileIOUtils.writeFileFromIS(com.blankj.androidutilcode.Config.getTestApkPath(), getAssets().open("test_install"), false); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/blankj/androidutilcode/core/log/LogActivity.java b/app/src/main/java/com/blankj/androidutilcode/core/log/LogActivity.java index 9ced1ef3..1f78aff8 100644 --- a/app/src/main/java/com/blankj/androidutilcode/core/log/LogActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/core/log/LogActivity.java @@ -28,7 +28,7 @@ public class LogActivity extends BaseBackActivity { private TextView tvAboutLog; - private LogUtils.Builder mBuilder = new LogUtils.Builder(); + private LogUtils.Config mConfig = LogUtils.getConfig(); private String dir = ""; private String globalTag = ""; @@ -244,7 +244,7 @@ public class LogActivity extends BaseBackActivity { fileFilter = fileFilter == LogUtils.V ? LogUtils.I : LogUtils.V; break; } - mBuilder.setLogSwitch(log) + mConfig.setLogSwitch(log) .setConsoleSwitch(console) .setGlobalTag(globalTag) .setLogHeadSwitch(head) @@ -253,11 +253,11 @@ public class LogActivity extends BaseBackActivity { .setBorderSwitch(border) .setConsoleFilter(consoleFilter) .setFileFilter(fileFilter); - tvAboutLog.setText(mBuilder.toString()); + tvAboutLog.setText(mConfig.toString()); } private String getDir() { - return mBuilder.toString().split(System.getProperty("line.separator"))[5].substring(5); + return mConfig.toString().split(System.getProperty("line.separator"))[5].substring(5); } @Override diff --git a/subutil/build.gradle b/subutil/build.gradle index 3857f879..62efc947 100644 --- a/subutil/build.gradle +++ b/subutil/build.gradle @@ -26,8 +26,8 @@ android { defaultConfig { minSdkVersion 14 - versionCode 100800100 - versionName "1.8.1" + versionCode 100800200 + versionName "1.8.2" } buildTypes { diff --git a/update_log.md b/update_log.md index f8d99833..808979a2 100644 --- a/update_log.md +++ b/update_log.md @@ -1,3 +1,4 @@ +* 17/08/11 LogUtils的Builder改为Config,发布版本1.8.2 * 17/08/10 优化FileUtils的deleteFilesInDir和listFilesInDir * 17/08/08 新增反射工具类ReflectUtils * 17/08/06 更改为按功能分包,增加subutil的Demo diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java index 0b7ab6c7..defcefea 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java @@ -76,6 +76,7 @@ public final class LogUtils { private static int sConsoleFilter = V; // log控制台过滤器 private static int sFileFilter = V; // log文件过滤器 + private static final Config CONFIG = new Config(); private static final String FILE_SEP = System.getProperty("file.separator"); private static final String LINE_SEP = System.getProperty("line.separator"); private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════"; @@ -92,89 +93,8 @@ public final class LogUtils { throw new UnsupportedOperationException("u can't instantiate me..."); } - public static class Builder { - public Builder() { - if (defaultDir != null) return; - if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) - && Utils.getContext().getExternalCacheDir() != null) - defaultDir = Utils.getContext().getExternalCacheDir() + FILE_SEP + "log" + FILE_SEP; - else { - defaultDir = Utils.getContext().getCacheDir() + FILE_SEP + "log" + FILE_SEP; - } - } - - public Builder setLogSwitch(final boolean logSwitch) { - LogUtils.sLogSwitch = logSwitch; - return this; - } - - public Builder setConsoleSwitch(final boolean consoleSwitch) { - LogUtils.sLog2ConsoleSwitch = consoleSwitch; - return this; - } - - public Builder setGlobalTag(final String tag) { - if (isSpace(tag)) { - LogUtils.sGlobalTag = ""; - sTagIsSpace = true; - } else { - LogUtils.sGlobalTag = tag; - sTagIsSpace = false; - } - return this; - } - - public Builder setLogHeadSwitch(final boolean logHeadSwitch) { - LogUtils.sLogHeadSwitch = logHeadSwitch; - return this; - } - - public Builder setLog2FileSwitch(final boolean log2FileSwitch) { - LogUtils.sLog2FileSwitch = log2FileSwitch; - return this; - } - - public Builder setDir(final String dir) { - if (isSpace(dir)) { - LogUtils.dir = null; - } else { - LogUtils.dir = dir.endsWith(FILE_SEP) ? dir : dir + FILE_SEP; - } - return this; - } - - public Builder setDir(final File dir) { - LogUtils.dir = dir == null ? null : dir.getAbsolutePath() + FILE_SEP; - return this; - } - - public Builder setBorderSwitch(final boolean borderSwitch) { - LogUtils.sLogBorderSwitch = borderSwitch; - return this; - } - - public Builder setConsoleFilter(@TYPE final int consoleFilter) { - LogUtils.sConsoleFilter = consoleFilter; - return this; - } - - public Builder setFileFilter(@TYPE final int fileFilter) { - LogUtils.sFileFilter = fileFilter; - return this; - } - - @Override - public String toString() { - return "switch: " + sLogSwitch - + LINE_SEP + "console: " + sLog2ConsoleSwitch - + LINE_SEP + "tag: " + (sTagIsSpace ? "null" : sGlobalTag) - + LINE_SEP + "head: " + sLogHeadSwitch - + LINE_SEP + "file: " + sLog2FileSwitch - + LINE_SEP + "dir: " + (dir == null ? defaultDir : dir) - + LINE_SEP + "border: " + sLogBorderSwitch - + LINE_SEP + "consoleFilter: " + T[sConsoleFilter - V] - + LINE_SEP + "fileFilter: " + T[sFileFilter - V]; - } + public static Config getConfig() { + return CONFIG; } public static void v(final Object contents) { @@ -521,4 +441,89 @@ public final class LogUtils { return bos.toByteArray(); } + + public static class Config { + private Config() { + if (defaultDir != null) return; + if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) + && Utils.getContext().getExternalCacheDir() != null) + defaultDir = Utils.getContext().getExternalCacheDir() + FILE_SEP + "log" + FILE_SEP; + else { + defaultDir = Utils.getContext().getCacheDir() + FILE_SEP + "log" + FILE_SEP; + } + } + + public Config setLogSwitch(final boolean logSwitch) { + LogUtils.sLogSwitch = logSwitch; + return this; + } + + public Config setConsoleSwitch(final boolean consoleSwitch) { + LogUtils.sLog2ConsoleSwitch = consoleSwitch; + return this; + } + + public Config setGlobalTag(final String tag) { + if (isSpace(tag)) { + LogUtils.sGlobalTag = ""; + sTagIsSpace = true; + } else { + LogUtils.sGlobalTag = tag; + sTagIsSpace = false; + } + return this; + } + + public Config setLogHeadSwitch(final boolean logHeadSwitch) { + LogUtils.sLogHeadSwitch = logHeadSwitch; + return this; + } + + public Config setLog2FileSwitch(final boolean log2FileSwitch) { + LogUtils.sLog2FileSwitch = log2FileSwitch; + return this; + } + + public Config setDir(final String dir) { + if (isSpace(dir)) { + LogUtils.dir = null; + } else { + LogUtils.dir = dir.endsWith(FILE_SEP) ? dir : dir + FILE_SEP; + } + return this; + } + + public Config setDir(final File dir) { + LogUtils.dir = dir == null ? null : dir.getAbsolutePath() + FILE_SEP; + return this; + } + + public Config setBorderSwitch(final boolean borderSwitch) { + LogUtils.sLogBorderSwitch = borderSwitch; + return this; + } + + public Config setConsoleFilter(@TYPE final int consoleFilter) { + LogUtils.sConsoleFilter = consoleFilter; + return this; + } + + public Config setFileFilter(@TYPE final int fileFilter) { + LogUtils.sFileFilter = fileFilter; + return this; + } + + @Override + public String toString() { + return "switch: " + sLogSwitch + + LINE_SEP + "console: " + sLog2ConsoleSwitch + + LINE_SEP + "tag: " + (sTagIsSpace ? "null" : sGlobalTag) + + LINE_SEP + "head: " + sLogHeadSwitch + + LINE_SEP + "file: " + sLog2FileSwitch + + LINE_SEP + "dir: " + (dir == null ? defaultDir : dir) + + LINE_SEP + "border: " + sLogBorderSwitch + + LINE_SEP + "consoleFilter: " + T[sConsoleFilter - V] + + LINE_SEP + "fileFilter: " + T[sFileFilter - V]; + } + } } -- GitLab