开发者

Line number in android log util

开发者 https://www.devze.com 2023-04-13 04:13 出处:网络
Is there any way to print file lin开发者_如何学JAVAe number with android Log ? for getting line number `

Is there any way to print file lin开发者_如何学JAVAe number with android Log ?

Line number in android log util


for getting line number `

public static int getLineNumber() {
    return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
or 
Thread.currentThread().getStackTrace()[2].getLineNumber()
 in Log()

` try it will help u....


Better Logging In Android Using Timber with Log Line Number

In /app/build.gradle file

implementation 'com.jakewharton.timber:timber:4.7.1'

In Application class

Timber.plant(new Timber.DebugTree() {
                @Override
                protected @Nullable String createStackElementTag(@NotNull StackTraceElement element) {
                    return super.createStackElementTag(element) + ":"+element.getLineNumber();
                }
            });


object Logg {
  private fun tag(): String? {
    return Thread.currentThread().stackTrace[4].let {
      val link = "(${it.fileName}:${it.lineNumber})"
      val path = "App# ${it.className.substringAfterLast(".")}.${it.methodName}"
      if (path.length + link.length > 80) {
        "${path.take(80 - link.length)}...${link}"
      } else {
        "$path$link"
      }
    }
  }

  fun v(msg: String?) {
    Log.v(tag(), "
0

精彩评论

暂无评论...
验证码 换一张
取 消