Capture output from adb logcat - android

I am automating an Android application and I need to be able to capture logs while I am running the automation tests. I have tried using a terminal emulator but this only seems to give console logs. Next I tried using
log = subprocess.check_output(["adb", "logcat"])
But when I do this my automation script stops indefinitely (presumably because it is waiting to continue after the logcat capture is complete), which does not work for me because I need the logcat to run in the background, while my script is running. So maybe 'Popen' is the way to go, and then pipe the output? Thank you!

The logs are always "running in the background", it's just whether you're looking at them. I would suggest instead, when you need a log dump, using
adb logcat -d
which will dump what it currently has, then exit.
So at the start of your test run:
subprocess.call(shlex.split('adb logcat -c'))
to clear the logs.
So at the end (and before any restart), dump the logs:
log = subprocess.check_output(shlex.split('adb logcat -d'))
with open("loggy.file", "w") as f:
f.write(log)

Related

Filter android adb logs in terminal when using Timber

In my app, I am using Timber as a logger. I am accessing the logs from the terminal via adb using this isntruction:
./adb logcat com.company.my_app:D
I do get the logs but the issue is that I am getting a crazy amount of noise from the OS (ie SurfaceFlinger , GraphicBuffer, vndksupport) which make the logs harder to read.
Is there a way to filter the logs like in Android Studio and just get logs from my app. Thank you !
P.S. I have trie a few answers from here but none of them remove the noise.
What works best for me is to grep for the process ID which is displayed in every log line. In the example below 7098 is printed with every log line.
08-10 18:48:39.825 7098 7144 D NetworkModule: --> END POST
So this is the adb instruction used to get filtered logs:
adb logcat | grep -F "7098"
Note: the process ID is not static and it is going to change if app is hard-closed or device is restarted.
I don't know if it is the best solution, but it works in my case.

How can I tell which app is producing debug statements in Android Studio?

I'm trying to figure out what is producing this debug output:
04-25 15:58:04.883 1542-5012/? D/NetworkStatsCollection: getHistory:mUID 10266 isVideoCallUID: false
The above output runs continuously with my phone plugged in. I don't see this using the emulator and I've uninstalled the app I'm working on, so I don't think it's that. I've tried restarting the phone, to no avail. How can I determine what app is causing this?
Use adb shell ps | fgrep 1511 to see what app is tied to the PID of 1511, which according to your LogCat output is the app that is doing the logging.
(BTW, in the future, please post LogCat output as text, not screenshots)

Any way to grab a snapshot of current logcat output without opening up the output stream?

I'm trying to grab some filtered output from logcat through command line, but would prefer to not have a logcat stream opened up for automation purposes.
Here's my use case: I want to clear logcat, perform some instrumentation tests with AccessibilityChecks enabled, then check logcat for Accessibility errors that the class has found in my UI.
Currently, here's what I can do via command line:
//clear logcat history:
adb logcat -c
// Run instrumentation tests at this time.
// Run following command when tests done:
adb logcat AccessibilityValidator:E *:S
// ctrl-c to close stream
This does what it is supposed to. I will see just the Accessibility errors printed out. The problem is that I do not want to open up a logcat stream. I'd like to get the results as is, right at the point that my instrumentation tests have completed. Opening up a stream is not very ideal as I would then need to pipe in a ctrl-C to my pipeline to close the stream. I would really like to get a snapshot of the output in plain text. Is such a thing possible?
Thank you!
Edit: I was able to find what I believe is a working solution - I just needed to add the -d flag. This will not open a stream and will print the contents of the logcat file. Combine with my filtering, I am able to get exactly what I was looking for.
I was able to find what I believe is a working solution - I just needed to add the -d flag. -d will dump the logs and exit. This will not open a stream and will print the contents of the logcat file to stdout. Combine with my filtering, I am able to get exactly what I was looking for.
The command I needed is:
adb logcat -d AccessibilityValidator:E *:S

Can I execute logcat on the mobile phone itself by issung a command in the shell?

How can I see timestamp in Logcat? Is there any application which shows me the log along with the timestamp? Also, how can I increase the size of the Log in andriod phone?
To get timestamps: logcat -v time
To run logcat on the device at a shell prompt: logcat
There are several free logcat viewers for Android. I've heard that aLogCat is pretty good.
EDIT
You can set your preferences in aLogCat to see the time.
Regarding your second question, I found an answer here:
The logs are held by a kernel device; the entries are in /dev/log.
The buffer is currently 64KB, and there is no way to change the size
on a production device.
The easiest way to keep more of the log is to run "logcat" and send
the output to a file. logcat runs on the device, so you could run it
there and redirect it to (say) /sdcard/log.txt.

Android Error Logging

How do i retrieve the error logs of my application from device (and send them over the internet to a server)?
(Assuming you are using log framework from android.util.Log package.)
You can start "logcat" process with specific parameters from within your process. It will dump last 16k of logs (16k - is default for my phone, it can be different on other phones).
Here is an example of command line that dumps all logs: logcat -d -f /mnt/sdcard/log-dump.txt
Another example that dumps errors from all applications: logcat -d -f /mnt/sdcard/err-dump.txt *:e
You'll need to launch the process from within your application programatically. And then process log-dump.txt/err-dump.txt in the way you want.
You also may want to monitor logs longer then those default 16k can allow you. For this you'll need to start logcat without -d parameter. If this is done, logcat process will write logs to file for as long as you want. When you are done just kill logcat process.
In any case you can look & test manually logcat using adb logcat <params> from you computer.
I think you need to implement the try_catch block.
try
{
}
catch(Exception e)
{
Log.e("Exception found ",e.getMessage);
// post the exception message to your server
}
This way you can send the error log messages to the server.

Categories

Resources