Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

For the More Curious: Logging Levels and Methods

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 Figure 3.17.Each level has a corresponding method in the Log class.Sending output to the log is as simple as calling the corresponding Log method.

Figure 3.17

Figure 3.17 Log levels and methods

In addition, each of the logging methods has two signatures: one which 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. 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);

TrueFalse question;
try {
    question = mAnswerKey[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);
  • + Share This
  • 🔖 Save To Your Account