-
Notifications
You must be signed in to change notification settings - Fork 39
File Store
Tony Shen edited this page Oct 27, 2021
·
10 revisions
有些 App 对产生的日志有存储到文件的需求,L 的 file 模块提供了文件存储到 sdcard 的功能。
需要添加如下的依赖:
implementation 'com.github.fengzhizi715.SAF-Kotlin-log:file:<latest-version>'
通过 FileBuilder 可以构造一个 FilePrinter
FileBuilder().folderPath(sdcardPath).fileNameGenerator(NetdiagnoseFileNameGenerator(
PREFIX_APP
)).cleanStrategy(cleanStrategy).build()
FileBuilder 构造的 FilePrinter 包含了文件名的生成策略、文件清理的策略。
一个完整的 LogManager, 大致如下:
object LogManager {
const val PREFIX_APP = "netdiagnose-app-"
const val SEVEN_DAYS:Long = 24*3600*1000*7
private val cleanStrategy = FileLastModifiedCleanStrategy(SEVEN_DAYS)
val filePrinter: FilePrinter by lazy {
var sdcardPath = "/sdcard/netdiagnose"
val f = Environment.getExternalStorageDirectory()
if (f != null) {
sdcardPath = f.absolutePath + "/netdiagnose/app"
}
FileBuilder().folderPath(sdcardPath).fileNameGenerator(NetdiagnoseFileNameGenerator(
PREFIX_APP
)).cleanStrategy(cleanStrategy).build()
}
@JvmStatic
fun initLog() {
configL {
header = "App Version ${App.CONTEXT.getAppVersion()}"
converter = GsonConverter()
}.apply {
addPrinter(filePrinter)
}
}
}
targetSdkVersion 29
AndroidManifest.xml 中 application 添加如下的属性: android:requestLegacyExternalStorage="true"
App Crash 时,产生的日志也可以存到文件中
在 Application 中作如下的配置,即可
crashPrinter = FileBuilder().folderPath("/storage/emulated/0/crash_logs").build()
CrashUtils.init(tag = "crashTag",printer = crashPrinter, onCrashListener = object : CrashUtils.OnCrashListener {
override fun onCrash(crashInfo: String, e: Throwable) {
}
})