When you use the android.util.Log class to send log messages, you control not only the content of a message, but also a level that specifies how important the message is. Android supports five log levels, shown in Table 3.2. Each level has a corresponding method in the Log class. Sending output to the log is as simple as calling the corresponding Log method.
Table 3.2 Log levels and methods
Log level | Method | Used for |
---|---|---|
ERROR |
Log.e(…) |
errors |
WARNING |
Log.w(…) |
warnings |
INFO |
Log.i(…) |
informational messages |
DEBUG |
Log.d(…) |
debug output (may be filtered out) |
VERBOSE |
Log.v(…) |
development only |
In addition, each of the logging methods has two signatures: one that takes a TAG string and a message string and a second that takes those two arguments plus an instance of Throwable, which makes it easy to log information about a particular exception that your application might throw. Listing 3.8 shows some sample log method signatures. You can use regular Java string concatenation to assemble your message string or String.format if you have fancier needs.
Listing 3.8 Different ways of logging in Android
// Log a message at "debug" log level Log.d(TAG, "Current question index: " + mCurrentIndex); Question question; try { question = mQuestionBank[mCurrentIndex]; } catch (ArrayIndexOutOfBoundsException ex) { // Log a message at "error" log level, along with an exception stack trace Log.e(TAG, "Index was out of bounds", ex); }
18.190.156.212