6

I'm using adb logcat in the **\Android\sdk\platform-tools directory. I'd like to filter out the log messages by package name, so I can see just the log messages that come from my app. I've gotten as far as using

adb logcat *:E

which only displays the messages on the error level. Then I tried to filter out things by tags (so if there is no other way to filter things out by app/package name, I thought I'd just put a name in the tag to filter those), but running something like

adb logcat TAG:E

doesn't seem to filter out anything. I've had a look at the Android Studio user guide, but that didn't get me any further, either.

I'm using Android Studio, and I'd like to see the log of my app after the app crashed when I'm really using it and not just debugging in Android Studio.

Thanks in advance for any help or tips.

Abby
  • 1,610
  • 3
  • 19
  • 35

1 Answers1

13

You can filter adb logcat output by process ID by using the --pid=<pid> option.

To get the process ID for your app, you can run adb shell ps | FINDSTR <app name> (for Windows) or adb shell ps | grep <app name> (for *nix and OSX) while the app is still running.

Since you are trying to get logcat output after the app has crashed, the ps command won't work. You can generally filter logcat output by running adb logcat | FINDSTR <search term> (for Windows) or adb logcat | grep <search term> (for *nix and OSX).

This way, you can still assign meaningful tags to your debug messages and further filter on them.

Hope this helps!

Robert Li
  • 425
  • 1
  • 4
  • 9
  • Hmm, it returns `'grep' is not recognized as an internal or external command, operable program or batch file.` – Abby Sep 02 '16 at 17:52
  • @Abby Where are you running these commands? What operating system are you using? – Robert Li Sep 02 '16 at 19:20
  • 1
    I'm using Windows 8.1, running the commands in `**\Android\sdk\platform-tools` (as mentioned in the question). – Abby Sep 02 '16 at 19:22
  • @Abby Ah, apologies. `grep` is a Unix tool. Try `adb shell ps | FINDSTR ` instead? If that works I'll update the answer. – Robert Li Sep 02 '16 at 19:24
  • That's alright. Well, it doesn't do anything. It doesn't give me an error, either... So I suppose that's something? – Abby Sep 02 '16 at 19:27
  • 1
    @Abby Hm, I just saw your update that you're doing this after the app has crashed. That's a problem, because `ps` returns a list of running processes, and your app's not running anymore. It looks like the best you can do is to filter all of the logcat output using `FINDSTR`, but that will return any lines with the search term, regardless of where it appears. That would be `adb logcat | FINDSTR `. – Robert Li Sep 02 '16 at 19:33
  • Ah, hey, I found out what the ID (if that's the proper way to refer to the number between the brackets) of my app is, and used `adb logcat | FINDSTR `. I think that did the trick, thanks a bunch! – Abby Sep 02 '16 at 19:38
  • Awesome, glad it worked! – Robert Li Sep 02 '16 at 19:40
  • On a Marshmallow device, I'm getting the error: `Unrecognized Option -` from `adb logcat --pid=...`. It then lists valid options, none of which seems to be about filtering by pid. There is something about white/black lists, but I don't really understand what that means. Any ideas? – Jules Jun 25 '17 at 14:43
  • Solution as a one-liner: `adb logcat --pid=$(adb shell pidof -s com.example.app)` (from [here](https://stackoverflow.com/a/48004086/2441655)) – Venryx Sep 19 '19 at 22:56