[android] How to filter Android logcat by application?

How can I filter Android logcat output by application? I need this because when I attach a device, I can't find the output I want due to spam from other processes.

This question is related to android logcat

The answer is


In Android Studio in the Android Monitor window: 1. Select the application you want to filter 2. Select "Show only selected application"

enter image description here


Generally, I do this command "adb shell ps" in prompt (allows to see processes running) and it's possible to discover aplication's pid. With this pid in hands, go to Eclipse and write pid:XXXX (XXXX is the application pid) then logs output is filtered by this application.

Or, in a easier way... in logcat view on Eclipse, search for any word related with your desired application, discover the pid, and then do a filter by pid "pid:XXXX".


On Linux/Un*X/Cygwin you can get list of all tags in project (with appended :V after each) with this command (split because readability):

$ git grep 'String\s\+TAG\s*=\s*' |  \
  perl -ne 's/.*String\s+TAG\s*=\s*"?([^".]+).*;.*/$1:V/g && print ' | \
  sort | xargs
AccelerometerListener:V ADNList:V Ashared:V AudioDialog:V BitmapUtils:V # ...

It covers tags defined both ways of defining tags:

private static final String TAG = "AudioDialog";
private static final String TAG = SipProfileDb.class.getSimpleName();

And then just use it for adb logcat.


The log cat output can be filtered to only display messages from your package by using these arguments.

adb com.your.package:I *:s

Edit - I spoke to soon.

adb com.your.package:v

use first parameter as your application name. Log.d("your_Application_Name","message");

and in LogCat : create Filter ---> Filter Name & by Log Tag: is equal to 'your_Application_Name' it will create new tab for your application.


Yes now you will get it automatically....
Update to AVD 14, where the logcat will automatic session filter
where it filter log in you specific app (package)


I have found an app on the store which can show the name / process of a log. Since Android Studio just puts a (?) on the logs being generated by the other processes, I found it useful to know which process is generating this log. But still this app is missing the filter by the process name. You can find it here.


Hi I got the solution by using this :

You have to execute this command from terminal. I got the result,

adb logcat | grep `adb shell ps | grep com.package | cut -c10-15`

What I usually do is have a separate filter by PID which would be the equivalent of the current session. But of course it changes every time you run the application. Not good, but it's the only way the have all the info about the app regardless of the log tag.


my .bash_profile function, it may be of any use

logcat() {
    if [ -z "$1" ]
    then
        echo "Process Id argument missing."; return
    fi
    pidFilter="\b$1\b"
    pid=$(adb shell ps | egrep $pidFilter | cut -c10-15)
    if [ -z "$pid" ]
    then
        echo "Process $1 is not running."; return
    fi
    adb logcat | grep $pid
}

alias logcat-myapp="logcat com.sample.myapp"

Usage:

$ logcat-myapp

$ logcat com.android.something.app


I am working on Android Studio, there is a nice option to get the message using package name. On the "Edit Filter Configuration" you can create a new filter by adding your package name on the "by package name".

enter image description here


When we get some error from our application, Logcat will show session filter automatically. We can create session filter by self. Just add a new logcat filter, fill the filter name form. Then fill the by application name with your application package. (for example : my application is "Adukan" and the package is "com.adukan", so I fill by application name with application package "com.adukan")


On my Windows 7 laptop, I use 'adb logcat | find "com.example.name"' to filter the system program related logcat output from the rest. The output from the logcat program is piped into the find command. Every line that contains 'com.example.name' is output to the window. The double quotes are part of the find command.

To include the output from my Log commands, I use the package name, here "com.example.name", as part of the first parameter in my Log commands like this:

Log.d("com.example.name activity1", "message");

Note: My Samsung Galaxy phone puts out a lot less program related output than the Level 17 emulator.


According to http://developer.android.com/tools/debugging/debugging-log.html:

Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at priority "Info" or above, and all log messages with tag "MyApp", with priority "Debug" or above:

adb logcat ActivityManager:I MyApp:D *:S

The final element in the above expression, *:S, sets the priority level for all tags to "silent", thus ensuring only log messages with "View" and "MyApp" are displayed.

  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)

Use fully qualified class names for your log tags:

public class MyActivity extends Activity {
  private static final String TAG = MyActivity.class.getName();
}

Then

Log.i(TAG, "hi");

Then use grep

adb logcat | grep com.myapp

On the left in the logcat view you have the "Saved Filters" windows. Here you can add a new logcat filter by Application Name (for example, com.your.package)


to filter the logs on command line use the below script

adb logcat com.yourpackage:v


If you could live with the fact that you log are coming from an extra terminal window, I could recommend pidcat (Take only the package name and tracks PID changes.)


The Android Device Monitor application available under sdk/tools/monitor has a logcat option to filter 'by Application Name' where you enter the application package name.


you can achieve this in Eclipse logcat by entering the following to the search field.

app:com.example.myapp

com.example.myapp is the application package name.


I use to store it in a file:

        int pid = android.os.Process.myPid();
        File outputFile = new File(Environment.getExternalStorageDirectory() + "/logs/logcat.txt");
        try {
            String command = "logcat | grep " + pid + " > " + outputFile.getAbsolutePath();
            Process p =  Runtime.getRuntime().exec("su");
            OutputStream os = p.getOutputStream();
            os.write((command + "\n").getBytes("ASCII"));
        } catch (IOException e) {
            e.printStackTrace();
        }

put this to applog.sh

#!/bin/sh
PACKAGE=$1
APPPID=`adb -d shell ps | grep "${PACKAGE}" | cut -c10-15 | sed -e 's/ //g'`
adb -d logcat -v long \
 | tr -d '\r' | sed -e '/^\[.*\]/ {N; s/\n/ /}' | grep -v '^$' \
 | grep " ${APPPID}:"

then: applog.sh com.example.my.package


Add your application's package in "Filter Name" by clicking on "+" button on left top corner in logcat.


This is probably the simplest solution.

On top of a solution from Tom Mulcahy, you can further simplify it like below:

alias logcat="adb logcat | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"

Usage is easy as normal alias. Just type the command in your shell:

logcat

The alias setup makes it handy. And the regex makes it robust for multi-process apps, assuming you care about the main process only.

Of coz you can set more aliases for each process as you please. Or use hegazy's solution. :)

In addition, if you want to set logging levels, it is

alias logcat-w="adb logcat *:W | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"

If you use Eclipse you are able to filter by application just like it is possible with Android Studio as presented by shadmazumder.

Just go to logcat, click on Display Saved Filters view, then add new logcat filter. It will appear the following:

enter image description here

Then you add a name to the filter and, at by application name you specify the package of your application.


Suppose your application named MyApp contains the following components.

  • MyActivity1
  • MyActivity2
  • MyActivity3
  • MyService

In order to filter the logging output from your application MyApp using logcat you would type the following.

adb logcat MyActivity1:v MyActivity2:v MyActivity3:v MyService:v *:s

However this requires you to know the TAG names for all of the components in your application rather than filtering using the application name MyApp. See logcat for specifics.

One solution to allow filtering at the application level would be to add a prefix to each of your unique TAG's.

  • MyAppActivity1
  • MyAppActivity2
  • MyAppActivity3
  • MyAppService

Now a wild card filter on the logcat output can be performed using the TAG prefix.

adb logcat | grep MyApp

The result will be the output from the entire application.