Vinod
Vinod

Reputation: 32861

Filter LogCat to get only the messages from My Application in Android?

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.

Upvotes: 501

Views: 626157

Answers (30)

shanet
shanet

Reputation: 7324

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

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>("<tag>", "message");

adb -d logcat <tag>:<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.

Upvotes: 328

LePain
LePain

Reputation: 1

The following method will obtain the PID first by running the pidof command using the shell. It requires providing the package name.

Replace com.myappp with your app's package name.

adb logcat | grep $(adb shell pidof com.myapp)

If it does not work, first try manually typing adb shell and see if you reliably get a shell. If not, there may be other issues. You can manually run pidof com.myapp when inside the adb shell and see that it indeed output a PID.

Note: On large logcat buffers and slow (e.g. Wi-Fi) connections, it may take a file for logcat to dump its entire history. You will notice this when typing just adb logcat; it may take a while to catch up. In that case, run adb logcat -c to clear the entire buffer.

Note: This assumes Linux/Unix. Use a VM when on Windows. It may work with Cygwin as well. You need a bash shell for this.

Note: Anything with the PID number will slip through grep, this will cause the occasional false-positive; e.g. a few unrelated log entries will slip through (if they happen to contain the PID number by chance)

Upvotes: 0

Manoj DB
Manoj DB

Reputation: 259

To store your ADB logs into a file with specific query text use below command for mac.

adb logcat pid=<pid> | grep 'add a text for filter' > file_name.txt
example : adb logcat pid=<pid> | grep 'user data' > android_adb.txt

Note : pid you can find in android studio adb logcat.

To store ADB logs (full log) without any filter

adb logcat > file_name.txt

Note : before expecting above two command connect your device using adb connect and then hit the above two command, logs will be writing into file at runtime until you stop the same.

Upvotes: 0

nobane
nobane

Reputation: 72

In linux, this worked for me:

adb logcat | grep `adb shell ps | grep your.package | awk '{print $2}'`

Upvotes: -4

Tom Mulcahy
Tom Mulcahy

Reputation: 5227

Note: The following answer is over 10 years old. It's probably not the best answer anymore. My current preferred way of accomplishing this is https://stackoverflow.com/a/76551835/1292598

Linux and OS X

Use ps/grep/cut to grab the PID, then grep for logcat entries with that PID. Here's the command I use:

adb logcat | grep -F "`adb shell ps | grep com.asanayoga.asanarebel | tr -s [:space:] ' ' | cut -d' ' -f2`"

