In the Android Open Source Project's code style, it states that we shouldn't use System.out.println()
but I don't understand why. Can anyone explain? What should I use to trace my app's log?
Here's the line for reference:
System.out.println()
(orprintf()
for native code) should never be used.System.out
andSystem.err
get redirected to/dev/null
, so your print statements will have no visible effects. However, all the string building that happens for these calls still gets executed.
System. out. println is an IO-operation and therefor is time consuming. The Problem with using it in your code is, that your program will wait until the println has finished.
Log4j allows logging on class-by-class basis i.e., each class can be covered whereas System. out. println can be controlled at application level. Through Log4j we can turn on or off the logging at runtime by changing the configuration file.
in the upper menu of Android Studio. In the bottom status bar, click 5: Debug button, next to the 4: Run button. Now you should select the Logcat console. Check this article for more information.
You should use the android.util.Log
class.
Here's a description of what the Log
class does:
API for sending log output.
Generally, you should use the
Log.v()
,Log.d()
,Log.i()
,Log.w()
, andLog.e()
methods to write logs. You can then view the logs in logcat.The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
These are the available methods of the Log
class:
Log.d()
- Send a DEBUG
log message.Log.e()
- Send an ERROR
log message.Log.i()
- Send an INFO
log message.Log.v()
- Send a VERBOSE
log message.Log.w()
- Send a WARN
log message.Log.wtf()
- What a Terrible Failure: Report an exception that should never happen.The methods above (with the exception of Log.w
and Log.wtf
which have 3 possible patterns of arguments) require the following arguments:
String tag, String msg
:
tag
: Used to identify the source of a log message. This value may benull
.
msg
: The message you would like logged. This value may benull
.
String tag, String msg, Throwable tr
- Similar to the first pattern, but allows for an exception to be specified. This pattern should be used if you want to log an exception to the log output.
(For Log.w
and Log.wtf
) String tag, Throwable tr
Similar to the third pattern, but does not allow for a message to be specified. Note that you can still pass a message but it should be in the second arrangement of arguments.
EDIT: Going straight to answer your question: println()
of System.out
and System.err
will still be displayed in logcat but with limitations.
VERBOSE
, ERROR
, or DEBUG
using System.out
or System.err
.You can't define your own tag, it will display System.err
or System.out
with your text. For instance:
System.out.println("Hello!")
is equivalent to Log.i("System.out","Hello!")
System.err.println("Hello!")
is equivalent to Log.w("System.err","Hello!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With