Android LogCat And Logging Best Practice

android.util.Log is the log class that provides the log function. It provides the below methods to log data into the LogCat console.

1. Android Log Methods.

  1. Log.v(): Print verbose level log data. The verbose level is the lowest log level, if you print so much this kind of log data, it is not meaningful.
  2. Log.d(): Print debug level log data. Debug level is one step higher than verbose level. Debug log data is usually useful in android application development and testing.
  3. Log.i():  Print info level log data. Info level is one step higher than debug level. Info log data is used to collect user actions and behaviors.
  4. Log.w(): Print warn level log data. Warn level is one step higher than info level. When you see this kind of log data, it means your code exists potential risks, you need to check your code carefully.
  5. Log.e(): Print error level log data. Error level is the highest level. It is always used in java code catch block to log exception or error information. This kind of log data can help you to find out the root cause of app crashes.

2. Android Log Methods Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/ciBUMesUfVo

  1. This example is very simple. When you click the button, it will print above 5 kinds of log data in the LogCat console.
  2. When you input the search keyword LogActivity in the LogCat panel, the app log data will be filtered out. For each line of log data, there are the log time, class name, and log message.
  3. You can also filter out the log data by it’s type, verbose, info, debug, warn, or error. This can make log data search more easily and accurately, you can see the below demo video.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/zyUvmTF_Mzw

  1. Click the Settings icon in the LogCat panel, you can configure which column to be displayed for each line of the log.
  2. The columns include Show date and time, Show process and thread IDs(PID-TID), Show package name, Show tag, you can see it in the below picture.
    android-logcat-settings

3. Android Log Example Source Code.

3.1 Main Layout Xml File.

  1. activity_log.xml
    <Button
        android:id="@+id/createLogButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me To Generate Log Data In LogCat"/>

3.2 Activity Java File.

  1. LogActivity.java
    package com.dev2qa.example;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class LogActivity extends AppCompatActivity {
    
        private static final String LOG_TAG = "LogActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_log);
    
            setTitle("dev2qa.com --- Android Log Methods Example.");
    
            // Get the button instance.
            Button createLogButton  = (Button)findViewById(R.id.createLogButton);
            // When the button is clicked, print log data.
            createLogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.v(LOG_TAG, "This is verbose log");
                    Log.d(LOG_TAG, "This is debug log");
                    Log.i(LOG_TAG, "This is info log");
                    Log.w(LOG_TAG, "This is warn log");
                    Log.e(LOG_TAG, "This is error log");
                }
            });
        }
    }

3.3 Log Tag Name Tip.

  1. The first parameter for the log method is a tag name, we can use this tag name to filter out the useful log data easily.
  2. Commonly the tag is the class name. You can simply create the tag name follow the below method in the activity java file.
  3. Write the log data in the activity java file outside of the onCreate() method.
  4. Click the Tab key, then the android studio will generate the tag name automatically. You can see this step demo video on URL https://youtu.be/ZxhnFly4-2o.
  5. Besides the class name, you can create the tag name by the class function also. You had better create a class that includes all static String Constant that used as tag name as below.
    package com.dev2qa.example.constant;
    
    public class LogTagName {
        
        public static final String  LOG_TAG_UI = "LOG_TAG_UI";
    
        public static final String  LOG_TAG_NETWORK = "LOG_TAG_NETWORK";
    
        public static final String  LOG_TAG_DATABASE = "LOG_TAG_DATABASE";
    
        public static final String  LOG_TAG_LOGIC = "LOG_TAG_LOGIC";
    
        public static final String  LOG_TAG_APP = "LOG_TAG_APP";
    }
    
    Log.w(LogTagName.LOG_TAG_NETWORK, "This is warn log");
  6. This way can make the log more readable, even none programmer can understand the meaning of the log.
    android-custom-log-tag-name

4. LogCat Filter.

4.1 There are four items in the LogCat filter drop-down list.

  1. Show only selected applications.
  2. Firebase. This is a log analytic tool provided by Google.
  3. No Filters.
  4. Edit Filter Configuration.

4.2 How To Edit LogCat Filter.

  1. Select the Edit Filter Configuration item in the LogCat filter drop-down list, there will popup the Create New Logcat Filter dialog window that lets you create or edit log filters.
  2. You can specify the below conditions to filter out related logs. For example, filter by Log Tag, filter by Log Message, filter by Package Name, filter by PID and filter by Log Level. The Log Tag, Log Message, and Package Name filter keyword support regular expression.
    android-studio-logcat-edit-filter

5. LogCat Operation.

  1. You can clear the Logcat content by right click the Logcat output console log message area, then click the Clear logcat menu item in the popup menu list.
  2. If you find the log data can not be cleared after the above action. That means the emulator has been stopped or disconnected. You need to select an activity emulator in the drop-down list to run the android app and watch the log data.

6. How To Retrieve Android Crash Logs Use ADB Command.

  1. You have learned how to record logs in android studio. But how to get those log data when you need to analyze the data for development. You can follow the below steps.
  2. But before you can try, you had better read the below 2 articles if you do not know. How To Use Android Debug Bridge (ADB), How To Enable USB Debugging Mode On Android Device.
  3. Now you can follow the below steps to save Logcat log data to a local text file.
  4. Open a dos command, cd to your %ANDROID_HOME% \ platform-tools folder.
  5. Input the command adb devices, this command will list all devices that are connected.
  6. Run command adb logcat>>logcatData.txt, after a while, you can find the file in the platform-tools folder. This file will include all the Logcat logs in it. The log file will increase while the emulator runs.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Josselin
Josselin

Thanks for this post! Especially “3.3 Log Tag Name Tip” ?

1
0
Would love your thoughts, please comment.x
()
x