Why doesn't "System.out.println" work in Android?
In Android, System.out.println
is not recommended for logging. Instead, Android's Log class methods like Log.d()
are your friend:
Tags help categorize logs; switch "YourTag"
with something identifiable. Similarly, Log.e()
, Log.i()
, Log.v()
, and Log.w()
are at service for error, info, verbose, and warning messages.
Graduating from System.out.println
Standard Java applications utilize System.out.println()
for sending output to the console. However, in an Android environment, Logcat is the preferred tool for logging.
Level-headed logging
- Log your errors with
Log.e("Tag", "Error message");
- Shout warnings using
Log.w("Tag", "Warning message");
- Tell the world info with
Log.i("Tag", "Info message");
Caveat: Keep your log tag consistent as a static final String
. This will be your beacon in the stormy sea of logs.
Eyes on the logs: Viewing tools
Eclipse IDE & Android Studio gift you with a Logcat view to inspect and filter log messages. Those preferring a command-line ascent, adb logcat
will get you to the pinnacle.
The "Edge" cases and the "legacy" solutions
Got a legacy code or an external library smitten with System.out.println
? No worries! With reflection, you can redirect System.out
to Android's Log
.
The Green way: Efficient logging
Verbose and debug logs are not invited to the production build party. They're the life of the dev party but could gatecrash your performance in the live app.
Cutting through logs: Identifying and filtering
Adorn your log messages with unique tags to swiftly filter through Logcat. It's like threading the debugging needle.
Was this article helpful?