adb logcat: Capture Device Logs in CI
adb logcat prints the device log buffer, with filters by tag and priority so you can pull just the crash or your app output.
When an instrumentation test fails on a headless emulator, the logcat dump is the only window into what happened. Capture it as an artifact so a red build is debuggable without re-running.
What it does
adb logcat reads the circular log buffers on the device and writes them to stdout. Filter expressions take the form Tag:Priority (for example ActivityManager:I) with a trailing *:S to silence everything else. -d dumps and exits; -c clears the buffer first.
Common usage
adb logcat -c # clear before the test
adb logcat -d > logcat.txt # dump and exit (for artifacts)
adb logcat "*:E" # only errors
adb logcat --pid=$(adb shell pidof -s com.example.app)Options
| Flag | What it does |
|---|---|
| -d | Dump the current buffer and exit (do not stream) |
| -c | Clear (flush) the log buffers |
| -v <format> | Output format: brief, time, threadtime, etc. |
| --pid=<pid> | Only logs from a specific process |
| -b <buffer> | Select a buffer: main, system, crash, events, all |
| *:E / Tag:D | Filter by priority (V D I W E F) and tag |
In CI
Clear the buffer with -c right before the test, then on failure run adb logcat -d -b all > logcat.txt and upload it as an artifact. For long UI suites, stream adb logcat > logcat.txt & in the background and kill it in the always() teardown so you capture the crash even on timeout.
Common errors in CI
"- waiting for device -" (printed by logcat) means no device is up yet; run adb wait-for-device first. An empty dump usually means -c was run after the failure, not before. "read: unexpected EOF" or a hang appears if the emulator dies mid-stream; guard the background logcat with the job teardown so it does not block the runner.