Extracting Logs With logcat — Reading Device Logs
This skill covers
end-to-end: streaming vs dumping, buffer selection, filter expressions, format flags, PID and time filters, file rotation, and the R8 rule that strips
from release builds. The companion CI capture-on-failure pattern lives in
../../automation/scripting-adb-for-ci/SKILL.md
.
When to use this skill
- A test fails on CI; the developer needs to dump the device log into the artefact archive.
- The developer's is overwhelming the SSH session — the logcat-side filter is the fix.
- A scenario only reproduces inside or buffers, not the default .
- The developer wants structured logs () for machine ingestion.
- A
Log.d("Sensitive", "...")
call appears to leak in release — the R8 rule is missing.
- Logs disappear after the app is killed by Doze/ANR — plus a re-resolution loop is needed.
When NOT to use this skill
- Capturing a screenshot or video. Use
../../capture/capturing-screenshots-and-screenrecord/SKILL.md
.
- Pulling a generic file from the device. Use
../../transfer/extracting-test-artifacts/SKILL.md
.
- The whole script — retries, port forwarding, parallel device fan-out. Use
../../automation/scripting-adb-for-ci/SKILL.md
.
- Driving gestures or settings changes. Use
../../control/injecting-input-and-state/SKILL.md
.
Prerequisites
- returns . See
../../devices/connecting-to-devices/SKILL.md
.
- For : device API 30+ (Android 11+). Stack with for unambiguous timestamps.
- For per-PID filtering: requires API 24+ (toybox ); on older releases use
adb shell ps -A | grep <pkg>
.
Three fundamental operations
| Command | Behavior |
|---|
| Stream the device log to stdout; runs until interrupted. |
| "Dumps the log and exits." Snapshot mode — perfect for CI. |
| "Clears the log buffer." Run this before reproducing a bug. |
(Verbatim quotes from developer.android.com/tools/logcat.)
Idiomatic CI capture pattern:
bash
adb logcat -c # clear before scenario
./run-scenario.sh # reproduce
adb logcat -d > artifacts/log.txt # dump after
Buffer selection
selects which kernel/userspace ring buffer is read. Buffers (developer.android.com/tools/logcat):
| Buffer | What it contains |
|---|
| Default app-side buffer. Does NOT contain system/crash. |
| Framework / system_server messages. |
| Tombstones + unhandled-exception output. |
| Structured/binary system event buffer. Pair with to decode tag names. |
| Radio/telephony related messages. |
| Kernel buffer. |
| Every buffer. |
| Implicit set: , , . |
Multiple
flags or comma-separated lists both work:
bash
adb logcat -b radio
adb logcat -b main -b radio -b events
adb logcat -b main,radio,events
The
buffer is where to look for
,
,
,
— emitted by the framework for instrumentation, not for human reading. Decode tag names with:
bash
adb logcat -b events -v descriptive
Priority ladder + filter expressions
Filter specs are space-separated
pairs.
matches every tag.
| Letter | Meaning |
|---|
| Verbose |
| Debug |
| Info |
| Warning |
| Error |
| Fatal |
| "Silent (highest priority, nothing is printed)" |
Setting
silences everything, then any preceding
re-enables that tag at priority
or above. The canonical "show only my app's logs" idiom:
bash
adb logcat ActivityManager:I MyApp:D *:S
"Suppress all logs except ActivityManager (Info+) and MyApp (Debug+)" — developer.android.com/tools/logcat.
Other staples:
bash
adb logcat *:W # warnings and above, all tags
adb logcat *:E # errors and above (very common in CI)
zsh/bash will glob-expand
outside of quotes when there is a file named
in cwd — quote when scripting:
bash
adb logcat "ActivityManager:I MyApp:D *:S"
The same filter can be the host default via env var:
bash
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
Format flags —
| Format | What you get |
|---|
| "Displays priority, tag, and PID" |
| "All metadata fields with blank lines between messages" |
| "PID only" |
| "Raw log message with no metadata" |
| "Priority and tag only" |
| "Legacy format showing priority, PID, and TID" |
| DEFAULT. "Date, time, priority, tag, PID, and TID" |
| "Date, time, priority, tag, and PID" |
Format
modifiers stack with
(comma-combinable or repeatable):
| Modifier | Effect |
|---|
| Per-priority colour. |
| Decode event log tag names. |
| Time in seconds since 1970-01-01. |
| CPU seconds from last boot. |
| Escape binary content. |
| UID or Android ID of logged process. |
| Time with microsecond precision. |
| Time as UTC. |
| Add year to displayed time. |
| Add local time zone. |
bash
adb logcat -v json -v UTC -v year # Android 11+; structured stream, unambiguous time
adb logcat -b all -v color -d # color dump, all buffers
Default-line example (developer.android.com/tools/logcat):
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action.MAIN ... }
Schema for
:
<priority>/<tag>(<PID>): <message>
.
adds date/time/TID.
Time and PID filters
- — start at the first entry on or after the given timestamp. Format matches , e.g. .
- — print the last N entries from the buffer, then continue streaming.
- — print the last N entries, then exit (i.e. is with auto-quit).
- — emit only entries from a single process. Combine with :
bash
adb logcat --pid=$(adb shell pidof -s com.example.app)
returns a single pid; without
you get a space-separated list which
will not accept directly. Wrap a re-resolution loop if the app may be killed and restarted (Doze, ANR, crash):
bash
while true; do
PID=$(adb shell pidof -s com.example.app 2>/dev/null)
[ -n "$PID" ] && adb logcat --pid="$PID"
sleep 1
done
Buffer rotation and persistence
For long captures, persist to a file with rotation:
| Flag | Meaning |
|---|
| Write to instead of stdout. Subtle: with , the file is host-side; with , the path is device-side. |
| Rotate output every N kilobytes. Requires . |
| Keep at most rotated files (, , ...). |
| Resize the kernel log buffer for the selected ring (e.g. , ). Persists for the life of . |
| Print buffer sizes and current usages, then exit. Cheap diagnostics for "why am I missing logs?". |
bash
adb logcat -G 16M # grow the main buffer
adb logcat -g # inspect sizes
adb logcat -b main -f /data/local/tmp/app.log \
-r 1024 -n 8 # 8x ~1MB rotated files (device-side)
adb shell logcat -b main -f /data/local/tmp/app.log \
-r 1024 -n 8 # same, but the device runs it
The
resize is global to that buffer and persists across
restarts (until reboot). Don't set 16 MB on a memory-constrained device and forget — release memory back with
after the suite if your CI farm is shared.
Stripping in release — R8 rule
calls remain in release APKs unless code-shrinking removes them. The standard idiom in
(R8/ProGuard):
pro
-assumenosideeffects class android.util.Log {
public static *** v(...);
public static *** d(...);
public static *** i(...);
public static boolean isLoggable(java.lang.String, int);
}
lets R8 treat the listed methods as pure no-ops, so all calls (and their string-builder argument expressions, when constants) are eliminated. Keep
,
,
for production diagnostics. With Timber: strip the
-gated
plant and rely on a
that forwards to Crashlytics for warnings/errors.
For Compose specifically, do
not kotlin.jvm.internal.Intrinsics
— it removes null-checks the compiler relies on.
Patterns
Pattern: WRONG — for filtering
bash
# WRONG
adb logcat | grep MyApp
# WRONG because: the device sends every log line over the USB/TCP transport, then the host
# discards 99% via grep. On a busy device this saturates the transport and the grep can lag
# behind real time. Worse, `grep` runs without -F, so a tag that contains regex metacharacters
# (`.`, `(`, `+`) silently mismatches.
bash
# RIGHT — filter at the logcat level
adb logcat MyApp:D *:S
The device only emits matching lines; the transport is uncongested.
Pattern: WRONG — assuming exits
bash
# WRONG
adb logcat -T 100 > tail.txt
# Hangs forever — -T <count> prints the last 100 lines THEN CONTINUES STREAMING.
bash
# RIGHT — `-t` is `-T` with auto-exit
adb logcat -t 100 > tail.txt
Pattern: structured logs for machine ingestion
bash
adb logcat -v json -v UTC -v year -d -b all > run.jsonl
Each line is a single JSON object — pipe through
:
bash
jq -c 'select(.priority == "ERROR")' run.jsonl
(
requires Android 11+ on the device.)
Pattern: capture-on-failure — clear, run, dump
bash
adb logcat -c
./run-scenario.sh
RC=$?
if [ "$RC" -ne 0 ]; then
adb logcat -d -v threadtime > artifacts/logcat.txt
adb logcat -d -b crash -v threadtime > artifacts/crash.txt
adb logcat -d -b events -v descriptive > artifacts/events.txt
fi
exit "$RC"
clears before the run so the dump captures only the scenario's logs.
Pattern: PID-bound stream that survives a process restart
bash
while true; do
PID=$(adb shell pidof -s com.example.app 2>/dev/null)
if [ -n "$PID" ]; then
adb logcat --pid="$PID" -v threadtime
fi
sleep 1
done
The outer loop re-resolves the PID after Doze/ANR/crash kills and restarts the app.
Pattern: host vs device side
bash
# host-side rotation (file lives on the developer machine)
adb logcat -b main -f ~/logs/app.log -r 1024 -n 8
# device-side rotation (file lives in /data/local/tmp on the device)
adb shell logcat -b main -f /data/local/tmp/app.log -r 1024 -n 8
adb pull /data/local/tmp/app.log ~/logs/app.log # later
Mandatory rules
- MUST filter at the logcat level (e.g. ), not via host-side . Filtering on the device avoids saturating the transport.
- MUST quote filter strings containing in zsh/bash (e.g. ) — globbing eats the asterisk if a file named exists in cwd.
- MUST use for "tail and exit". continues streaming after the initial dump.
- MUST use for snapshot dumps in CI. Streaming requires explicit termination.
- MUST call before reproducing a bug whose log dump you want to attach. Otherwise the dump includes irrelevant prior history.
- MUST distinguish (host-side path) from
adb shell logcat -f <file>
(device-side path). The shell forms diverge.
- MUST add the R8 rule for // (and ) when shipping release APKs that previously called with sensitive data. Keep //.
- MUST NOT
kotlin.jvm.internal.Intrinsics
— that removes null-check semantics the Kotlin compiler relies on.
- MUST NOT rely on on devices below Android 11 — silently emits an empty stream.
- PREFERRED: stack (or
-v threadtime -v year -v UTC
) for unambiguous absolute timestamps in CI artefacts.
- PREFERRED: run with for the buffer when capturing slow-burning bugs, and reset to default in teardown.
Verification
References
- developer.android.com/tools/logcat — buffer table, priority ladder, filter expressions, //, format modifiers, ////// flags, line-format schema.
- developer.android.com/tools/adb — host-side shorthand vs .
- developer.android.com/studio/build/shrink-code — R8 and the strip rule.
- developer.android.com/reference/android/util/Log — ////// signatures.
- Research note
tasks/research/A3-adb-observability-automation.md
— full logcat surface, host-vs-device subtlety, R8 keep-rule rationale, capture-on-failure CI pattern.
- Sibling skill:
../../architecture/understanding-adb-architecture/SKILL.md
— server / daemon, transport.
- Sibling skill:
../../devices/connecting-to-devices/SKILL.md
— device states, .
- Sibling skill:
../../devices/connecting-over-wifi/SKILL.md
— / .
- Sibling skill:
../../apps/installing-and-managing-apps/SKILL.md
— installing the APK whose logs are being captured.
- Sibling skill:
../../tests/running-instrumented-tests-via-adb/SKILL.md
— interplay with logcat.
- Sibling skill:
../../control/injecting-input-and-state/SKILL.md
— driving gestures whose effects show up in logcat.
- Sibling skill:
../../capture/capturing-screenshots-and-screenrecord/SKILL.md
— companion screenshot capture.
- Sibling skill:
../../transfer/extracting-test-artifacts/SKILL.md
— pulling rotated log files from the device.
- Sibling skill:
../../automation/scripting-adb-for-ci/SKILL.md
— full CI capture-on-failure / retry / cleanup patterns.
- Cross-set:
../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md
— instrumentation logs interleaved with logcat.
- Cross-set:
../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md
— Activity lifecycle events surfaced in buffer.
- Cross-set:
../../../fundamentals/strategies/applying-testing-strategies/SKILL.md
— when log capture is the appropriate observability layer.