About the author: CSDN content partner and technical expert, built an APP with tens of millions of daily users from scratch.
Focus on sharing original series of articles in various fields, specializing in java backend, mobile development, business realization, artificial intelligence, etc. I hope you will support me a lot.
Table of contents
1. Introduction
We continue to summarize and learn basic knowledge , review the past and learn new things.
This page introduces the command line logcat tool, including viewing and filtering logs, and writing logs to files.
2. Overview
Logcat is a command-line tool for dumping system message logs, including messages written from your application using the Log class.
Logcat is relatively simple. Here we only record some of the more common usages that we may use in our daily work.
3. Daily usage
3.1 Panel introduction
3.2 Log filtering
Label | describe | Negation and regular expressions |
---|---|---|
tag: | Matches the tag field of the log entry. | tag: tag~: tag=: -tag: -tag~: -tag=: |
package: | Matches the package name of the logging application. | package: package~: package=: -package: -package~: -package=: |
process: | Matches the process name of the logging application. | |
message: | Matches the message portion of the log entry. | message: message~: message=: -message: -message~: -message=: |
level: | Matches logs with a specified severity level or higher, such as DEBUG. | |
is: | is key a special match | is:crash is:firebase is:stacktrace |
age: | Matches if the entry timestamp is the most recent. Values are specified as a number followed by a letter that represents the unit of time: s for seconds, m for minutes, h for hours, and d for days. For example, age: 5m will only filter messages logged within the past 5 minutes. |
3.2.1 Negation and regular expressions
Negation is expressed by adding - in front of the field name. For example, -tag:MyTag matches log entries whose tag does not contain the string MyTag.
Regular expression matching is indicated by appending ~ to the field name. For example tag~:My.*Tag.
Negation and regular expression modifiers can be used together. For example, -tag~:My.*Tag.
3.2.2 Logical operators and parentheses
The query language supports the AND and OR operators represented by & and | and parentheses, eg:
(tag:foo | level:ERROR) & package:mine
If no logical operators are applied, the query language automatically treats multiple non-negative key-value filter items with the same key as OR, and other filter items as AND.
tag:foo tag:bar package:myapp
求值结果为:
(tag:foo | tag:bar) & package:myapp
但是:
tag:foo -tag:bar package:myapp
求值结果为:
tag:foo & -tag:bar & package:myapp
- is:crash matches log entries that represent app crashes (native or Java).
- is:stacktrace matches any log entry that represents anything like a Java stack trace, regardless of log level.
3.3 Save log to file
[adb] shell logcat [<option>] ... [<filter-spec>] ...
adb logcat > 12345.txt