1

In Swift, I can use the following code to log a message, and I have access to all the relevant data I need:

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

How can I do something similar in Android Java or Kotlin (preferable)?

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • I can't answer my own question yet but I ended up using the following code, it was exactly what I was after: [link](https://stackoverflow.com/a/60785969/19493002) – Daniel Marks Jul 07 '22 at 06:05

1 Answers1

0

Well, in Kotlin there isn't anything that can give you, for example, a line number so easily. However, on Android, in most of the times we use Timber as a logging 3rd party library, which is very convenient.

So, in Timber it can be achieved like this:

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

Timber.plant(LineNumberDebugTree())

You can find additional info in this thread.

Demigod
  • 5,073
  • 3
  • 31
  • 49