【Android 開發入門】android studio 控制檯列印輸出日誌
有些情況下,不方便使用斷點的方式來除錯,而是希望在控制檯列印輸出日誌,使用過Eclipse的同學都知道Java可以使用 System.out.println(""); 來在控制檯列印輸出日誌,但是在android studio中卻是不行的,還是有差別的,那應該用什麼呢?
android.util.Log
在除錯程式碼的時候我們需要檢視除錯資訊,那我們就需要用Android Log類。
android.util.Log常用的方法有以下5個:Log.v() Log.d() Log.i() Log.w()以及 Log.e() 。根據首字母對應VERBOSE,DEBUG,INFO, WARN,ERROR。
1、Log.v 的除錯顏色為黑色
2、Log.d的輸出顏色是藍色的,僅輸出debug除錯的意思,但他會輸出上層的資訊,過濾起來可以通過DDMS的Logcat標籤來選擇.
3、Log.i的輸出為綠色,一般提示性的訊息information,它不會輸出Log.v和Log.d的資訊,但會顯示i、w和e的資訊
4、Log.w的意思為橙色,可以看作為warning警告,一般需要我們注意優化Android程式碼,同時選擇它後還會輸出Log.e的資訊。
5、Log.e為紅色,可以想到error錯誤,這裡僅顯示紅色的錯誤資訊,這些錯誤就需要我們認真的分析,檢視棧的資訊了。
注意:不同的列印方法在使用時都是某個方法帶上(String tag, String msg)引數,tag表示的是列印資訊的標籤,msg表示的是需要列印的資訊。
Log.java類
有小夥伴問我怎麼在Android Studio中檢視Log類?/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.util; import java.io.PrintWriter; import java.io.StringWriter; import java.net.UnknownHostException; /** * Mock Log implementation for testing on non android host. */ public final class Log { /** * Priority constant for the println method; use Log.v. */ public static final int VERBOSE = 2; /** * Priority constant for the println method; use Log.d. */ public static final int DEBUG = 3; /** * Priority constant for the println method; use Log.i. */ public static final int INFO = 4; /** * Priority constant for the println method; use Log.w. */ public static final int WARN = 5; /** * Priority constant for the println method; use Log.e. */ public static final int ERROR = 6; /** * Priority constant for the println method. */ public static final int ASSERT = 7; private Log() { } /** * 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. */ public static int v(String tag, String msg) { return println(LOG_ID_MAIN, VERBOSE, tag, msg); } /** * 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 */ public static int v(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr)); } /** * 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. */ public static int d(String tag, String msg) { return println(LOG_ID_MAIN, DEBUG, tag, msg); } /** * 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 */ public static int d(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr)); } /** * 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. */ public static int i(String tag, String msg) { return println(LOG_ID_MAIN, INFO, tag, msg); } /** * 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 */ public static int i(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr)); } /** * 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. */ public static int w(String tag, String msg) { return println(LOG_ID_MAIN, WARN, tag, msg); } /** * 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 */ public static int w(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(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 tr An exception to log */ public static int w(String tag, Throwable tr) { return println(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr)); } /** * 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. */ public static int e(String tag, String msg) { return println(LOG_ID_MAIN, ERROR, tag, msg); } /** * 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 */ public static int e(String tag, String msg, Throwable tr) { return println(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr)); } /** * Handy function to get a loggable stack trace from a Throwable * @param tr An exception to log */ public static String getStackTraceString(Throwable tr) { 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 PrintWriter(sw); tr.printStackTrace(pw); pw.flush(); return sw.toString(); } /** * 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. */ public static int println(int priority, String tag, String msg) { return println(LOG_ID_MAIN, priority, tag, msg); } /** @hide */ public static final int LOG_ID_MAIN = 0; /** @hide */ public static final int LOG_ID_RADIO = 1; /** @hide */ public static final int LOG_ID_EVENTS = 2; /** @hide */ public static final int LOG_ID_SYSTEM = 3; /** @hide */ public static final int LOG_ID_CRASH = 4; /** @hide */ @SuppressWarnings("unused") public static int println(int bufID, int priority, String tag, String msg) { return 0; } }
在程式碼編輯器中,將游標定位在Log上,然後按下快捷鍵:Ctrl+B,就可以開啟類檔案了。
Logcat視窗
我還真不知道這個視窗能不能完全關閉掉,它好像是和Android Monitor 是一體的。如下圖:
我們可以在logcat上按下然後拖動滑鼠,把它拉出來,彈出一個單獨的視窗。
關閉視窗之後,它又會回到Android Monitor 上。
上圖中點選Restore ‘logcat’View 之後又彈出視窗,而不是和Android Monitor並排在標籤上;
這時你可以在 ‘logcat’標籤上按鈕滑鼠左鍵拖動它到Monitor標籤旁邊鬆開滑鼠,它就回去了。
你也可以通過下拉列表框中的選項,或右側的搜尋框輸入指定的關鍵詞來篩選日誌內容。
也可以通過右側的編輯篩選配置來建立特定的篩選型別,方便以後使用。
小結
關於日誌的使用就說這麼多。
通過列印輸出日誌來除錯是一種方法,卻不是一種萬能的方法。
為什麼這麼說呢?
因為有些時候(比如執行緒、網路操作之類的),列印日誌的時候沒有Bug,但把日誌關了就會出Bug。
有些情況是除錯模式執行的時候不出Bug,而正式執行的時候就出Bug。
===========文後小料============
真正改變命運的其實並不是知識,而是這些知識帶給你的能力的提高。也就是說,轉化為能力的知識,才能夠改變你的命運。
當你去追求一個百分之百的安全感的時候,你可能就只能把自己困在原地,哪兒都去不了,其實這是最不安全的。
今天這一代的員工,一定是通過提升自己的能力換來自己的安全。我們也更傾向於把價值存在朋友圈裡邊。
===========文件資訊============
版權宣告:非商用自由轉載-保持署名-註明出處
署名(BY) :testcs_dn(微wx笑)
文章出處:[無知人生,記錄點滴](http://blog.csdn.net/testcs_dn)