Log — Display Messages
Imagine you are building an Android app and you want to know:
Did the button click work?
Did the data load correctly?
Where did the app crash?
Developers use Log messages to print information in a special debugging console called Logcat inside Android Studio.
1. Definition
Log is an Android class used to display debugging messages in Logcat while an app is running.
Simple meaning:
Log = A tool to print messages for developers to check what is happening inside the app.
2. Purpose
The Log class helps developers:
Debug errors
Track program flow
Display variable values
Detect crashes
Example situations:
| Situation | Why use Log |
|---|---|
| Button not working | Check if click event runs |
| App crashing | Find where crash occurs |
| API data issue | See returned data |
| Testing features | Track program flow |
3. Basic Syntax (Kotlin)
Log.d("TAG", "Message")
Explanation:
Log→ Android logging class"TAG"→ identifier for message"Message"→ text to display in Logcat
Example:
Log.d("MainActivity", "App Started")
Output in Logcat:
MainActivity: App Started
4. Different Log Types
Android provides different log levels.
| Method | Meaning | Use Case |
|---|---|---|
Log.v() | Verbose | Detailed debugging |
Log.d() | Debug | General debugging |
Log.i() | Info | Important information |
Log.w() | Warning | Something unexpected |
Log.e() | Error | Error or crash |
Example:
Log.v("App", "Verbose message")
Log.d("App", "Debug message")
Log.i("App", "Information message")
Log.w("App", "Warning message")
Log.e("App", "Error message")
5. Important Properties
Although Log mostly uses methods, some concepts act like properties.
| Property | Description |
|---|---|
| TAG | Label used to identify log messages |
| Message | Text displayed in Logcat |
| Log Level | Type of message (debug, error, etc.) |
Example:
val TAG = "MainActivity"
Log.d(TAG, "Activity Created")
6. Important Methods
Log.d()
Used for debug messages.
Log.d("Login", "Login button clicked")
Log.i()
Used for general information.
Log.i("App", "User logged in")
Log.w()
Used for warnings.
Log.w("Network", "Slow internet connection")
Log.e()
Used for errors.
Log.e("Database", "Data not found")
Log.v()
Used for very detailed debugging.
Log.v("AppFlow", "Method started")
7. Real-Time Examples
Example 1: Button Click Debug
button.setOnClickListener {
Log.d("ButtonClick", "Button was clicked")
}
Logcat output:
ButtonClick: Button was clicked
Example 2: Activity Lifecycle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i("MainActivity", "Activity Created")
}
Output:
MainActivity: Activity Created
Example 3: Checking Variable Value
val age = 21
Log.d("UserAge", "Age is $age")
Output:
UserAge: Age is 21
Example 4: Error Handling
try {
val result = 10 / 0
} catch (e: Exception) {
Log.e("MathError", "Cannot divide by zero")
}
8. Advantages
✔ Helps debug apps easily
✔ Shows program flow
✔ Helps find bugs and crashes
✔ Easy to use in Kotlin
9. Disadvantages
❌ Only visible to developers
❌ Too many logs can clutter Logcat
❌ Should be removed or minimized in production apps
10. One-Line Summary
Log is an Android class used to display debugging messages in Logcat to help developers monitor and troubleshoot app behavior.