Просмотр исходного кода

feature:logger模块Config配置类开发

#Suyghur 4 лет назад
Родитель
Сommit
bc4b26b530

+ 9 - 0
demo/src/main/java/com/suyghur/dolin/simple/Le.kt

@@ -0,0 +1,9 @@
+package com.suyghur.dolin.simple
+
+/**
+ * @author #Suyghur.
+ * Created on 4/7/2021
+ */
+enum class Le {
+
+}

+ 23 - 1
demo/src/main/java/com/suyghur/dolin/simple/Test.java

@@ -1,11 +1,33 @@
 package com.suyghur.dolin.simple;
 
+import android.app.Activity;
+import android.util.Log;
+
+import com.suyghur.dolin.logger.Config;
+
 /**
  * @author #Suyghur.
  * Created on 4/7/21
  */
 public class Test {
 
-//    Logger.Config
 
+    String logDir = "";
+    private int logcatLevel = -1;
+    private int recordLevel = -1;
+    private long overdueDayMs = 0L;
+    private int fileSizeLimitDayByte = 0;
+
+
+    @Override
+    public String toString() {
+//        Log.DEBUG
+        return "Test{" +
+                "logDir='" + logDir + '\'' +
+                ", logcatLevel=" + logcatLevel +
+                ", recordLevel=" + recordLevel +
+                ", overdueDayMs=" + overdueDayMs +
+                ", fileSizeLimitDayByte=" + fileSizeLimitDayByte +
+                '}';
+    }
 }

+ 1 - 0
library_logger/build.gradle

@@ -10,6 +10,7 @@ android {
     defaultConfig {
         minSdkVersion MIN_SDK_VERSION
         targetSdkVersion TARGET_SDK_VERSION
+        ndkVersion "21.1.6352462" // <<==== Here
 
         externalNativeBuild {
             cmake {

+ 84 - 1
library_logger/src/main/java/com/suyghur/dolin/logger/Config.kt

@@ -1,8 +1,91 @@
 package com.suyghur.dolin.logger
 
+import android.content.Context
+import android.text.TextUtils
+
 /**
  * @author #Suyghur.
  * Created on 4/7/21
  */
-class Config {
+class Config private constructor(builder: Builder) {
+
+    var logDir = ""
+        private set
+    private var logcatLevel: Level
+    private var recordLevel: Level
+    private var overdueDayMs = 0L
+    private var fileSizeLimitDayByte = 0
+
+
+    init {
+        this.logDir = builder.logDir
+        this.logcatLevel = builder.logcatLevel
+        this.recordLevel = builder.recordLevel
+        this.overdueDayMs = builder.overdueDay * 24 * 3600 * 1000L
+        this.fileSizeLimitDayByte = builder.fileSizeLimitDay * 1024 * 1024
+    }
+
+    override fun toString(): String {
+        return "Config{" +
+                "logDir='" + logDir + '\'' +
+                ", logcatLevel=" + logcatLevel +
+                ", recordLevel=" + recordLevel +
+                ", overdueDayMs=" + overdueDayMs +
+                ", fileSizeLimitDayByte=" + fileSizeLimitDayByte +
+                '}'
+    }
+
+    class Builder(context: Context) {
+        internal var logDir = context.getExternalFilesDir("dolin")!!.absolutePath
+        internal var logcatLevel = Level.DEBUG
+        internal var recordLevel = Level.DEBUG
+        internal var overdueDay = 3
+        internal var fileSizeLimitDay = 15
+
+        /**
+         * 日志存储路径,默认是应用的私有目录下dolin文件夹
+         */
+        fun setLogDir(logDir: String): Builder {
+            if (!TextUtils.isEmpty(logDir)) {
+                this.logDir = logDir
+            }
+            return this
+        }
+
+        /**
+         * 允许输出到logcat的日志的最低级别,默认为[Level.DEBUG]级别
+         */
+        fun setLogcatLevel(level: Level): Builder {
+            this.logcatLevel = level
+            return this
+        }
+
+        /**
+         * 允许记录到文件的日志的最低级别, [Level.NONE]会禁用记录,默认为[Level.DEBUG]级别
+         */
+        fun setRecordLevel(level: Level): Builder {
+            this.recordLevel = level
+            return this
+        }
+
+        /**
+         * 日志过期天数,过期日志将自动清除,默认为3
+         */
+        fun setOverdueDay(overdueDay: Int): Builder {
+            this.overdueDay = overdueDay
+            return this
+        }
+
+        /**
+         * 单天日志文件存储上限,单位 MB,默认为15
+         */
+        fun setFileSizeLimitDay(mb: Int): Builder {
+            this.fileSizeLimitDay = mb
+            return this
+        }
+
+        fun create(): Config {
+            return Config(this)
+        }
+    }
 }

+ 6 - 7
library_logger/src/main/java/com/suyghur/dolin/logger/Level.kt

@@ -4,13 +4,12 @@ package com.suyghur.dolin.logger
  * @author #Suyghur.
  * Created on 4/7/21
  */
-object Level {
-    const val ALL = -1
-    const val VERBOSE = 0
-    const val DEBUG = 1
-    const val INFO = 2
-    const val WARNING = 3
-    const val ERROR = 4
+enum class Level {
+    NONE,
+    DEBUG,
+    INFO,
+    WARNING,
+    ERROR,
 }
 
 

+ 7 - 0
library_logger/src/main/java/com/suyghur/dolin/logger/impl/LoggerImpl.kt

@@ -76,6 +76,13 @@ class LoggerImpl : ILogger {
         private object LoggerImplHolder {
             val INSTANCE = LoggerImpl()
         }
+
+        /**
+         * 防止单例对象在反序列化时重新生成对象
+         */
+        private fun readResolve(): Any {
+            return LoggerImplHolder.INSTANCE
+        }
     }