LinuxCommandLibrary

logcat

View Android system logs

TLDR

Display system logs

$ logcat
copy

Write system logs to a file

$ logcat -f [path/to/file]
copy

Display lines that match a regex

$ logcat --regex [regex]
copy

SYNOPSIS

adb logcat [options] [filterspecs]

PARAMETERS

-s
    Silent mode, suppress all messages except for specified tags.

-d
    Dump the entire log buffer and exit.

-c
    Clear the entire default log buffer and exit.

-C
    Clear the specified log buffer and exit.

-v
    Set the log output format (e.g., brief, process, tag, thread, raw, time, long).

-b
    Select an alternate log buffer (e.g., main, system, radio, events, crash, kernel, security).

-f
    Write log output to a specified file instead of stdout.

-r
    Rotate log files every specified kilobyte size.

-n
    Limit the number of rotated log files to a specific count.

-L
    Print only the last specified number of kilobytes of log data from the buffer.

-P
    Print only messages from the specified process ID (PID).

-g
    Print the log buffer to stdout and exit (similar to -d).

-t
    Specify the time format for log entries (e.g., UTC, local, epoch).

-D
    Display current date/time on output.

-e
    Filter out log messages matching a specified regular expression.

-A
    Assert. Display assert messages (fatal errors).

DESCRIPTION


logcat is a command-line tool integral to the Android Debug Bridge (ADB) suite, primarily used by developers and system administrators to view system messages and logs from an Android device or emulator. It captures and displays a continuous stream of messages produced by the device's operating system, applications, and services. These logs are essential for debugging, monitoring application behavior, identifying errors, and understanding system events.

logcat allows users to filter log output based on various criteria, such as tag, priority, and process ID, enabling focused analysis of specific log streams. It interacts with different log buffers (e.g., main, system, radio, events, crash, kernel), each storing different types of messages. Without logcat, debugging and troubleshooting on Android devices would be significantly more challenging. It is typically invoked through adb shell logcat or directly as adb logcat from a Linux terminal connected to an Android device.

CAVEATS

logcat is primarily designed for Android device logging and requires an Android device or emulator connected via ADB (Android Debug Bridge). It is not a general-purpose Linux logging tool like dmesg or journalctl.

The raw output can be extremely verbose; effective filtering using filterspecs is crucial for practical use.

Access to certain log buffers or functionalities might require specific Android permissions or root access on the device.

Log retention is limited; older logs are overwritten as new ones come in, unless specifically captured or rotated.

UNDERSTANDING FILTERSPECS

logcat allows sophisticated filtering using filterspecs. A filterspec is in the format TAG:PRIORITY, where TAG is the source component (e.g., "ActivityManager", "MyApp") and PRIORITY is the minimum priority level to display. Multiple filterspecs can be provided, separated by spaces. For example, adb logcat ActivityManager:I MyApp:D *:S would show "ActivityManager" messages at Info level or higher, "MyApp" messages at Debug level or higher, and suppress all other tags.

LOG PRIORITIES

Android log messages are assigned a priority level, from lowest to highest severity:
V: Verbose (lowest priority, typically for debug messages)
D: Debug (debug messages)
I: Info (informational messages)
W: Warn (warnings)
E: Error (errors)
F: Fatal (fatal errors, highest priority)
S: Silent (used for suppressing all messages, e.g., in filterspecs)

LOG BUFFERS

Android maintains several distinct log buffers, each storing different types of messages:
main: The default buffer for most application and system component logs.
system: Logs from the Android system server and system services.
radio: Messages related to the radio and telephony stack.
events: Binary buffer for system events (requires decoding).
crash: Logs specifically related to crashes (ANRs, native crashes).
kernel: Kernel messages (similar to dmesg output on standard Linux).
security: Security-related events.
You can select a specific buffer using the -b option (e.g., adb logcat -b radio).

HISTORY

logcat has been an integral part of the Android SDK and platform since its early versions, evolving alongside the Android operating system. Its development has focused on enhancing filtering capabilities, improving performance for large log streams, and adding support for new log buffers and formats as the complexity of Android devices and applications grew. It remains a foundational tool for debugging and system analysis in the Android ecosystem.

SEE ALSO

adb(1): The Android Debug Bridge, essential for connecting to and interacting with Android devices., dmesg(1): Displays kernel ring buffer messages on Linux systems (general Linux logging)., journalctl(1): Query the systemd journal (general Linux logging).

Copied to clipboard