Logcat and Debugger

Android Studio 2: The New UI

Course 2 ยท Chapter 7 ยท Logcat and the Debugger

When an app misbehaves you have two tools: read what it says (Logcat) and stop it mid-run to look inside (the debugger). Both work on a running device from Chapter 6. This chapter covers the Logcat window and its query-style filter box, then breakpoints, stepping and the Debug window.

What was checked, and how: read this first
Checked on the install (a screenshot, Rabbit 2): the Logcat window's layout and its default filter. Checked in the docs (two web searches, summaries only): the query syntax and the debugger features, marked (docs). General knowledge, not checked: marked (general knowledge). I have no screenshot of the Debug window, so its layout is not described beyond what the docs say.

The Logcat Window

Logcat shows the messages the device's system and your app write. On the install it is a tool window (View > Tool Windows > Logcat; it has no shortcut) with these parts:

PartWhat it does
Tab strip, with a + buttonSeveral Logcat tabs, each with its own device and filter.
Device drop-downWhich device's log to show; it names the emulator and its Android version.
Filter boxDefaults to package:mine: only your project's app.
Cc toggleMatch case in the filter.
Star, help iconSave the current filter as a favourite; open help.
Vertical button stripClear, pause, restart, wrap lines, scroll to end and others (I could not read every icon's name).

The Query Filter

(docs) The filter takes key:value terms for tag, package, process, message and level. package:mine is special: it matches any package in the open project. level:INFO matches INFO and everything more serious (WARN, ERROR, ASSERT). A leading minus negates a term, and terms combine with &, | and parentheses. A ~ after the key (tag~:) switches that term to a regular expression.

# Only my app (the default) package:mine # Warnings and errors from my app package:mine & level:WARN # Hide a noisy tag package:mine -tag:Choreographer # Either my tag or any error (tag:Catalogue | level:ERROR) & package:mine

The docs' own example is (tag:foo | level:ERROR) & package:mine; the other lines apply the same rules (Choreographer and Catalogue are example tags). To write a message yourself, use Log.d("Catalogue", "saved item") in Kotlin (general knowledge) and filter on that tag.

The Debugger

(docs) Run the app with Debug instead of Run. When execution reaches a breakpoint (click in the editor's left gutter next to a line) the app pauses, and you can examine variables, evaluate expressions and continue line by line. The docs also describe conditional breakpoints (stop only when a condition is true) and logging breakpoints (write a message to Logcat without stopping).

ActionWhat it does
Step overRun the current line, stop at the next one (general knowledge).
Step intoGo inside the function the line calls (general knowledge).
Step outFinish the current function and stop in its caller (general knowledge).
ResumeRun until the next breakpoint (general knowledge).
Evaluate expressionRun a Kotlin expression in the paused frame; Alt+F8 on Windows and Linux (docs). The Variables pane also lets you evaluate ad hoc expressions.

The Debug tool window is Alt+5 and Run is Alt+4 (Chapter 1's tool-window list, checked on the install).

Compared with the Classic UI
Android Studio: The IDE Itself 4 describes the older Logcat, where you chose a level from a drop-down and built filters through the interface. On Rabbit 2 the filter is a typed query, and package:mine replaces the old "Show only selected application" idea (my reading of the two, general knowledge). The debugger's core ideas are unchanged.

Try It Yourself

Exercise 1

Run the scratch project on a device, open Logcat and describe each control you can see. Clear the log, then switch the filter from package:mine to level:ERROR and say what changes.

๐Ÿ“„ View solution
Exercise 2

Add Log.d("Demo", "hello") to onCreate in MainActivity.kt and run. Write filters that show only that message by tag, and by message text. Save one as a favourite with the star.

๐Ÿ“„ View solution
Exercise 3

Set a breakpoint on a line that runs at start-up, start the app with Debug, and when it pauses step over twice, then evaluate an expression with Alt+F8. Finally change the breakpoint into a logging breakpoint and check the message reaches Logcat.

๐Ÿ“„ View solution
Not Yet Verified
Checked on the install: the Logcat window parts in the table above, the package:mine default, Logcat having no shortcut, and the Run and Debug shortcuts (Alt+4, Alt+5).
Checked in the docs (summaries only): the key names, package:mine, level: semantics, negation, combining, the ~ regex form, conditional and logging breakpoints, and Alt+F8.
Not verified: the exact names of the Logcat button-strip icons; the Debug window's layout and button names; the step actions' shortcuts; how Logcat's old and new filters map to each other; my example queries (not run); the exercises (not run by me). A screenshot of the Debug window while paused would verify the debugger half.
Sources: View logs with Logcat (developer.android.com), Debug your app (developer.android.com).
Coming up
Chapter 8 covers the task-based Profiler, App Inspection with the Network Inspector and Layout Inspector, and the Build Analyzer.