How to filter Android logcat by application?

AndroidLogcat

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Edit: The original is below. When one Android Studio didn't exist. But if you want to filter on your entire application I would use pidcat for terminal viewing or Android Studio. Using pidcat instead of logcat then the tags don't need to be the application. You can just call it with pidcat com.your.application

You should use your own tag, look at: http://developer.android.com/reference/android/util/Log.html

Like.

Log.d("AlexeysActivity","what you want to log");

And then when you want to read the log use>

adb logcat -s AlexeysActivity 

That filters out everything that doesn't use the same tag.

Solution 2 - Android

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)

Solution 3 - Android

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`

Solution 4 - Android

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

Solution 5 - Android

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.)

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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")

Solution 9 - Android

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.

Solution 10 - Android

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.

Solution 11 - Android

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();
        }

Solution 12 - Android

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`"

Solution 13 - Android

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)

Solution 14 - Android

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)

Solution 15 - Android

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.

Solution 16 - Android

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".

Solution 17 - Android

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.

Solution 18 - Android

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

Solution 19 - Android

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

Solution 20 - Android

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

Solution 21 - Android

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.

Solution 22 - Android

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.

Solution 23 - Android

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.

Solution 24 - Android

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.

Solution 25 - Android

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

Solution 26 - Android

to filter the logs on command line use the below script

adb logcat com.yourpackage:v

Solution 27 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexey ZakharovView Question on Stackoverflow
Solution 1 - AndroidDavid OlssonView Answer on Stackoverflow
Solution 2 - AndroidDominik SchürmannView Answer on Stackoverflow
Solution 3 - AndroidShanmugapriyanView Answer on Stackoverflow
Solution 4 - AndroidShadView Answer on Stackoverflow
Solution 5 - AndroidTheHippoView Answer on Stackoverflow
Solution 6 - AndroidStephen DoneckerView Answer on Stackoverflow
Solution 7 - AndroidGavrielView Answer on Stackoverflow
Solution 8 - AndroidsigejeView Answer on Stackoverflow
Solution 9 - AndroidCiro CostaView Answer on Stackoverflow
Solution 10 - AndroidearlcasperView Answer on Stackoverflow
Solution 11 - AndroidSpyZipView Answer on Stackoverflow
Solution 12 - AndroidFrank DuView Answer on Stackoverflow
Solution 13 - AndroidLabeeb PanampullanView Answer on Stackoverflow
Solution 14 - Androidjane1012View Answer on Stackoverflow
Solution 15 - AndroidAndroidersonView Answer on Stackoverflow
Solution 16 - AndroidDaniCView Answer on Stackoverflow
Solution 17 - AndroidLilylakshiView Answer on Stackoverflow
Solution 18 - Androiduser1817787View Answer on Stackoverflow
Solution 19 - AndroidDJTanoView Answer on Stackoverflow
Solution 20 - AndroidNick SnyderView Answer on Stackoverflow
Solution 21 - AndroidalexyView Answer on Stackoverflow
Solution 22 - AndroidpevikView Answer on Stackoverflow
Solution 23 - AndroidSanjeet AView Answer on Stackoverflow
Solution 24 - AndroidHiren DabhiView Answer on Stackoverflow
Solution 25 - Androiduser4054189View Answer on Stackoverflow
Solution 26 - AndroidBijeshView Answer on Stackoverflow
Solution 27 - AndroidbsquaredView Answer on Stackoverflow