From 33d7c35f676ece09343caf94e15a4e150058d24e Mon Sep 17 00:00:00 2001 From: yimiao Date: Wed, 28 Jul 2021 14:34:34 +0800 Subject: [PATCH] =?UTF-8?q?author:wangyimiao=20desc:=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=89=93=E6=97=A5=E5=BF=97=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/commonlibtest/MainActivity.java | 22 ++--- .../common/commonlib/log/LogSecureHelper.kt | 20 ++++ .../java/com/common/commonlib/log/Logger.kt | 97 +++++++++++++++++++ 3 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 commonLib/src/main/java/com/common/commonlib/log/LogSecureHelper.kt create mode 100644 commonLib/src/main/java/com/common/commonlib/log/Logger.kt diff --git a/app/src/main/java/com/common/commonlibtest/MainActivity.java b/app/src/main/java/com/common/commonlibtest/MainActivity.java index ec323b0..476e4ad 100644 --- a/app/src/main/java/com/common/commonlibtest/MainActivity.java +++ b/app/src/main/java/com/common/commonlibtest/MainActivity.java @@ -3,11 +3,11 @@ package com.common.commonlibtest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; -import android.util.Log; import com.common.commonlib.image.config.GlideHolderConfig; import com.common.commonlib.image.config.GlideTransitionConfig; import com.common.commonlib.image.loader.ImageLoader; +import com.common.commonlib.log.Logger; import com.common.commonlib.net.RequestCallBack; import com.common.commonlib.net.interceptor.RequestHeadInterceptor; import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; @@ -56,17 +56,17 @@ public class MainActivity extends AppCompatActivity { loader.getHomeArticles(0, new RequestCallBack() { @Override public void onResult(ArticlesResponse result) { - Log.d(TAG, result.getData().toString()); + Logger.INSTANCE.d(TAG, result.getData().toString()); } @Override public void onError(@Nullable String error) { - Log.d(TAG, "onError"); + Logger.INSTANCE.d(TAG, "onError"); } @Override public void onComplete() { - Log.d(TAG, "onComplete"); + Logger.INSTANCE.d(TAG, "onComplete"); } }); }); @@ -82,17 +82,17 @@ public class MainActivity extends AppCompatActivity { loader.getCollections(0, new RequestCallBack() { @Override public void onResult(CollectionResponse result) { - Log.d(TAG, "onResult"); + Logger.INSTANCE.d(TAG, "onResult"); } @Override public void onError(@Nullable String error) { - Log.d(TAG, "onError"); + Logger.INSTANCE.d(TAG, "onError"); } @Override public void onComplete() { - Log.d(TAG, "onComplete"); + Logger.INSTANCE.d(TAG, "onComplete"); } }); }); @@ -106,17 +106,17 @@ public class MainActivity extends AppCompatActivity { new LoginLoader(interceptors).login("wang11", "wang456852", new RequestCallBack() { @Override public void onResult(LoginResponse result) { - Log.d(TAG, "onResult"); + Logger.INSTANCE.d(TAG, "onResult"); } @Override public void onError(@Nullable String error) { - Log.d(TAG, "onError"); + Logger.INSTANCE.e(TAG, "onError"); } @Override public void onComplete() { - Log.d(TAG, "onComplete"); + Logger.INSTANCE.i(TAG, "onComplete"); } }); }); @@ -128,7 +128,7 @@ public class MainActivity extends AppCompatActivity { @Override public void run() { FTPUtils.RESULT result = FTPUtils.INSTANCE.ftpUpload("192.168.101.215", "2221", "admin", "123456", filePath, fileName); - Log.i("wangym", result.name()); + Logger.INSTANCE.d(result.name()); } }); thread.start(); diff --git a/commonLib/src/main/java/com/common/commonlib/log/LogSecureHelper.kt b/commonLib/src/main/java/com/common/commonlib/log/LogSecureHelper.kt new file mode 100644 index 0000000..6556607 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/log/LogSecureHelper.kt @@ -0,0 +1,20 @@ +package com.common.commonlib.log + +/** + * 日志敏感信息处理 + */ +object LogSecureHelper { + + /** + * 一个间隔一个将数据替换成* + */ + fun cast2Star(source: String): String { + val sourceTemp = source.toCharArray() + for (item in sourceTemp.withIndex()) { + if (item.index / 2 == 0) { + sourceTemp[item.index] = '*' + } + } + return sourceTemp.toString() + } +} \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/log/Logger.kt b/commonLib/src/main/java/com/common/commonlib/log/Logger.kt new file mode 100644 index 0000000..6d292d6 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/log/Logger.kt @@ -0,0 +1,97 @@ +package com.common.commonlib.log + +import android.util.Log + +object Logger { + var commonTag = "" + var needSecurity = false + + fun init(tag: String, security: Boolean) { + commonTag = tag + needSecurity = security + } + + fun w(msg: Any?) { + w(commonTag, msg.toString(), needSecurity) + } + + fun d(msg: Any?) { + d(commonTag, msg.toString(), needSecurity) + } + + fun i(msg: Any?) { + i(commonTag, msg.toString(), needSecurity) + } + + fun e(msg: Any?) { + e(commonTag, msg.toString(), needSecurity) + } + + fun w(msg: Any?, needSecurity: Boolean) { + w(commonTag, msg.toString(), needSecurity) + } + + fun d(msg: Any?, needSecurity: Boolean) { + d(commonTag, msg.toString(), needSecurity) + } + + fun i(msg: Any?, needSecurity: Boolean) { + i(commonTag, msg.toString(), needSecurity) + } + + fun e(msg: Any?, needSecurity: Boolean) { + e(commonTag, msg.toString(), needSecurity) + } + + fun w(tag: String, msg: Any?) { + w(tag, msg.toString(), needSecurity) + } + + fun d(tag: String, msg: Any?) { + d(tag, msg.toString(), needSecurity) + } + + fun i(tag: String, msg: Any?) { + i(tag, msg.toString(), needSecurity) + } + + fun e(tag: String, msg: Any?) { + e(tag, msg.toString(), needSecurity) + } + + fun w(tag: String, msg: Any?, needSecurity: Boolean) { + Log.w( + tag, if (needSecurity) + LogSecureHelper.cast2Star(msg.toString()) + else + msg.toString() + ) + } + + fun d(tag: String, msg: Any?, needSecurity: Boolean) { + Log.d( + tag, if (needSecurity) + LogSecureHelper.cast2Star(msg.toString()) + else + msg.toString() + ) + } + + fun i(tag: String, msg: Any?, needSecurity: Boolean) { + Log.i( + tag, if (needSecurity) + LogSecureHelper.cast2Star(msg.toString()) + else + msg.toString() + ) + } + + fun e(tag: String, msg: Any?, needSecurity: Boolean) { + Log.e( + tag, if (needSecurity) + LogSecureHelper.cast2Star(msg.toString()) + else + msg.toString() + ) + } +} \ No newline at end of file