原因:默认只输出 Info 级别以上信息

解决:设置测试 Android 系统的 prop:

./adb shell setprop log.tag.<Your Log Tag> DEBUG

将 <Your Log Tag> 替换成你的具体 tagName。
tagName 在使用 下面代码获取 Logger 时指定。下例中的 tagName = "MyApplication"。

private val logger: Logger = org.slf4j.LoggerFactory.getLogger(MyApplication::class.java)

adb 文件的位置,可以查看 sdk 安装位置

补充:
slf4j-android 在输出 log 时,先调用 android.util.log 包中的 isLoggable() 来确定是否输出 log。

https://developer.android.google.cn/reference/android/util/Log#isLoggable(java.lang.String,%20int)

isLoggable

Added in API level 1

public static boolean isLoggable (String tag, 
                int level)

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: 'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, or ASSERT. You can also create a local.prop file that with the following in it: 'log.tag.<YOUR_LOG_TAG>=<LEVEL>' and place that in /data/local.prop.

 org.slf4j.slf4j-api,这个包很有意思,它只接收 logger.XXX 输出指令,但没有去实现实际输出,而是由其它包来决定如何输出日志。这种代码解耦方式,在用户想更换日志输出方式时,很方便。只要引入相应的辅助包即可,无需修改代码。

像标题中所说的 slf4j-android,还有 slf4j-simple,ch.qos.logback 等等,你想把 log 存本地文件,发网络服务器,或直接在控制台输出,只要简单引用相应的日志管理开发包即可。

这对于第三方组件来说,以这种方法提供 log 输出,客户适配性最好。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