org.slf4j:slf4j-android 無法在 Logcat 中輸出 debug 信息

原因:默認只輸出 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 輸出,客戶適配性最好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章