android series (three) android logcat

1. logcat is a command-line tool in Android, which can be used to get the log information of the program.
How to output the desired information to the log during the development process?
    The Log class is a log class that you can use in your code to print out messages using logcat.
      Common logging methods include:
    v(String, String) (verbose) show full information
    d(String, String) (debug) show debug
    information i(String, String) (information) show general information
    w(String, String) ( warning) Display warning message
    e(String, String) (error)
Example of displaying error message log:

Log.i("MyActivity", "MyClass.getView() — get item number " + position);
logcat command to get log: adb logcat
logcat will output information similar to the following:
    I/MyActivity( 1557): MyClass.getView() — get item number 1

 

Practical examples


import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

//Add a tag to easily know which check log
    public static java.lang.String TAG="MyApp";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main) ;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//TAG tag
        Log.v(TAG, "LOGV, I am black ************ ****************");
        Log.d(TAG, "LOGD, I am blue ***************** **********");
        Log.i(TAG, "LOGI, I am green ************************ *****");
        Log.w(TAG, "LOGW, I am orange ****************************" );
        Log.e(TAG, "LOGE, I am red ****************************");

    }


}

2. logcat buffer - buffer introduction
Android's Log output is huge, especially the Log of the communication system. Therefore, Android outputs the Log to different buffers. Four Log buffers are currently defined:
Radio: Log
System of the output communication system: Log of the output system components
Event: Log of the output event module
Main: Logs of all java layers, and Log
buffersUsed by system components, general applications do not need to care, and the application LOG is output to the main buffer.

three. logcat command

1. View the log command of the buffer: -b followed by the buffer name

adb logcat -b main -b system -b radio -b event

2. Clear all logs in the buffer and exit (you can use -g to view the buffer after clearing)

adb logcat -c

3. Dump the log of the buffer to the screen, and then exit

adb logcat -d

4. Output the log to the specified file <file name>. The default is standard output (stdout)

-f <filename>

Often used with -c and -r

------------------------------------------------------------------------------------------

logcat needs to be run on the device after adb shell, otherwise it will report an error:

C:\Users\kanglimin>adb logcat -f E:\logcat\a.txt -n 2 -r 1
couldn't open output file: Read-only file system

 

---------------------------------------------------------------------------------------------

logcat -f /data/local/tmp/log.txt -n 10 -r 1

-rw------- root     root          258 2016-07-13 15:47 log.txt
-rw------- root     root         1056 2016-07-13 15:47 log.txt.1
-rw------- root     root         1066 2016-07-13 15:47 log.txt.10
-rw------- root     root         1056 2016-07-13 15:47 log.txt.2
-rw------- root     root         1058 2016-07-13 15:47 log.txt.3
-rw------- root     root         1056 2016-07-13 15:47 log.txt.4
-rw------- root     root         1066 2016-07-13 15:47 log.txt.5
-rw------- root     root         1056 2016-07-13 15:47 log.txt.6
-rw------- root     root         1054 2016-07-13 15:47 log.txt.7
-rw------- root     root         1054 2016-07-13 15:47 log.txt.8
-rw------- root     root         1056 2016-07-13 15:47 log.txt.9

 

 

5. Print the size of the log buffer and exit

C:\Users\kanglimin>adb logcat -g
/dev/log/main: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max pa
yload is 4076b
/dev/log/system: ring buffer is 256Kb (95Kb consumed), max entry is 5120b, max p
ayload is 4076b

6. Set the maximum number of logs <count>. The default value is 4, which needs to be used together with the -r option.

-n <count> number of files

7. Output log every <kbytes>, the default value is 16, it needs to be used with the -f option

-r <kbytes> unit is KB

8. Set up filters

-s

9. Set the log message for the output format. The default is the ephemeral format. List of supported formats

-v <format>

logcat formatted output - parameter description
The log message contains a metadata field, in addition to the label and priority. You can modify the output to display a message in a specific metadata field format. To do this, you use the -v option to specify a supported output format. The following formats are supported:
brief - displays priority/tag and process PID messages (default format)
process - only PID is displayed
tag - only priority/tag is displayed
raw - raw log message is displayed, no other metadata fields
time — calls display date, time, priority/label, and process PID issues message
threadtime — calls display date, time, priority, label, and PID TID message issues
long — displays all metadata fields with blank lines and separate information

 

 example:

When logcat starts, you can specify your desired output format using the -v option:
    [adb] logcat [-v <format>]
Here is an example showing how to generate messages
in the thread output format:     adb logcat -v thread
Note that you can only specify one output format - v

 

three. log priority

Priority is identified by character, the following priorities are from low to high
V — Verbose (lowest priority)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent

It is convenient to find users to view logs related to themselves

To reduce the output of the log, you can create a filter:
filter syntax: tag:priority
adb logcat ActivityManager:I MyApp:D *:S
filter TAG for ActivityManager output level greater than I logs and TAG for MyApp output level greater than D logs
adb logcat * :W Set the filter level to W      
Commonly used settings above are environment variables:
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

 

 

 

 

 

 

 

 

 

 

 

 

 

Log.i("MyActivity", "MyClass.getView() — get item number " + position);

 

logcat command to get log : adb logcat

 

logcat will output information similar to the following :

 

  I/MyActivity( 1557): MyClass.getView() — get item number 1

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327050992&siteId=291194637