17

Is it possible to also display the Log's Package Name in each line?
Using

logcat -v long

leaves exactly the package name field (after PID) empty.
I want to filter the Logs from a specific application with different Tags, just wondering if it is possible.

Felix Plaumann
  • 235
  • 1
  • 2
  • 8
  • I'd like to comment for @Alexander, but apparently I only have enough brownie points to answer. The pipe through `cut -c10-15` grabs the `pid` from the whole line of text. cut --help – grossdm Jul 21 '18 at 09:03
  • 1
    Does this answer your question? [Filter LogCat to get only the messages from My Application in Android?](https://stackoverflow.com/questions/6854127/filter-logcat-to-get-only-the-messages-from-my-application-in-android) – nima moradi Mar 05 '20 at 15:45

3 Answers3

40

logcat record does not have a "package name field". Therefore there is no standard/built-in way to filter by it.

Although since Android 7.0 you can use logcat --pid option combined with pidof -s command to filter output by binary/package name:

adb shell "logcat --pid=$(pidof -s <package_name>)"

Replace " with ' for Linux/MacOS

Alex P.
  • 28,775
  • 16
  • 113
  • 160
  • Not working for me on an Marshmallow-based device: `$ adb -d shell logcat --pid=6693 Unrecognized Option - Usage: logcat [options] [filterspecs]`... doesn't seem like `--pid` is a valid option any more. – Jules Jun 25 '17 at 14:45
  • 1
    @Jules, `logcat --pid` was introduced in Nougat – Alex P. Jun 25 '17 at 15:01
  • It work for me. The only things is that I type first `adb shell` then on the shell I type `logcat --pid=$(pidof -s )` – Majonsi May 03 '19 at 14:07
  • 1
    It works for me. Remember to replace " with ' for Linux/MacOS – Shailesh Mar 31 '20 at 05:57
  • For Windows PowerShell: `adb logcat --pid=$(adb shell pidof -s )` – rolinger Feb 04 '22 at 10:05
7

This is my script. A little rusty but still working.

PID=`adb shell ps | grep -i <your package name> | cut -c10-15`;
adb logcat | grep $PID

Actually I have a python script to add more colours, and a while loop to keep monitoring for that process when it gets restarted, but I think you'll get the point.

Lucas Vidal
  • 79
  • 1
  • 2
  • I actually don't know how to use this.. I neither know how to launch scripts from the command line nor how to use this in my Android app (where I actually want/need this). I am currently doing it with Runtime.getRuntime().exec("su -c logcat -d -v long"); but I don't get Package names. – Felix Plaumann Jan 22 '16 at 09:39
2

Option 1

Enter these line by line:

adb shell
su
bash
PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
function pid2pkg() { pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1); if [ "$pkgName" != "" ] ; then echo $pkgName; else echo "*NOT RUNNING*"; fi }
eval "$(logcat -d | sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2 \$\(pid2pkg \3\) \5/g' | sed -r -e 's/(.+)/echo -e \"\1\"/g')"

The logcat output will be in the following format:

[Time] [Name] [Type] [Message]

Example:

14:34:59.386 wpa_supplicant E wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
14:35:02.231 com.android.phone D TelephonyProvider: subIdString = 1 subId = 1
14:35:03.469 android.hardware.wifi@1.0-service E WifiHAL : wifi_get_logger_supported_feature_set: Error -3 happened.
14:35:03.518 system_server I WifiService: getWifiApEnabledState uid=10086
14:35:03.519 dev.ukanth.ufirewall D AFWall  : isWifiApEnabled is false
14:35:03.520 system_server I GnssLocationProvider: WakeLock released by handleMessage(UPDATE_NETWORK_STATE, 0, 123)
14:35:03.522 dev.ukanth.ufirewall I AFWall  : Now assuming wifi connection

Some system processes don't have packages. system_server and wpa_supplicant for instance. If a package name can't be found, the process name will be displayed instead.

Caveats:

If the process behind a certain PID is not running anymore, you can't get the package/process name anymore.

After a process exited itself or your device restarted, the PIDs may actually be assigned to completely different processes.

So you might want to check for how long the process has actually been running:

ps -e -o pid -o stime -o name

Option 2

If you want the formatting to be a bit more readable, you can copy my logging script to your device and execute it:

better_logging.sh

PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
MAX_LEN=$(echo "$PID_LIST_CACHE" | cut -d ' ' -f1 | awk '{ if ( length > L ) { L=length} }END{ print L}')
function pid2pkg() {
    pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1);
    if [ "$pkgName" != "" ] ; then
        printf "%-${MAX_LEN}s" "$pkgName";
    else
        printf "%-${MAX_LEN}s" "<UNKNOWN (NOT RUNNING)>";
    fi
}

eval "$(\
logcat -d | \
sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2\ $\(pid2pkg \3\) \5/g' | \
sed -r -e 's/(.+)/echo -e \"\1\"/g' \
)" | \
awk '
function color(c,s) {
    printf("\033[%dm%s\033[0m\n",90+c,s)
}
/ E / {color(1,$0);next}
/ D / {color(2,$0);next}
/ W / {color(3,$0);next}
/ I / {color(4,$0);next}
{print}
'

To copy and run it, you can use:

adb push better_logging.sh /sdcard/better_logging.sh
adb shell "bash /sdcard/better_logging.sh"

Output will look like this:

log_screenshot

Forivin
  • 13,544
  • 25
  • 91
  • 183
  • 1
    Is there anyway to do this without root? Having the PID updated to the process name in a adb logcat would be amazing. – rcmpayne Mar 06 '20 at 03:27