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 sh
Log is the best way to trace our android project
like following code...
it will help u...
just look in DDMS logCat that how exactly project is build...
requirement... android.utils.Log; package is used..
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i {
Log.e("i = ",""+i);
Log.v("i = ",""+i);
Log.i("i = ",""+i);
Log.w("i = ",""+i);
Log.d("i = ",""+i);
}
}
i hope it will help u
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:
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!")
You can use the built in Log utility that will print right out to the LogCat.
You can use Log.e(String, String)
for errors which will appear in red. There is also v
, d
, i
, and w
for verbose, debug, info, and warning respectively.
The following should do the trick to print the exception
1. Log.d("myapp", Log.getStackTraceString(new Exception()));
or
2. You can get longer stack traces by digging deeper. For example:
Log.getStackTraceString(e.getCause().getCause());
System.out.println("")
in android will not run well because there is no Terminal that the app is corrected to.
You would be better off using Log.(d)(v)(e)(i)(w)
, because there is something actively monitoring LogCat.
System.out.println()
will print to LogCat, but only after an additional set of System instuctions, making it not as efficient, however, as i said, it still works.
From your own link:
System.out.println() (or printf() for native code) should never be used. System.out and System.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.
In addition, at the beginning of that page, it says:
The rules below are not guidelines or recommendations, but strict rules. Contributions to Android generally will not be accepted if they do not adhere to these rules.
So DON'T do it!