I am testing an app with 2 devices communicating using sockets and monitoring the timestamp values. I select the required device name from the DDMS perspective. But some initial logs are missing(as I have many Log.d statements in the code). I want to store the whole log file after I stop apps in both the phones. Can someone tell me how this can be done in command line using adb? I couldn't find any example for 2 devices.
Thanks
You can try with two console and get logcat separately for two devices
Get serial no for each devices using adb devices
and save logcat as text files
console 1: adb -s <device1serialNO> logcat -d > logcat1.txt
console 2: adb -s <device2serialNO> logcat -d > logcat2.txt
You can filter result for your given tag if needed
Before launching your application you can start the following command in your terminal:
adb logcat <your_application_log_tag>:V *:S > file.txt
<your_application_log_tag> is a log tag that you use in your application. *:S means that you suppress all log outputs from other components. > file.txt redirects the output of the command to file.txt.
The cause of the problem is that for logging Android has a buffer in RAM and if it becomes full it rewrites the most old entries (FIFO). The command I've provided will store the log on your computer.
Related
Note:
This question is a duplicate of Filter log messages by PID or application package in Android.
But there is no correct answer to that question & it is very very old.
I can't make any changes in the app codebase as suggested in the accepted answer, I can only access the app build.
As per Android Docs: https://developer.android.com/studio/command-line/logcat#alternativeBuffers
We should be able to filter logs of a given application using the command:
adb logcat --pid=<pid>
--pid= ... Only print logs from the given PID.
But I am getting an error "unrecognized option" when I enter it in cmd.
Got PID of application using:
adb shell ps <app-package-name>
Note: Want a solution without using cmd alone, not using Android Studio.
For adb --pid command to work , the device/emulator in which you have tired capturing the logcat messages should atleast be Android 7.0 . Otherwise this command will not work.
if you have device with android 7.0 above , then try this command
adb logcat --pid=19816 *:D
replace your pid value and * means (any tag) : D meas DEBUG severity log messages
if you have device which is older versions , you can search specific logcat messages based on your package name string or given tag strings like this
adb logcat | findstr <PACKAGENAME OR TAGS>
When I connect to a device or an emulator using adb, I can clear logcat using adb logcat -c. The issue I am facing is that I often get the following message:
failed to clear the 'main' log
AFAIK, 'main' is a non-rooted buffer which means that I should be able to clear it without root. What can then be the reason for this message?
You might have your Android Studio opened, please close it and run again , it should able to clear the logs.
use
adb logcat -b all -c
it will clear all buffers. sometime adb logcat -c will not work because of many process using it like android studio.
Logcat messages are stored in buffer, you can try,
adb logcat -c
Clear (flush) the selected buffers and exit. The default buffer set is main, system and crash. To clear all of the buffers, use -b all -c.
source: logcat doc
For more logcat usage, check How do I get the logfile from an Android device?
I'm running the command
logcat -d AndroidRuntime:E *:S
When ran from adb on my computer, it displays all the things it should. When I try to run it from an Android application with
Runtime.getRuntime().exec("logcat -d AndroidRuntime:E *:S");
and print the output, it won't display anything except the headers.
How can I fix this?
First, this has never been supported.
Second, if you are running on Android 4.1 and later, you will only get any log messages that your own app logs, not messages from other apps, as you can no longer hold the READ_LOGS permission in an ordinary SDK app.
I know that the boot up log can be obtained by pulling out contents of kmsg or dmesg through ADB.
But I'm not aware of how to retrieve the shutdown logs in Android as there's no /var folder in Android (place where most desktop linux distros generally store their shutdown logs).
So how can I obtain the shutdown logs in Android?
Look in some locations such as these:
/proc/last_kmsg
/data/tombstones/
/data/dontpanic/
/data/system/dropbox/
(This list isn't strictly kernel logs, including framework and application logs too, which are also sometimes of interest)
One work around I found for collecting shutdown logs in Android is to run adb pull /proc/kmsg C:\Logs.txt on the host PC and then switch off the device. You will get the logs till the USB communication between the host and the device snaps! I know this is only one case out of the numerous shutdown scenarios but I haven't found satisfactory answers for other cases!
TL;DR:
Run command through adb that copies logcat and proc/kmsg to a file and keep it running even when adb disconnects with nohup, disown or setsid. Probably needs busybox, needs root and adb root, too.
setsid cat proc/kmsg > /sdcard/kmsg.txt &
and
logcat -v long -f /sdcard/logcat.txt (somehow only works without setsid)
Or add normal copy commands to some startup script.
/TL;DR
You can constantly copy proc/kmsg and logcat to a file on your android device or a microSD card to get the logs even after adb disconnects.
You need root access and adb root access for this to work. For the latter, use the setting in the developer options if you have a custom rom or the adbd insecure app.
After using adb shell to get your android shell, type su to get superuser access.
Then you not only need to put an ampersand (&) after the command but also make sure that the command keeps running after adb disconnects. That is done by nohup, disown or setsid (see here for usage).
If it doesn't work because you don't have these commands, you need to install busybox.
See my question here.
See here for how to get logcat and kernel logs and print it to some file or merge it.
See developer.android.com/tools/help/logcat.html for parameters for the logcat command.
In the end you could have a command like setsid cat proc/kmsg > /sdcard/kmsg.txt & for the kernel messages.
For logcat you could have one of the following commands: logcat -v long -f /sdcard/logcat.txt or logcat -v long > /sdcard/logcat.txt
I don't know why, but sometimes it didn't work with setsid and just didn't copy continuously but stopped shortly after executing the command. In these situations, it also showed up when entering jobs, which it didn't otherwise. Then it just worked without setsid, it stayed alive after disconnecting and reconnecting. I guess you must just try when the file does keep getting larger. If someone figured out why it is behaving like it is... let me know and I'll edit the answer.
Probably adding the commands to a startup script could be a solution for some, too.
Hope this helps.
fightcookie
Newer phones do NOT use any of these locations so if you're reading this article then as of now
The kernel crash logs are now in /sys/fs/pstore instead of /proc/last_kmsg
I was looking for the same thing, and finally, I found the answer!
In android 8 all logs are located in \data\log\android_logs\... including apps and kernel logs. Kernel logs are called kmsgcat-log_timestamp_.gz
edit: Although this is a very old thread, I think the answer might be helpful.
I am running a custom android application. inside that application I have various Log statements.
Log.i(TAG, "I am in activityA") etc. Which log file and directory location will this log file appear in. Will it print to the log file on device and if so which one? Thanks Also where do I set levels which will print? I assume Log.d(TAG, "") will not appear on device logs since it needs to be in debug mode ... Also where to set log levels for deployed app in manifest?
You can use logcat to view the logs.
You can run logcat as an adb command : Open a command windw on your PC and type
$ adb logcat
(OR)
$ adb shell
# logcat
(OR)
You can execute it directly from the shell prompt of your target device.