Logpublic final class Log extends Object API for sending log output.
Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
methods.
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.
Tip: A good convention is to declare a TAG constant
in your class:
private static final String TAG = "MyActivity";
and use that in subsequent calls to the log methods.
Tip: Don't forget that when you make a call like
Log.v(TAG, "index=" + i);
that when you're building the string to pass into Log.d, the compiler uses a
StringBuilder and at least three allocations occur: the StringBuilder
itself, the buffer, and the String object. Realistically, there is also
another buffer allocation and copy, and even more pressure on the gc.
That means that if your log message is filtered out, you might be doing
significant work and incurring significant overhead. |
Fields Summary |
---|
public static final int | VERBOSEPriority constant for the println method; use Log.v. | public static final int | DEBUGPriority constant for the println method; use Log.d. | public static final int | INFOPriority constant for the println method; use Log.i. | public static final int | WARNPriority constant for the println method; use Log.w. | public static final int | ERRORPriority constant for the println method; use Log.e. | public static final int | ASSERTPriority constant for the println method. | private static TerribleFailureHandler | sWtfHandler | public static final int | LOG_ID_MAIN | public static final int | LOG_ID_RADIO | public static final int | LOG_ID_EVENTS | public static final int | LOG_ID_SYSTEM | public static final int | LOG_ID_CRASH |
Constructors Summary |
---|
private Log()
|
Methods Summary |
---|
public static int | d(java.lang.String tag, java.lang.String msg)Send a {@link #DEBUG} log message.
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
| public static int | d(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)Send a {@link #DEBUG} log message and log the exception.
return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n" + getStackTraceString(tr));
| public static int | e(java.lang.String tag, java.lang.String msg)Send an {@link #ERROR} log message.
return println_native(LOG_ID_MAIN, ERROR, tag, msg);
| public static int | e(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)Send a {@link #ERROR} log message and log the exception.
return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n" + getStackTraceString(tr));
| public static java.lang.String | getStackTraceString(java.lang.Throwable tr)Handy function to get a loggable stack trace from a Throwable
if (tr == null) {
return "";
}
// This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return "";
}
t = t.getCause();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new FastPrintWriter(sw, false, 256);
tr.printStackTrace(pw);
pw.flush();
return sw.toString();
| public static int | i(java.lang.String tag, java.lang.String msg)Send an {@link #INFO} log message.
return println_native(LOG_ID_MAIN, INFO, tag, msg);
| public static int | i(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)Send a {@link #INFO} log message and log the exception.
return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n" + getStackTraceString(tr));
| public static native boolean | isLoggable(java.lang.String tag, int level)Checks to see whether or not a log for the specified tag is loggable at the specified level.
The default level of any tag is set to INFO. This means that any level above and including
INFO will be logged. Before you make any calls to a logging method you should check to see
if your tag should be logged. You can change the default level by setting a system property:
'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
turn off all logging for your tag. You can also create a local.prop file that with the
following in it:
'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
and place that in /data/local.prop.
| public static int | println(int priority, java.lang.String tag, java.lang.String msg)Low-level logging call.
return println_native(LOG_ID_MAIN, priority, tag, msg);
| public static native int | println_native(int bufID, int priority, java.lang.String tag, java.lang.String msg)
| public static android.util.Log$TerribleFailureHandler | setWtfHandler(android.util.Log$TerribleFailureHandler handler)Sets the terrible failure handler, for testing.
if (handler == null) {
throw new NullPointerException("handler == null");
}
TerribleFailureHandler oldHandler = sWtfHandler;
sWtfHandler = handler;
return oldHandler;
| public static int | v(java.lang.String tag, java.lang.String msg)Send a {@link #VERBOSE} log message.
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
| public static int | v(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)Send a {@link #VERBOSE} log message and log the exception.
return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n" + getStackTraceString(tr));
| public static int | w(java.lang.String tag, java.lang.Throwable tr)
return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
| public static int | w(java.lang.String tag, java.lang.String msg)Send a {@link #WARN} log message.
return println_native(LOG_ID_MAIN, WARN, tag, msg);
| public static int | w(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)Send a {@link #WARN} log message and log the exception.
return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n" + getStackTraceString(tr));
| public static int | wtf(java.lang.String tag, java.lang.String msg)What a Terrible Failure: Report a condition that should never happen.
The error will always be logged at level ASSERT with the call stack.
Depending on system configuration, a report may be added to the
{@link android.os.DropBoxManager} and/or the process may be terminated
immediately with an error dialog.
return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
| public static int | wtf(java.lang.String tag, java.lang.Throwable tr)What a Terrible Failure: Report an exception that should never happen.
Similar to {@link #wtf(String, String)}, with an exception to log.
return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
| public static int | wtf(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)What a Terrible Failure: Report an exception that should never happen.
Similar to {@link #wtf(String, Throwable)}, with a message as well.
return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
| static int | wtf(int logId, java.lang.String tag, java.lang.String msg, java.lang.Throwable tr, boolean localStack, boolean system)
TerribleFailure what = new TerribleFailure(msg, tr);
int bytes = println_native(logId, ASSERT, tag, msg + '\n"
+ getStackTraceString(localStack ? what : tr));
sWtfHandler.onTerribleFailure(tag, what, system);
return bytes;
| static void | wtfQuiet(int logId, java.lang.String tag, java.lang.String msg, boolean system)
TerribleFailure what = new TerribleFailure(msg, null);
sWtfHandler.onTerribleFailure(tag, what, system);
| public static int | wtfStack(java.lang.String tag, java.lang.String msg)Like {@link #wtf(String, String)}, but also writes to the log the full
call stack.
return wtf(LOG_ID_MAIN, tag, msg, null, true, false);
|
|