FileDocCategorySizeDatePackage
Log.javaAPI DocAndroid 5.1 API13746Thu Mar 12 22:22:10 GMT 2015android.util

Log

public 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
VERBOSE
Priority constant for the println method; use Log.v.
public static final int
DEBUG
Priority constant for the println method; use Log.d.
public static final int
INFO
Priority constant for the println method; use Log.i.
public static final int
WARN
Priority constant for the println method; use Log.w.
public static final int
ERROR
Priority constant for the println method; use Log.e.
public static final int
ASSERT
Priority 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 intd(java.lang.String tag, java.lang.String msg)
Send a {@link #DEBUG} log message.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.

        return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
    
public static intd(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)
Send a {@link #DEBUG} log message and log the exception.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
param
tr An exception to log

        return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n" + getStackTraceString(tr));
    
public static inte(java.lang.String tag, java.lang.String msg)
Send an {@link #ERROR} log message.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.

        return println_native(LOG_ID_MAIN, ERROR, tag, msg);
    
public static inte(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)
Send a {@link #ERROR} log message and log the exception.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
param
tr An exception to log

        return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n" + getStackTraceString(tr));
    
public static java.lang.StringgetStackTraceString(java.lang.Throwable tr)
Handy function to get a loggable stack trace from a Throwable

param
tr An exception to log

        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 inti(java.lang.String tag, java.lang.String msg)
Send an {@link #INFO} log message.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.

        return println_native(LOG_ID_MAIN, INFO, tag, msg);
    
public static inti(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)
Send a {@link #INFO} log message and log the exception.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
param
tr An exception to log

        return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n" + getStackTraceString(tr));
    
public static native booleanisLoggable(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.

param
tag The tag to check.
param
level The level to check.
return
Whether or not that this is allowed to be logged.
throws
IllegalArgumentException is thrown if the tag.length() > 23.

public static intprintln(int priority, java.lang.String tag, java.lang.String msg)
Low-level logging call.

param
priority The priority/type of this log message
param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
return
The number of bytes written.

        return println_native(LOG_ID_MAIN, priority, tag, msg);
    
public static native intprintln_native(int bufID, int priority, java.lang.String tag, java.lang.String msg)

hide

public static android.util.Log$TerribleFailureHandlersetWtfHandler(android.util.Log$TerribleFailureHandler handler)
Sets the terrible failure handler, for testing.

return
the old handler
hide

        if (handler == null) {
            throw new NullPointerException("handler == null");
        }
        TerribleFailureHandler oldHandler = sWtfHandler;
        sWtfHandler = handler;
        return oldHandler;
    
public static intv(java.lang.String tag, java.lang.String msg)
Send a {@link #VERBOSE} log message.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.

        return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
    
public static intv(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)
Send a {@link #VERBOSE} log message and log the exception.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
param
tr An exception to log

        return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n" + getStackTraceString(tr));
    
public static intw(java.lang.String tag, java.lang.Throwable tr)

        return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
    
public static intw(java.lang.String tag, java.lang.String msg)
Send a {@link #WARN} log message.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.

        return println_native(LOG_ID_MAIN, WARN, tag, msg);
    
public static intw(java.lang.String tag, java.lang.String msg, java.lang.Throwable tr)
Send a {@link #WARN} log message and log the exception.

param
tag Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
param
msg The message you would like logged.
param
tr An exception to log

        return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n" + getStackTraceString(tr));
    
public static intwtf(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.

param
tag Used to identify the source of a log message.
param
msg The message you would like logged.

        return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
    
public static intwtf(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.

param
tag Used to identify the source of a log message.
param
tr An exception to log.

        return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
    
public static intwtf(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.

param
tag Used to identify the source of a log message.
param
msg The message you would like logged.
param
tr An exception to log. May be null.

        return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
    
static intwtf(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 voidwtfQuiet(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 intwtfStack(java.lang.String tag, java.lang.String msg)
Like {@link #wtf(String, String)}, but also writes to the log the full call stack.

hide

        return wtf(LOG_ID_MAIN, tag, msg, null, true, false);