Browse Source

feature:zap模块增加过时日志处理

#Suyghur 4 years ago
parent
commit
180f3f86f0

+ 2 - 1
library_zap/src/main/java/com/dolin/zap/entity/Config.kt

@@ -18,7 +18,8 @@ class Config private constructor(builder: Builder) {
         private set
     var compressEnable: Boolean
         private set
-    private var overdueDayMs = 0L
+    var overdueDayMs = 0L
+        private set
     private var fileSizeLimitDayByte = 0
 
 

+ 0 - 8
library_zap/src/main/java/com/dolin/zap/impl/LogCleaner.kt

@@ -1,8 +0,0 @@
-package com.dolin.zap.impl
-
-/**
- * @author #Suyghur.
- * Created on 4/15/21
- */
-class LogCleaner {
-}

+ 9 - 4
library_zap/src/main/java/com/dolin/zap/impl/ZapPrint.kt

@@ -10,6 +10,7 @@ import com.dolin.zap.entity.ZapData
 import com.dolin.zap.format.DateFileFormatter
 import com.dolin.zap.internal.IPrint
 import com.dolin.zap.lifecycle.ZapLifecycle
+import com.dolin.zap.util.LogFileUtils
 import java.io.File
 import java.lang.reflect.Array
 import java.text.SimpleDateFormat
@@ -32,7 +33,7 @@ class ZapPrint : IPrint {
 
     fun initialize(application: Application, config: Config) {
         if (hasInitialized) {
-            throw IllegalArgumentException("Logger already initialize")
+            return
         }
 
         this.logDir = if (TextUtils.isEmpty(config.logDir)) {
@@ -46,14 +47,18 @@ class ZapPrint : IPrint {
         } else {
             config.tag
         }
-
         this.logcatLevel = config.logcatLevel
         this.recordLevel = config.recordLevel
-        val logFileName = logDir + File.separator + SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date()) + "-zap"
+
+        val logFileName = logDir + File.separator + SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date()) + ".zap"
         if (config.recordEnable) {
-            record2MMap = Record2MMap("$logDir/zap.cache", 1024 * 400, logFileName, config.compressEnable)
+            record2MMap = Record2MMap("$logDir/.cache", 1024 * 400, logFileName, config.compressEnable)
             ZapLifecycle.registerZapLifeCallback(application, record2MMap!!)
         }
+
+        Thread {
+            LogFileUtils.cleanOverdueLog(application, logDir, config.overdueDayMs)
+        }.start()
     }
 
     fun recycle() {

+ 30 - 0
library_zap/src/main/java/com/dolin/zap/util/LogFileUtils.kt

@@ -0,0 +1,30 @@
+package com.dolin.zap.util
+
+import android.content.Context
+import java.io.File
+
+/**
+ * @author #Suyghur.
+ * Created on 4/15/2021
+ */
+object LogFileUtils {
+
+    fun cleanOverdueLog(context: Context, logFolderPath: String, overdueDayMs: Long) {
+        val folder = File(logFolderPath)
+        if (!folder.exists()) {
+            return
+        }
+        folder.walk().maxDepth(1)
+                .filter { it.isFile }
+                .filter { it.extension == "zap" }
+                .forEach {
+                    if (isFileOverdue(it, overdueDayMs)) {
+                        it.delete()
+                    }
+                }
+    }
+
+    private fun isFileOverdue(file: File, overdueDayMs: Long): Boolean {
+        return System.currentTimeMillis() - file.lastModified() > overdueDayMs
+    }
+}