(You could improve the regex further to avoid the theoretical problem of unrelated log lines containing the same number, but it's never been an issue for me)

This also works when matching multiple processes.

Windows

On Windows, to get full logs, you can do:

adb logcat | findstr com.example.package

Logcat logs has got levels at which to get info:

V — Verbose, D — Debug, I — Info, W — Warning, E — Error, F — Fatal, S — Silent

So to get only error logs related to the app, you can update the above command as follows:

adb logcat *:E | findstr com.example.package

Upvotes: 483

Tom Mulcahy
Tom Mulcahy

Reputation: 5227

For a debuggable application, I suggest

adb shell run-as my.package.name logcat

run-as doesn't work for non-debuggable applications, so for those I use the --uid flag. Unfortunately there isn't a uidof, so I need to extract it from pm. Here's a small sh script to do that:

function logcat {
  pkg="$1"
  shift
  if [ -z "$pkg" ]; then
    >&2 echo 'Usage: logcat pkg ...'
    return 1
  fi

  uid="$(adb shell pm list package -U $pkg | sed 's/.*uid://')"
  if [ -z "$uid" ]; then
    >&2 echo "pkg '$pkg' not found"
    return 1
  fi

  adb logcat --uid="$uid" "$@"
}

Usage is logcat my.package.name. It accepts additional arguments like normal logcat.

I prefer this to the --pidof based solution (see https://stackoverflow.com/a/48004086/1292598) since that requires you to re-run the command each time the process is restarted.

Upvotes: 19

digitalfootmark
digitalfootmark

Reputation: 744

adb logcat -e "package-name" 

This works prefectly when filtering rows for one app only.

Upvotes: 27

Lennoard Silva
Lennoard Silva

Reputation: 945

This works for me with USB debugging:

The solution was to use your device's own logcat directly via shell.

  1. Connect the device and use:

    adb shell

  2. Use logcat after the shell is set up:

    logcat | grep com.yourapp.packagename

Upvotes: 63

yoAlex5
yoAlex5

Reputation: 34175

LogCat Application messages

As a variant you can use third party script PID Cat by Jake Wharton. This script has two major advantages:

  • shows log entries for processes from a specific application package
  • color logcat

From documentation:

During application development you often want to only display log messages coming from your app. Unfortunately, because the process ID changes every time you deploy to the phone it becomes a challenge to grep for the right thing.

This script solves that problem by filtering by application package.

An output looks like enter image description here

Upvotes: 7

Eyni Kave
Eyni Kave

Reputation: 1520

Windows CMD

For sample, if your application package name is: com.nader.chat

cd C:\Users\[your-username]\AppData\Local\Android\Sdk\platform-tools
adb shell logcat *:E | findstr /c:"at com.nader.chat"

enter image description here

  • :E just filter Erros from logs, you can replace it with V: Verbose (lowest priority), D: Debug, I: Info, W: Warning, F: Fatal.
  • at to find errors just in your written source code not for others related modules

Upvotes: 0

Burak Day
Burak Day

Reputation: 959

Another way of getting logs of exact package name when you are inside the shell:

logcat --pid $(ps -ef | grep -E "com.example.app\$" | awk '{print $2}') 

Upvotes: 4

钟智强
钟智强

Reputation: 480

I have different approach, you can try access to local device's shell.

adb shell

and then follow by

logcat | grep com.package.name

This print all containing that package.

Alternatively, You can try flutter logs --verbose

Upvotes: 4

freezing_
freezing_

Reputation: 1044

I am usually adding something in the log messages to make it distinct. Or for example unity app you can use "Unity" as matching string.

For mac :

adb logcat | grep "MyUniqueString" 

for Windows (powershell ):

adb logcat | Select-String "MyUniqueString"

Upvotes: 5

Deepak Attri
Deepak Attri

Reputation: 132

In order to access the logcats you first need to install ADB command-line tool. ADB command-line tool is a part of android studio platform tools and can be downloaded from here. After this, you need to set the path/environment variable for adb tools. Now you can access logcat from eclipse terminal/ intellij terminal or mac terminal in case you are using a macbook.

adb logcat : To get entire logcat.

adb shell pidof 'com.example.debug' : To get the process id of your app.

adb logcat pid=<pid> : To get logcat specific to your app.

adb logcat pid=<pid>|grep 'sometext' : To filter logcat on basis of some text.

For more info about filtering logcats read this.

Upvotes: 8

Amol Desai
Amol Desai

Reputation: 927

You can use below command to fetch verbose logs for your application package

adb logcat com.example.myapp:V *:S

Also if you have rolled out your app and you want to fetch error logs from released app, you can use below command.

adb logcat AndroidRuntime:E *:S

Upvotes: 2

Alex Shevelev
Alex Shevelev

Reputation: 691

For windows, you can use my PowerShell script to show messages for your app only: https://github.com/AlShevelev/power_shell_logcat

Upvotes: 1

Sandeep PC
Sandeep PC

Reputation: 867

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

Upvotes: 13

You Kim
You Kim

Reputation: 2795

Since Android 7.0, logcat has --pid filter option, and pidof command is available, replace com.example.app to your package name.
(ubuntu terminal / Since Android 7.0)

adb logcat --pid=`adb shell pidof -s com.example.app`

or

adb logcat --pid=$(adb shell pidof -s com.example.app)

For more info about pidof command:
https://stackoverflow.com/a/15622698/7651532

Upvotes: 217

Lud Akell
Lud Akell

Reputation: 191

On Windows 10, using Ionic, what worked great to me was combine 'findstr' with the "INFO:CONSOLE" generated by all App messages. So, my command in command line is:

adb logcat | findstr INFO:CONSOLE

Upvotes: 3

Tom Ladek
Tom Ladek

Reputation: 807

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.

Upvotes: 1

Paschalis
Paschalis

Reputation: 12291

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.

Upvotes: 13

Lawyno
Lawyno

Reputation: 11

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%

Upvotes: 1

rareclass
rareclass

Reputation: 785

Using Windows command prompt: adb logcat -d | findstr <package>.

*This was first mentioned by jj_, but it took me ages to find it in the comments...

Upvotes: 8

KrisWebDev
KrisWebDev

Reputation: 9492

Use -s !

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.

Source

Upvotes: 7

ls.illarionov
ls.illarionov

Reputation: 741

Yet another variant of Gavriel's applog.sh with support of several devices and applications with multiple processes:

#!/bin/sh
PKG=$1
shift

APPIDS=`adb $@ shell ps | awk -v PKG="$PKG" '
    (NR == 1){appcolumn=2; for (i=1; i<=NF; i++) if ($i=="PID") {appcolumn=i}}
    index($0,PKG){print $(appcolumn)}' | paste -d \| -s`

echo "PID's: $APPIDS"
adb $@ logcat -v color | awk  "(\$3 ~ /$APPIDS/){print \$0}"

Usage: applog.sh com.example.my.package [-s <specific device>]

Upvotes: 0

Frank Du
Frank Du

Reputation: 516

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

Upvotes: 2

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

I tried to use Tom Mulcahy's answer but unfortunately it was not working for applications with multiple processes so I edit it to fit my needs.

#!/bin/bash
if [ "$#" -ne 1 ]; then echo "Illegal number of parameters"; exit 1; fi
echo "Lof for package name: $1"
PROCESSES=`adb shell ps | grep "$1" | cut -c10-15`
NUM_OF_PROCESSES=`echo "$PROCESSES" | wc -l`
if [ $NUM_OF_PROCESSES -eq 0 ]; then echo "The application is not running!"; exit 1; fi
COUNTER=1
for process in $PROCESSES; do
        if [ $COUNTER -eq 1 ]; then GREP_TEXT="("; fi
        GREP_TEXT+=$process
        if [ $COUNTER -eq $NUM_OF_PROCESSES ]; then GREP_TEXT+=")"; else GREP_TEXT+="|"; fi
        let COUNTER=COUNTER+1 
        if [ $COUNTER -gt $NUM_OF_PROCESSES ]; then break; fi  
done
adb logcat | grep -E "$GREP_TEXT"

Upvotes: 1

Gene
Gene

Reputation: 11267

Give your log a name. I called mine "wawa".

enter image description here

In Android Studio, go to Android-> Edit Filter Configurations

enter image description here

Then type in the name you gave the logs. In my case, it's called "wawa". Here are some examples of the types of filters you can do. You can filter by System.out, System.err, Logs, or package names:

enter image description here enter image description here enter image description here

Upvotes: 2

Gavriel
Gavriel

Reputation: 19237

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

Upvotes: 9

Marcin Mikołajczyk
Marcin Mikołajczyk

Reputation: 731

In intelliJ (and probably in eclipse also) you can filter the logcat output by text webview, so it prints basically everything phonegap is producing

Upvotes: 0

Related Questions