top of page

How to read values from logcat in Android Studio Programmatically?

  • agulevski10
  • May 31, 2021
  • 1 min read

I was stuck with a log values from one library that I was using in a project. The values that the library was producing were printed in Android studio logcat and visible. Some of those log values I needed inside my app, but there was no library function/method go get that kind of data. The idea was to cycle trough the log values and get what I needed. Here is the result: Kotlin:

JavaScript

private fun readLogs() { try { val process = Runtime.getRuntime().exec("logcat -d") val bufferedReader = BufferedReader( InputStreamReader(process.inputStream) ) val log = StringBuilder() var line: String? = "" while (bufferedReader.readLine().also { line = it } != null) { log.append(line) } Timber.i("LOG---> ${log.toString()}") } catch (e: IOException) { Timber.i(e) } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

private fun readLogs() {

try {

val process = Runtime.getRuntime().exec("logcat -d")

val bufferedReader = BufferedReader(

InputStreamReader(process.inputStream)

)

val log = StringBuilder()

var line: String? = ""

while (bufferedReader.readLine().also { line = it } != null) {

log.append(line)

}

Timber.i("LOG---> ${log.toString()}")

} catch (e: IOException) {

Timber.i(e)

}

}

Note: The flag “-d” in “logcat -d” means –Dump the log to the screen and exits-. If you want more options you should follow this link explaining the logcat in detail: https://developer.android.com/studio/command-line/logcat

Comments


bottom of page