Filter log messages by PID or application package in Android
Asked Answered
O

1

7

I understand that to filter Android log messages we can use something like

adb logcat ActivityManager:I MyApp:D *:S

But, in my application, I'm using different TAGS for different activities and I want to filter all the logs of this application only. What's the best way to do it?

Do I need to specify all the tags in the command?

Or using a common tag across the application, the only other alternative?

While looking at log messages in Eclipse, I notice that there is a column named PID and another named Application (contains name of app package) both of which are (obviously) same for different Tag for a given application. That suggests that it should be possible to filter not just by Tag but by pid/package as well.

Orelie answered 11/3, 2012 at 19:40 Comment(5)
Atul,use ActivityManager for getting your application PID then filler on the bases of PIDMammy
ahh, I updated my question before I saw your comment. Is it possible to filter by pid, that's what I want to do.Orelie
@imrankhan how do we filter by pid?Orelie
can you use with grep in pipe?Snapback
@blackbelt: Yes that would work but on the command line I'm just testing what the output looks like but actually I've to pass the arguments of logcat command to a library ACRA so grep in pipe wouldn't work there.Orelie
R
5

I use a common TAG format as follows.

For Activities for example, I have defined a base Activity class...

public class MyCompanyActivity extends Activity {
    protected final String TAG = this.getClass().getName();
    ...
}

All Activities I create extend that Activity, example.

public class FishActivity extends MyCompanyActivity {
    ...
}

The result is that FishActivity will have a TAG which is...

com.mycompany.myapp.FishActivity

All I then need to do is filter the logcat on com.mycompany.myapp

Riccio answered 11/3, 2012 at 20:3 Comment(6)
Thanks, this approach looks a clean way of doing it, we wouldn't even need to declare TAG variable in every activity, but just wondering is there any side-effect of using this approach? Since you said "I use..." so I guess this should work fine without any catch, right?Orelie
@AtulGoyal: I can't think of any side effect and have been using this approach since I started with Android development. I've created base classes for Application, Activity, Service and so on. Because of this I never need to manually define a TAG and it is always available and reflects the class name with the unique package.appname prefix. I even have users help me debug my app by using something like Catlog and filtering on the company name and emailing me the results. It ensures I only get information related to my apps and nothing else that may infringe user privacy for example.Riccio
One more thing, how do you filter the logcat on com.mycompany.myapp The logcat command wouldn't work for me when I type adb logcat com.mycompany.myapp:I *:S but works for adb logcat com.mycompany.myapp.FishActivity:I *:SOrelie
I don't use the adb command line, I use the DDMS perspective in Eclipse (available through Window -> Open Perspective -> Other). I specify a filter to match on com.mycompany.myapp.* and it works fine.Riccio
So, there is no way to get it working outside Eclipse? Actually I'm using this library ACRA to collect crash data when the application crashes so have to pass the parameters of logcat command there. adb logcat command seems to be not much powerful now :(Orelie
Did u try using a regular expression? Like this: adb logcat com.mycompany.myapp.*:I *:S The star at the end of myapp is a regular expression, including any characters. I haven't tried it myself but it might be worth giving it a shot.Northbound

© 2022 - 2024 — McMap. All rights reserved.