android - Android kotlin 日志库
问题描述
我需要一个用于 android 的日志库,它将:
- 将日志写入某处的文件
/storage/emulated/0/Android/data/com.example.myproject/files/logs/
- 日志文件应该以某种方式命名
2021-01-01.17-39-29.txt
- 我希望能够指定
logs
目录可以包含多少个文件。例如,如果我将最大文件设置为 20。它将自动删除旧的日志文件。 - 我想控制日志记录过程,以便库仅在我调用该方法时才会写入文件,例如:
logfile.log(data)
现在,我知道有很多关于 android 在 stackoverflow 上登录的问题,但我想找到适合我所有需求的库。如果您知道一个好的,我将非常感谢您的帮助。
谢谢!
解决方案
首先,我推荐使用Timber来管理日志(这个库支持用于调试和生产目的的日志)。
然后,您需要编写一个函数将日志写入磁盘。
例如:
if (BuildConfig.DEBUG) {
// Default debug tree
// A Tree for debug builds. Automatically infers the tag from the calling class.
Timber.plant(Timber.DebugTree())
} else {
// Implement your custom Tree if need, eg. enable crashlytics,...
}
DebugTree
您现在可以编写函数以通过覆盖类将日志写入磁盘:
Timber.plant(object: Timber.DebugTree() {
// for example, if you need to write log when catched exceptions
override fun e(t: Throwable?) {
super.e(t)
}
// or here to check for all tags
override fun createStackElementTag(element: StackTraceElement): String? {
return super.createStackElementTag(element)
}
})
推荐阅读
- javascript - 尝试使用 Firebase 功能发送电子邮件时不断出错
- javascript - 根据另一个数组中值的存在向对象数组添加属性
- tabs - 如何关闭新选项卡并返回 Robot Framework 中的第一个选项卡?
- mongodb - 使用 let 变量的管道和 geoIntersect 查找
- android - 如何从 IntentService 类调用片段?
- sql - JOIN 查询,SQL Server 正在删除我的第一个表的一些行
- php - php ccavenue 整数:“错误代码:10001 无效请求 - 加密请求无效/不存在。”
- python-3.x - Debian 中的 Python 3 和土耳其语字符
- ios - 无法通过 XIB 在 UTableView 中添加 footerView
- service-worker - 在 Workbox 中使用缓存优先策略的主要目的是什么?