Share
Explore

Using Logcat to Trace Code Execution

Student Guide: Using Logcat to Trace Code Execution

What is Logcat?

Logcat is Android's logging system that displays system messages, including:
Your app's debug messages
System error messages
Stack traces when your app crashes

How to Access Logcat in Android Studio

Open Logcat Panel:
View → Tool Windows → Logcat
Or click "Logcat" tab at the bottom of Android Studio
Filter Your Logs:
In the search box, type: DataTypesDemo
This filters to show only logs with our TAG
Log Levels (color-coded):
VERBOSE (V) - Gray - Everything
DEBUG (D) - Blue - Debug information
INFO (I) - Green - Informational messages
WARN (W) - Orange - Warnings
ERROR (E) - Red - Errors

Understanding Our Log Statements

Basic Log Pattern:

kotlin
Log.d(TAG, "message")
Log.d = Debug level log
TAG = "DataTypesDemo" (for filtering)
"message" = What to display

What You'll See in Logcat:

Activity Lifecycle:
D/DataTypesDemo: ====================================
D/DataTypesDemo: onCreate: Activity starting
D/DataTypesDemo: ====================================
Button Clicks:
D/DataTypesDemo: BUTTON CLICKED: Store Data
Data Storage:
D/DataTypesDemo: Storing String - Key: 'username', Value: 'john_doe'
D/DataTypesDemo: Storing Int - Key: 'user_age', Value: 25
D/DataTypesDemo: Storing Boolean - Key: 'is_premium', Value: true
Data Retrieval:
D/DataTypesDemo: Retrieved String - Username: 'john_doe'
D/DataTypesDemo: Retrieved Int - Age: 25

Exercise: Trace the Execution Flow

Run the app and open Logcat
Filter by "DataTypesDemo"
Click "Store All Data Types" and observe:
Each data type being stored
The order of operations
When apply() is called
Click "Retrieve All Data" and observe:
Each value being retrieved
Default values when keys don't exist
The complete list of stored keys
Click "Clear All Data" and observe:
What data exists before clearing
The clear operation

Adding Your Own Logs

Try adding these log statements to understand the flow better:
kotlin
// Before storing
Log.d(TAG, "User clicked store button at ${System.currentTimeMillis()}")

// After retrieving
Log.d(TAG, "Successfully retrieved ${allEntries.size} items")

// In catch blocks
Log.e(TAG, "Failed to store data", exception)

Tips for Effective Logging

Use descriptive messages:
kotlin
// Bad
Log.d(TAG, "data: $data")

// Good
Log.d(TAG, "Retrieved user preferences - Theme: $theme, Language: $language")
Log method entry/exit:
kotlin
Log.d(TAG, "entering storeUserData()")
// ... method code ...
Log.d(TAG, "exiting storeUserData() - success")
Use appropriate log levels:
Debug (Log.d) - Development info
Error (Log.e) - Actual errors
Warning (Log.w) - Potential issues
Clean up before release:
Remove or disable verbose logs
Keep only essential error logging

Common Logcat Filters

Tag filter: tag:DataTypesDemo
Package filter: package:com.example.sharedpreferencesdemo
Log level: level:ERROR (shows only errors)
Text search: Any text in the message

Debugging Challenge

Add logs to answer these questions:
How long does it take to store all data? (Hint: log timestamps)
What happens if you retrieve data before storing?
Can you store null values? (Try it and check logs)
Remember: Logcat is your window into what your app is doing behind the scenes!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.