0

For some user experience related stuff, I want to capture logs of only my App (not the whole logcat) and save them in a text file. I am using Kotlin for my app and it supports latest version of android.

I checked many solutions and answers on SO for this but many of them were written for older versions of Android and reading system logs is not possible as android.permission.READ_LOGS is only granted to system apps.

Saving Logcat to a text file in Android Device

Android Writing Logs to text File

I also checked some libraries, but they are not working on the latest versions of android. One of them is https://github.com/hypertrack/hyperlog-android#initialize

Also, does capturing our own App's logs from user's device violates Play store's policies or we can do it safely?

RKB
  • 106
  • 10
  • `does capturing our own App's logs from user's device violates Play store's policies` this we can't answer – a_local_nobody Apr 19 '21 at 12:08
  • If you want to capture logs generated by your code, you can use a logging library instead of the default `Log.i` etc. That will allow you to write logs to logcat on debug and in a file on release. This may help: https://medium.com/android-news/my-timber-setup-493a8ec7a10c – Muhammad Faiq Apr 19 '21 at 12:13

1 Answers1

0

From what I got, it is actually possible to get logs from logcat without android.permission.READ_LOGS permission. The following snipped worked for me on a modern Android device. Disclaimer: this is quick and dirty; the logs are written into an arbitrary external location. Therefore you need android.permission.WRITE_EXTERNAL_STORAGE if you go the same way.

    try {
        val filename = File(getExternalFilesDirs(null)[0]!!, "log.txt")
        val cmd = "logcat -r 100 -f " + filename.getAbsolutePath() + " ActivityManager:I MyAwesomeApp:V"
        filename.createNewFile()
        Runtime.getRuntime().exec(cmd)
    } catch (e: IOException) {
        e.printStackTrace()
    }
lnstadrum
  • 550
  • 3
  • 15