Package android.util
Class Log
- java.lang.Object
-
- android.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
TAGconstant 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceLog.TerribleFailureHandlerInterface to handle terrible failures fromwtf(java.lang.String, java.lang.String).
-
Field Summary
Fields Modifier and Type Field Description static intASSERTPriority constant for the println method.static intDEBUGPriority constant for the println method; use Log.d.static intERRORPriority constant for the println method; use Log.e.static intINFOPriority constant for the println method; use Log.i.static intLOG_ID_CRASHstatic intLOG_ID_EVENTSstatic intLOG_ID_MAINstatic intLOG_ID_RADIOstatic intLOG_ID_SYSTEMstatic intVERBOSEPriority constant for the println method; use Log.v.static intWARNPriority constant for the println method; use Log.w.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intd(String tag, String msg)Send aDEBUGlog message.static intd(String tag, String msg, Throwable tr)Send aDEBUGlog message and log the exception.static inte(String tag, String msg)Send anERRORlog message.static inte(String tag, String msg, Throwable tr)Send aERRORlog message and log the exception.static StringgetStackTraceString(Throwable tr)Handy function to get a loggable stack trace from a Throwablestatic inti(String tag, String msg)Send anINFOlog message.static inti(String tag, String msg, Throwable tr)Send aINFOlog message and log the exception.static booleanisLoggable(String tag, int level)Checks to see whether or not a log for the specified tag is loggable at the specified level.static intprintln(int priority, String tag, String msg)Low-level logging call.static intprintln_native(int bufID, int priority, String tag, String msg)static Log.TerribleFailureHandlersetWtfHandler(Log.TerribleFailureHandler handler)Sets the terrible failure handler, for testing.static intv(String tag, String msg)Send aVERBOSElog message.static intv(String tag, String msg, Throwable tr)Send aVERBOSElog message and log the exception.static intw(String tag, String msg)Send aWARNlog message.static intw(String tag, String msg, Throwable tr)Send aWARNlog message and log the exception.static intw(String tag, Throwable tr)static intwtf(String tag, String msg)What a Terrible Failure: Report a condition that should never happen.static intwtf(String tag, String msg, Throwable tr)What a Terrible Failure: Report an exception that should never happen.static intwtf(String tag, Throwable tr)What a Terrible Failure: Report an exception that should never happen.static intwtfStack(String tag, String msg)Likewtf(String, String), but also writes to the log the full call stack.
-
-
-
Field Detail
-
VERBOSE
public static final int VERBOSE
Priority constant for the println method; use Log.v.- See Also:
- Constant Field Values
-
DEBUG
public static final int DEBUG
Priority constant for the println method; use Log.d.- See Also:
- Constant Field Values
-
INFO
public static final int INFO
Priority constant for the println method; use Log.i.- See Also:
- Constant Field Values
-
WARN
public static final int WARN
Priority constant for the println method; use Log.w.- See Also:
- Constant Field Values
-
ERROR
public static final int ERROR
Priority constant for the println method; use Log.e.- See Also:
- Constant Field Values
-
ASSERT
public static final int ASSERT
Priority constant for the println method.- See Also:
- Constant Field Values
-
LOG_ID_MAIN
public static final int LOG_ID_MAIN
- See Also:
- Constant Field Values
-
LOG_ID_RADIO
public static final int LOG_ID_RADIO
- See Also:
- Constant Field Values
-
LOG_ID_EVENTS
public static final int LOG_ID_EVENTS
- See Also:
- Constant Field Values
-
LOG_ID_SYSTEM
public static final int LOG_ID_SYSTEM
- See Also:
- Constant Field Values
-
LOG_ID_CRASH
public static final int LOG_ID_CRASH
- See Also:
- Constant Field Values
-
-
Method Detail
-
v
public static int v(String tag, String msg)
Send aVERBOSElog message.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.
-
v
public static int v(String tag, String msg, Throwable tr)
Send aVERBOSElog message and log the exception.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.tr- An exception to log
-
d
public static int d(String tag, String msg)
Send aDEBUGlog message.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.
-
d
public static int d(String tag, String msg, Throwable tr)
Send aDEBUGlog message and log the exception.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.tr- An exception to log
-
i
public static int i(String tag, String msg)
Send anINFOlog message.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.
-
i
public static int i(String tag, String msg, Throwable tr)
Send aINFOlog message and log the exception.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.tr- An exception to log
-
w
public static int w(String tag, String msg)
Send aWARNlog message.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.
-
w
public static int w(String tag, String msg, Throwable tr)
Send aWARNlog message and log the exception.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.tr- An exception to log
-
isLoggable
public static boolean isLoggable(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.- Parameters:
tag- The tag to check.level- The level to check.- Returns:
- Whether or not that this is allowed to be logged.
- Throws:
IllegalArgumentException- is thrown if the tag.length() > 23.
-
e
public static int e(String tag, String msg)
Send anERRORlog message.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.
-
e
public static int e(String tag, String msg, Throwable tr)
Send aERRORlog message and log the exception.- Parameters:
tag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.tr- An exception to log
-
wtf
public static int wtf(String tag, 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 theandroid.os.DropBoxManagerand/or the process may be terminated immediately with an error dialog.- Parameters:
tag- Used to identify the source of a log message.msg- The message you would like logged.
-
wtfStack
public static int wtfStack(String tag, String msg)
Likewtf(String, String), but also writes to the log the full call stack.
-
wtf
public static int wtf(String tag, Throwable tr)
What a Terrible Failure: Report an exception that should never happen. Similar towtf(String, String), with an exception to log.- Parameters:
tag- Used to identify the source of a log message.tr- An exception to log.
-
wtf
public static int wtf(String tag, String msg, Throwable tr)
What a Terrible Failure: Report an exception that should never happen. Similar towtf(String, Throwable), with a message as well.- Parameters:
tag- Used to identify the source of a log message.msg- The message you would like logged.tr- An exception to log. May be null.
-
setWtfHandler
public static Log.TerribleFailureHandler setWtfHandler(Log.TerribleFailureHandler handler)
Sets the terrible failure handler, for testing.- Returns:
- the old handler
-
getStackTraceString
public static String getStackTraceString(Throwable tr)
Handy function to get a loggable stack trace from a Throwable- Parameters:
tr- An exception to log
-
println
public static int println(int priority, String tag, String msg)Low-level logging call.- Parameters:
priority- The priority/type of this log messagetag- Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.msg- The message you would like logged.- Returns:
- The number of bytes written.
-
-