In Android Studio's Logcat window, how do you write a query that shows only your own app's warnings and errors from the last five minutes?
answer
- key-value terms, regex optional
- a special value for your package
- level matches at or above
- age takes a unit suffix
basics
~10 sType package:mine level:WARN age:5m into Logcat's query field. package:mine matches the package names in the open project, level:WARN keeps WARN and more severe entries, and age:5m keeps entries from the last five minutes.
solid answer
~40 sAndroid Studio's Logcat filters with a key-value query language, so I'd type `package:mine level:WARN age:5m`. `package:mine` is a special value matching any package name contained in the open project. `level:WARN` matches that level or more severe, so `ERROR` and `ASSERT` come along. `age:5m` keeps entries no older than five minutes, measured against the host computer's clock, not the device's. Terms separated by spaces are ANDed, except that repeated non-negated terms with the same key are ORed: `tag:foo tag:bar` means either tag. A `-` before a key negates it (`-tag:Chatty`), a `~` after it switches to a regex (`tag~:Net.*`), `is:crash` finds crashes and `name:` labels a query you save in the history. `Ctrl` + `Space` in the query field suggests keys.
go deeper
Know the three terms by heart: package:mine for your app, level: for severity at or above a level, age: for recent entries. Ctrl + Space shows the rest.
Be able to explain how terms combine: same-key terms OR, everything else AND, and explicit operators with normal precedence. Negation and regex are prefixes and suffixes on the key.
Show the habits that save time on a real bug: a split with is:crash beside the app filter, named and starred queries, and knowing that age: trusts the host clock.
Consider which saved queries and default filter a team should share so that logs from a crash loop or a flaky device read the same way for everyone who opens Logcat.
## What the Logcat window shows **Logcat** is the Android Studio tool window (View > Tool Windows > Logcat) that streams log entries from a connected device or emulator in real time. Entries come from your own `Log` calls, from services running on Android and from the system itself, for example garbage-collection messages. When the app throws an exception, Logcat shows the message and a stack trace whose lines link back to your code. Each entry carries a date and timestamp, a process and thread ID, a **tag**, a **package name**, a **priority** and the message. Without a query, a busy device produces far more output than anyone can read, so the query field at the top of the window is the tool you work with all day. ## The query language: keys The query field takes **key-value terms**. Regular expressions are available but optional. The keys you use most: | Key | Matches | |---|---| | `tag:` | the entry's tag | | `package:` | the package name of the app that logged it | | `process:` | the process name of the app that logged it | | `message:` | the message text | | `level:` | the given level **or more severe** | | `age:` | entries no older than the given duration | | `is:` | `is:crash` (a Java or native crash) or `is:stacktrace` (anything that looks like a Java stack trace, at any level) | | `name:` | no filtering: it names the query for the history list | Press `Ctrl` + `Space` in the field to see suggestions. ## How terms combine - **Negation**: put `-` before the key. `-tag:MyTag` drops entries whose tag contains `MyTag`. Negation works on `tag`, `package`, `message` and `line`. - **Regular expressions**: put `~` after the key. `tag~:My.*Tag` matches by regex. The two combine: `-tag~:My.*Tag`. - **Explicit operators**: `&` for AND, `|` for OR, parentheses for grouping. Normal precedence applies, so `tag:foo | level:ERROR & package:mine` means `tag:foo | (level:ERROR & package:mine)`. - **Implicit operators**: with no operator, repeated **non-negated terms with the same key are ORed** and everything else is ANDed. `tag:foo tag:bar package:myapp` is `(tag:foo | tag:bar) & package:myapp`, but `tag:foo -tag:bar package:myapp` is three ANDed terms. - Bare words separated by spaces form one low-precedence AND: `foo bar tag:bar1 | tag:bar2` is `'foo bar' & (tag:bar1 | tag:bar2)`. ## The special values behind the answer 1. **`package:mine`** matches any package name contained in the project open in the IDE. You do not have to type your application ID, and the filter keeps working when the process restarts. 2. **`level:WARN`** matches `WARN`, `ERROR` and `ASSERT`. The valid levels are `VERBOSE`, `DEBUG`, `INFO`, `WARN`, `ERROR` and `ASSERT`, and the value is not case-sensitive. 3. **`age:5m`** takes a whole number and a unit: `s`, `m`, `h` or `d`. Logcat compares the entry's timestamp with the **host's** clock, not the device's, so on a device whose clock is wrong `age:` can hide entries you expected. Put together, `package:mine level:WARN age:5m` is the answer, and adding `name:recent-warnings` gives it a readable label in the history. ## Working habits that pay off - **History and favourites**: Show history lists past queries, and starring one keeps it at the top across all your projects. - **Tabs and splits**: each Logcat tab, and each split inside a tab (Split Right or Split Down), has its own device, view options and query. Put an app filter beside an `is:crash` filter, or compare two devices. - **Process restarts**: when the app process stops and starts again, Logcat prints `PROCESS ENDED` and `PROCESS STARTED` and keeps your filters, so a crash-and-relaunch loop stays readable. - **Settings** (Settings > Tools > Logcat) set the cycle buffer size and the default filter that new Logcat windows open with. - **Compact view** and **Modify Views** remove columns you do not need, such as the process ID. ## Where the IDE ends This query language belongs to Android Studio's Logcat window. On a machine without the IDE, or in a CI script, you filter logs with the command-line tool's own filter syntax, which is covered with adb and the emulator. The two syntaxes are different: typing the command line's tag-and-priority filter into the IDE's query field does not filter by priority.
- How does Logcat evaluate `tag:foo tag:bar package:myapp` compared with `tag:foo -tag:bar package:myapp`?The first is `(tag:foo | tag:bar) & package:myapp`: repeated non-negated terms with the same key are ORed. In the second, `-tag:bar` is negated, so that rule does not apply and all three terms are ANDed: `tag:foo & -tag:bar & package:myapp`. If you want to be explicit, write `&`, `|` and parentheses yourself.
- Your app is logging, but `age:5m` returns nothing; what do you check?`age:` compares each entry's timestamp with the host computer's clock, not the device's. If the device's clock is wrong, its timestamps fall outside the window and the entries disappear. Check the device's date and time, or drop the `age:` term and clear the Logcat view before reproducing instead.
- How do you find a crash quickly when the stack trace was logged at an unexpected level?Use `is:crash` for entries that represent a crash, Java or native, and `is:stacktrace` for anything that looks like a Java stack trace at any level. Add `package:mine`, and put the query in its own split so it sits beside your normal filter.
- Logcat's query field needs regular expressions to filter by tag or package.
- level:WARN shows only WARN entries and hides ERROR and ASSERT.
- tag:foo tag:bar matches only entries that carry both tags at once.
- age:5m is measured against the device's clock, not the computer's.
- Typing adb's *:W filter spec into the IDE's query field keeps only warnings.