Android adb logcat capture log

Logcat command-line tool

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws errors, and messages written from your app using the Log class.

command line syntax

To run Logcat through the adb shell, the general usage is as follows:

[adb] logcat [] … [] …

You can run logcat as an adb command, or directly at a shell prompt on the emulator or associated device. To view log output using adb, go to your SDK platform-tools/ directory and execute the following command:

adb logcat

To get logcat online help, start the device, then execute the following command: adb logcat --help

You can establish a shell connection to the device and execute the following command: adb shelllogcat

options

The command line options for logcat are described below

-b : Load an alternate log buffer for viewing, such as events or radio. By default the main, system and crash buffer sets are used. See Viewing Alternate Log Buffers.

-c, --clear : Clear (empty) the selected buffer and exit. The default buffer sets are main, system and crash. To clear all buffers, use -b all -c.

-e , --regex= : Only output log messages matching lines where is a regular expression.

-m , --max-count= : exit after outputting lines. This is meant to be paired with --regex, but can be run independently.

--print : Paired with --regex and --max-count, makes content bypass the regex filter, but still be able to stop when it gets the appropriate number of matches.

-d : Dump logs to screen and exit.

-f : Write log message output to. The default is stdout.

-g, --buffer-size : output the size of the specified log buffer and exit

-n : Set the maximum number of rotated logs to . The default value is 4. Requires the -r option.

-r : Rotate the log file every output. The default value is 16. The -f option needs to be used.

-s : Equivalent to the filter expression '*:S'; it prioritizes all flags to 'silent' and is used to precede the list of filter expressions that can add content. To learn more, go to the section on filtering log output.

-v : Set the output format of log messages. The default format is threadtime. For a list of supported formats, see the section on Controlling Log Output Formats.

-D, --dividers : Output dividers between individual log buffers.

-c : Empty (clear) the entire log and exit.

-t : Only output the latest line number. This option includes the -d functionality.

filter log output

1. The tag of a log message is a short string indicating the system component from which the message originated (for example, "View" for the view system).

2. Priority is one of the following character values ​​(in order from lowest to highest priority):

V: verbose (lowest priority)

D: debug

I: Information

W: warning

E: error

F: fatal error

S: Silent (highest priority, never output anything)

By running Logcat and observing the first two columns of each message, you can get a list of the prioritized tokens used in the system, formatted as /.

The following is an example of a brief Logcat output obtained using the logcat -v brief output command. It shows that the message is related to priority "I" and tag "ActivityManager":

I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action…}

To reduce log output to a manageable level, you can limit log output using filter expressions.

Filter expressions allow you to indicate to the system which tag/priority combinations you are interested in, and the system suppresses other messages for the specified tags.

Filter expressions take the form tag:priority... where tag indicates the tag you are interested in and priority indicates the lowest priority that can be reported against that tag. Messages tagged with a priority no lower than the one specified are written to the log.

You can provide any number of tag:priority specifications in a filter expression. A list of specifications is separated by spaces.

The following is an example of a filter expression that suppresses log messages except for those tagged with "ActivityManager" with a priority of "INFO" or higher, and with the tag "MyApp" with a higher priority of "Debug" All log messages other than log messages. adb logcat ActivityManager:I MyApp:D *:S

The last element *:S in the above expression sets the priority of all tags to "silent", thus ensuring that only log messages tagged with "ActivityManager" and "MyApp" are displayed.

Using *:S is an excellent way to ensure that log output is restricted to filters that you have explicitly specified, allowing the filter to act as a "whitelist" for log output.

The following filter expression shows all log messages for all tags with a priority of "warning" or higher: adb logcat *:W

If you're running Logcat from your development computer (as opposed to running it on a remote adb shell), you can also set the default filter expression by exporting the value of the environment variable ANDROID_LOG_TAGS: export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

Control log output format

In addition to tags and priorities, log messages contain a number of metadata fields. You can modify the output format of messages so that they display specific metadata fields. You can do this by using the -v option and specifying one of the following supported output formats.

brief: Shows the priority, tag, and PID of the process that sent the message.

long: Displays all metadata fields and separates messages with blank lines.

process: only show PID.

raw: Display raw log messages without other metadata fields.

tag: Only priority and tags are displayed.

thread:: Legacy format, showing the priority, PID, and TID of the thread that issued the message.

threadtime (default): Displays the date, call time, priority, tag, PID, and TID of the thread that issued the message.

time: Displays the date, time of invocation, priority, tag, and PID of the process that issued the message.

When starting Logcat, you can specify the desired output format with the -v option: [adb] logcat [-v ]

The following example shows how to generate messages with the output format thread: adb logcat -v thread

Note that you can specify only one output format with the -v option, but any number of meaningful modifiers can be specified. Logcat ignores modifiers that don't make sense.

format modifier

Format modifiers change the Logcat output according to any combination of one or more of the following modifiers. To specify a format modifier, use the -v option as follows: adb logcat -b all -v color -d

Every Android log message has a tag and priority associated with it. You can combine any format modifier with any of the following format options: brief, long, process, raw, tag, thread, threadtime, and time.

You can get format modifier details by entering logcat -v --help at the command line.

color: Use a different color to display each priority.

descriptive: Display the log buffer event description. This modifier only affects event log buffer messages and has no effect on other non-binary file buffers. Event descriptions are taken from the event-log-tags database.

epoch: Displays the time in seconds since January 1, 1970.

monotonic: Displays the time (in CPU seconds) since the last boot.

printable: Ensures that all binary logging content is escaped.

uid: If allowed by access control, displays the UID or Android ID of the logged process.

usec: Displays time accurate to microseconds.

UTC: Displays UTC time.

year: Adds the year to the displayed time.

zone: Add the local time zone to the displayed time.

View alternate log buffers

The Android logging system keeps multiple ring buffers for log messages, and not all log messages are sent to the default ring buffer.

To view additional log messages, you can run the logcat command with the -b option to request viewing of an alternate ring buffer. You can view any of the following alternate buffers:

radio: View buffers containing radio/telephone related messages.

events: View interpreted binary system event buffer messages.

main: View the main log buffer (default), excluding system and crash log messages.

system: View the system log buffer (default).

crash: View the crash log buffer (default).

all: View all buffers.

default: report main, system and crash buffers.

The following is the usage of the -b option: [adb] logcat [-b ]

The following example shows how to view a log buffer that contains messages related to radios and phones. adb logcat -b radio

Alternatively, you can specify multiple -b flags for all buffers to be output, as follows: logcat -b main -b radio -b events

You can specify a -b flag followed by a comma-separated list of buffers, for example: logcat -b main,radio,events

Guess you like

Origin blog.csdn.net/Hh19900902/article/details/127317296