0

my problem isn't really that hard probably. I just want to save the log from logcat in android studio using Kotlin. Basically, I just need to save all of the text from the logcat in a readable manner as a text file whenever the application closes. It needs to work with API versions of 28 and up to version 31. Thank you very much ahead of time.

1 Answers1

0

I don't think you're supposed to

READ_LOGS Added in API level 1

public static final String READ_LOGS

Allows an application to read the low-level system log files.

Not for use by third-party applications, because Log entries can contain the user's private information.

There are some solutions here using Runtime#exec to run a logcat command - but honestly, if you just want to save your own app's logs, you're probably best using a library like Timber and creating a Tree that also writes out each line as well as calling Log.

(You could just create your own wrapper functions, but that requires you to remember to call them - Timber does a lot of the work for you, it's convenient!)

That won't catch other logged stuff generated by your app though, like library logs and system info.

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • Actually, I'm using this for a testing app that will be used by the company on only company sample phones. So it giving off the private information is ok. I am using timber as well, but I don't know how to use it to write my log to a file. – Dante Reinhardt Feb 03 '22 at 15:48
  • I think the implication there is the system prevents you from doing it unless your app is on a whitelist, that's how permissions like that usually work. You could try the solutions in that thread though, seems hacky but they might work for you? But if you're already using Timber and that level of logging works for you, just take a look at the sample: https://github.com/JakeWharton/timber/blob/trunk/timber-sample/src/main/java/com/example/timber/ExampleApp.java The ``Tree`` just calls through to some other logging library, you'd just need to write each message to a file you open as well – cactustictacs Feb 03 '22 at 15:54