Android LogCat And Logging Best Practice

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

1. Android Log Methods.

  1. Log.v() : Print verbose level log data. 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 behaviours.
  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 crash.

2. Android Log Methods Example.

This example is very simple. When you click the button, it will print above 5 kinds of log data in the LogCat console.

android logcat and logging best practice

When you input the search keyword LogActivity in LogCat panel, the app log data will be filtered out. For each line of log data, there are log time, class name and log message.

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 accurate.android logcat log output

Click the Settings icon in LogCat panel, you can config which column to be displayed for each line of log.

READ :   Android Button OnClick Example

android logcat settings

3. Android Log Example Source Code.

3.1 Main Layout Xml File.

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.

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.

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.

Commonly the tag is the class name. You can simply create the tag name follow below method in activity java file.

  1. Write logt in activity java file outside of onCreate() method.
  2. Click Tab key then android studio will generate the tag name automatically.
    android studio create log tag name automatically
  3. Besides the class name, you can create the tag name by the class function also. You had better create a class which include all static String Constant that used as tag name as below.
    package com.dev2qa.example.constant;
    
    /**
     * Created by Jerry on 12/9/2017.
     */
    
    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");

    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.

There are four items in LogCat filter drop down list.

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

Select the Edit Filter Configuration drop down item, there will popup a dialog which let you create or edit log filters. You can specify below conditions to filter out related log. For example, filter by log tag, log message, package name, pid and log level. Log tag, message and package name support regular expression.

android studio logcat edit filter

5. LogCat Operation.

You can clear logcat content by right click the logcat output console, click Clear logcat menu in popup menu list.

If you find the log data can not be cleared after 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.

android clear logcat console output

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

You have learn 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 below steps.

But before you can try, you had better read below articles if you do not know.

  1. How To Use Android Debug Bridge (ADB)
  2. How To Enable USB Debugging Mode On Android Device

Save LogCat Log Data To Local Text File.

  1. Open a dos command, cd to your %ANDROID_HOME% \ platform-tools folder.
  2. Input command adb devices, this command will list all devices that connected.
  3. 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.
    use adb to save logcat log to text file

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

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