Android Error Logging - android

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.

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.

Capture output from adb logcat

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)

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

Use grep with python subproccess.check_output and adb logcat

I am trying to write a python function that captures the time it takes for an android device to get gps fix. I want to do this by capturing the timestamp of the logcat record which mentions that system time is syncronised with GPS. So I submit an adb logcat with timestamp and grep for "Setting system time at". The I want to give the timestamp value of this line to a varialble. Specifically I want to capture the characters 10-18 of this line as this reflects the exact time from device boot. Once the string has been detected once I want the function to exit and my program to continue.
I try in the following:
cmdtimegps='adb logcat -v time| grep "Setting system time at"'
w=subprocess.check_output(cmdtimegps.split())
print w
My main problem is making the command (adb logcat -v time| grep SetTimeFromGPSService) work. I get a "/system/bin/sh: grep: not found" error. Any help with this or advice on the following steps would be very welcome. Many thanks in advance,
You have a couple of problems. First, this is a piped shell command so you need to pass the string without splitting it into a list and set shell=True. Second, check_output is a bad choice here because it only returns after the program terminates but your particular command runs forever.
A better solution is to stop grepping and have python process the command. Then you can kill it when you see the data you want.
proc = subprocess.Popen(['adb', 'logcat', '-v', 'time'], stdout=subprocess.PIPE)
for line in proc.stdout:
if "Setting system time at" in line:
proc.kill()
break
proc.wait()

How to keep android device log from not being cleared?

I want to dump all logs out to test a long usage of my app, maybe 2-3 days, but the logs seem to be cleared out while running after a long time.
How can I keep the logs?
I do not want to connect my device with eclipse all the day, I want the log just keep for me, like store it in the sd card for further check.
I had same requirement for my application. By default there is logcat process that tracks logs up to around 64kb (depends on device).
You can start your own logcat process using adb shell with some custom arguments. For example:
adb shell logcat -f /mnt/sdcard/large.log -r 100 -n 10
This will save up to 1mb of logs evenly distributed among 10 files (large.log, large.log.0, larget.log.1, etc.);
The next step is to launch logcat from your application:
String [] args = new String[] { Logcat, "-v", "threadtime",
"-f", logFile.getAbsolutePath(),
"-r", Integer.toString(sizePerFile),
"-n", Integer.toString(rotationCount),
"*:"+filter};
Runtime.getRuntime().exec(args);
So now your app may start saving logs. But you should be careful not to enable this in your production versions of application (or at least make this opt-in feature).
Instead of using the eclipse log use the apps like 'log viewer'. I think that app can solve your issue. I think the log will remain until you clear the log manually in 'log viewer'. I just downloaded it today morning and still has the logs of actions I performed just after I switched on the device.
Did you try dumping the log to a file?
adb logcat > log.txt

Categories

Resources