使用:
添加依赖
implementation 'log4j:log4j:1.2.17'
implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'
添加存储权限
初始化配置
class App : Application() {
override fun onCreate() {
super.onCreate()
val config = LogConfigurator()
val filePath = "${Environment.getExternalStorageDirectory()}/myapp.log"
println("filePath = $filePath")
config.fileName = filePath
config.rootLevel = Level.DEBUG
config.setLevel("org.apache", Level.ERROR)
config.configure()
}
}
class MainActivity : AppCompatActivity() {
private val log: Logger = Logger.getLogger(MainActivity::class.java.simpleName)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
log.info("Hello")
log.debug("World")
}
}
控制台输出效果如下:
2022-07-20 11:54:23.133 8663-8663/cn.android666.android_sip I/MainActivity: Hello
2022-07-20 11:54:23.134 8663-8663/cn.android666.android_sip D/MainActivity: World
SD卡根目录中的myapp.log文件内容如下:
2022-07-20 11:54:23,130 - [INFO::MainActivity::cn.android666.android_sip.MainActivity] - Hello
2022-07-20 11:54:23,133 - [DEBUG::MainActivity::cn.android666.android_sip.MainActivity] - World