Line number in android log util
Is there any way to print file lin开发者_如何学JAVAe number with android Log ?
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(), "
精彩评论