最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Android, how to print out coroutine id or name on a custom log function - Stack Overflow

programmeradmin8浏览0评论

I want to modify this log helper function to print out coroutine id or name.

   @JvmInline
   value class MyLogger(private val tag: String) {       
    fun log(level: Level, e: Throwable? = null, message: String) {
    if (isLoggingEnabled(level)) {
        val logTime = System.currentTimeMillis()
        val messageWithCoroutineInfo = "Running on: [${Thread.currentThread().name}] | $message"
        when (level) {
            Level.VERBOSE -> Log.v(tag, messageWithCoroutineInfo, e)
            Level.DEBUG -> Log.d(tag, messageWithCoroutineInfo, e)
            Level.INFO -> Log.i(tag, messageWithCoroutineInfo, e)
            Level.WARN -> Log.w(tag, messageWithCoroutineInfo, e)
            Level.ERROR -> Log.e(tag, messageWithCoroutineInfo, e)
            Level.ASSERT -> Log.wtf(tag, messageWithCoroutineInfo, e)
        }
    }
}
 ....
}


private val logger = MYLogger("Navigator")
logger.i { "goTo: $screen; backstack: [${backStack.joinToString()}]" }

In onCreate function:

    override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    System.setProperty("kotlinx.coroutines.debug", if(!BuildTypes.isProduction) "on" else "off")

it can print out the thread name, but it can't print out the coroutine id or name, such as [AWT-EventQueue-0 @coroutine#40], any suggestion?

发布评论

评论列表(0)

  1. 暂无评论