【问题标题】:Kotlin equivalent to Swift Literal ExpressionKotlin 等价于 Swift Literal Expression
【发布时间】:2022-07-06 17:14:13
【问题描述】:

在 Swift 中,我使用以下代码记录消息,并收到我需要的所有相关数据:

class func log(type: LogType, message: String, fileName: String = #file, line: Int = #line, column: Int = #column, function: String = #function)

如何在 Android Java 或 Kotlin 中做类似的事情(首选)?

在此致谢

【问题讨论】:

    标签: java android swift kotlin logging


    【解决方案1】:

    好吧,在 Kotlin 中,没有任何东西可以如此轻松地为您提供例如行号。但是在Android上,大部分时候我们使用Timber作为日志的第三方库,非常方便。

    所以,在 Timber 中可以这样实现:

    class LineNumberDebugTree : Timber.DebugTree() {
        override fun createStackElementTag(element: StackTraceElement): String? {
        return "(${element.fileName}:${element.lineNumber})#${element.methodName}"
    }
    

    Timber.plant(LineNumberDebugTree())

    您可以在this 线程中找到更多信息。

    【讨论】:

      猜你喜欢
      • 2019-04-16
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多