Filter LogCat to get only the messages from My App

2019-09-14 12:06发布

I observed that when i use Logcat with Eclipse with ADT for Android, I get messages from many other applications as well. Is there a way to filter this and show only messages from my own application only.

30条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-14 12:15

This is obviously a question aimed at usage of Logcat from outside of the developer device, however if you want to display Logcat output on the device (programmatically), you just need this:

Runtime.getRuntime().exec("logcat " + android.os.Process.myPid() + " *:D");

The *:D at the end filters out every message below Debug log level but you can leave that out.

To direct the output to, say, a TextView, see for example here.

查看更多
戒情不戒烟
3楼-- · 2019-09-14 12:16

Update May 17

It's been a few years, and thing have changed. And Eclipse is no longer officially supported. So here's two more up-to-date approaches:

1. Android Studio

enter image description here In the Android monitor toolbox, you can filter logcat per debuggable process. Normally, when you develop an application it is a debuggable process. Every once in a while I am having issues with this, and a do the following:

  1. Tools -> Android -> Enable ADB Integration.
    If it was already enabled, then toggle it off, and then back on

  2. Unplug and replug your mobile device.

There are also options to filter via regex and the debug level

2. logcat-color

This is a nice python wrapper on top of adb logcat if you want to use a terminal based solution. The good thing about it is that you can save multiple configurations and simply reuse them. Filtering by tags is quite reliable. You can also filter by package to see logs of one or more apps only, but you start logcat-color right before launching your app.

Old Answer:

It seems that I can't comment to previous answers, so I will post a new one. This is a comment to Tom Mulcahy's answer, that shows how the command should change so as to work on most devices, since adb shell ps PID column is variable.

NOTE: The command below works for the cases where you have connected many devices. So device id is needed. Otherwise, you can simply omit the brackets '[', ']'

1. To find out the column of pid, type:

adb [-s DEVICE_ID] shell ps | head -n 1

Now memorise the column number for the PID. Numbering starts from 1.

2. Then type the following:

adb [-s DEVICE_ID] logcat | grep $(adb [-s DEVICE_ID] shell ps \
| grep "com.example" | awk -F" " ' {print $PUT_COLUMN_HERE}')

Simply put the column you memorised in PUT_COLUMN_HERE, e.g. $5

Caveat

Each time you re-run your application, you have to re-run the 2nd command, because the application gets a new PID from the OS.

查看更多
狗以群分
4楼-- · 2019-09-14 12:16

Package names are guaranteed to be unique so you can use the Log function with the tag as your package name and then filter by package name:

NOTE: As of Build Tools 21.0.3 this will no longer work as TAGS are restricted to 23 characters or less.

Log.<log level>("<your package name>", "message");

adb -d logcat <your package name>:<log level> *:S

-d denotes an actual device and -e denotes an emulator. If there's more than 1 emulator running you can use -s emulator-<emulator number> (eg, -s emulator-5558)

Example: adb -d logcat com.example.example:I *:S

Or if you are using System.out.print to send messages to the log you can use adb -d logcat System.out:I *:S to show only calls to System.out.

You can find all the log levels and more info here: https://developer.android.com/studio/command-line/logcat.html

http://developer.android.com/reference/android/util/Log.html

EDIT: Looks like I jumped the gun a little and just realized you were asking about logcat in Eclipse. What I posted above is for using logcat through adb from the command line. I'm not sure if the same filters transfer over into Eclipse.

查看更多
走好不送
5楼-- · 2019-09-14 12:17

Ubuntu : adb logcat -b all -v color --pid=`adb shell pidof -s com.packagename` With color and continous log of app

查看更多
何必那么认真
6楼-- · 2019-09-14 12:17

Just use the filters inside the logcat. There is a button there that lets you add filters. Just specify the application ID, make sure it is selected afterwards, and you're done, easy!

查看更多
太酷不给撩
7楼-- · 2019-09-14 12:18

In addition to Tom Mulcahy's answer, if you want to filter by PID on Windows' console, you can create a little batch file like that:

@ECHO OFF

:: find the process id of our app (2nd token)
FOR /F "tokens=1-2" %%A IN ('adb shell ps ^| findstr com.example.my.package') DO SET PID=%%B

:: run logcat and filter the output by PID
adb logcat | findstr %PID%
查看更多
登录 后发表回答