How to Filter an App’s Error Logs Using adb
To filter for Error (E) level logs specifically from a particular Android application using adb logcat
, you can combine various filtering options. Here are the most effective methods:
Method 1: Filter by Process ID (PID) - Recommended
This is the most precise method as logs are directly associated with the application’s running process.
Get your app’s PID:
First, find the Process ID (PID) of your running application. Open a new command line window and type:
Bash
adb shell pidof -s your.package.name
Replace
your.package.name
with your app’s actual package name (e.g.,com.example.myapp
). This command will return the app’s PID, for instance,12345
.Filter using PID and Error Level:
Once you have the PID, run the logcat command in another command line window, combining the —pid argument with the
level filter: Bash
adb logcat --pid=12345 *:E
Or, more concisely, you can nest the
pidof
command:Bash
adb logcat --pid=$(adb shell pidof -s your.package.name) *:E
(Note: In some shells, you might need to use backticks instead of
$(...)
, e.g.,adb logcat --pid=\
adb shell pidof -s your.package.name*:E
)--pid=12345
: Displays logs only from the process with PID 12345.*:E
: Shows logs of Error (E) level or higher (Fatal, Assert) from all tags.
Method 2: Filter by Log Tag and Error Level
If your application consistently uses a specific tag when printing logs, you can filter by that tag.
Identify your app’s log tag:
In your app’s code, the TAG in Log.e(TAG, ”…”) is what you’ll use for filtering. For example:
Java
// Java private static final String TAG = "MyAppError"; Log.e(TAG, "Something went wrong!");
Kotlin
// Kotlin const val TAG = "MyAppError" Log.e(TAG, "Something went wrong!")
Filter using the tag and error level:
Bash
adb logcat YourAppTag:E *:S
YourAppTag:E
: Displays logs only with the tagYourAppTag
and of Error (E) level or higher.*:S
: This is crucial. It means “Silence all other tags.” Without this,logcat
would still display error logs from other processes. Adding*:S
ensures that only the error logs for your specifiedYourAppTag
are shown.
Method 3: Filter by Package Name using grep
(Less Accurate)
This method pipes the logcat
output through the grep
command to perform text matching. While generally useful, it might not catch all relevant logs, as some system errors related to your app might not explicitly contain your package name in the log message.
Know your app’s package name:
For example: com.example.myapp
Filter using
grep
:Bash
adb logcat *:E | grep "com.example.myapp"
*:E
: First,logcat
outputs only error-level logs.| grep "com.example.myapp"
: Then,grep
further filters these error logs to show only lines that contain your application’s package name.
Which Method Is Best?
- Method 1 (PID filtering): This is the most recommended and accurate approach. It directly targets your app’s process, effectively excluding irrelevant logs from other processes.
- Method 2 (Tag filtering): If your app has a well-managed and consistent logging tag strategy, this method is very effective. The use of
*:S
is key here. - Method 3 (Grep filtering): This is simple and often good enough for quick checks, but it’s less precise because it relies on text matching and might miss some relevant logs that don’t explicitly contain the package name.
Choose the method that best suits your current needs for debugging and logging analysis.