日志工具Log(android.util.Log),共有五個方法打印日志
package android.util;
public final class Log {
public static final int ASSERT = 7;
public static final int DEBUG = 3;
public static final int ERROR = 6;
public static final int INFO = 4;
public static final int VERBOSE = 2;
public static final int WARN = 5;
Log() {
throw new RuntimeException("Stub!");
}
public static int v(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int v(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int d(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int d(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int i(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int i(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int w(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int w(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static native boolean isLoggable(String var0, int var1);
public static int w(String tag, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int e(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int e(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int wtf(String tag, String msg) {
throw new RuntimeException("Stub!");
}
public static int wtf(String tag, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int wtf(String tag, String msg, Throwable tr) {
throw new RuntimeException("Stub!");
}
public static String getStackTraceString(Throwable tr) {
throw new RuntimeException("Stub!");
}
public static int println(int priority, String tag, String msg) {
throw new RuntimeException("Stub!");
}
}
級別從低到高:
- Log.v() 最為瑣碎的,意義最小的日志信息,對應級別verbose
- Log.d() 打印調試信息,最常用,對應級別debug
- Log.i() 打印比較重要的數據,可以幫助分析用戶行為數據,對應級別info
- Log.w() 打印警告信息,此處有潛在危險,需要修復,對應級別warn
- Log.e() 打印程序中的錯誤信息,,必須修復,對應級別error
注1:tag 參數一般為當前的類名,msg想打印的具體信息
private String tag = MainActivity.class.getSimpleName();
/**
* Activity 被創建時
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "onCreate");
}
注2:System.out.println(),雖然在eclipse中輸入syso,按下代碼提示鍵就會出來,但在Android studio中不支持這種快捷方式。打印日志的的優點:可以添加過濾器、有等級區分、打印可控制、有打印時等。
注3:快捷輸入,如輸入logd,然后點擊tab鍵,就會補全。
在onCreate()外面輸入logt,點擊tab,就會以當前的類名作為值生成一個TAG常量
private static final String TAG = "MainActivity";
注4:添加過濾器,選擇顯示級別,搜索等等
圖片.png