Logcat In Android

Introduction

Logcat is an important tool in Android studio. Logcat helps developers to debug the code when an application is not running or has crashed. Suppose, we are developing a large application such as a Soical Media application, E-Commerce application, News application etc. Such applications have lots of modules and contain n number lines of code. If our application crashes then it is very difficult to find out the errors that occured when our application was running. Logcat is nothing but a solution to this type of problem. Logcat helps developers to detect and solve the error which caused application crash.

How to find Logcat in Android Studio?

Step 1

Just go to bottom section of Android Studio and find out the logcat option and click on it.

 

Step 2

After Clicking Logcat option, a new window will be open. This is our logcat window.

 

Here, no device is connected yet so logcat is empty. Sometime logcat still empty even after application crashed. In this situation just press the restarted button which appears on the left side of logcat window.

How logcat works?

Let's take a simple example

Step 1

Create new project in android studio and select empty activity

 

Click on Finish Button.

Step 2

Now go to activity_main.xml. By default we get a textview so just set the id of this textview.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 3

Now go to MainActivity.java. Create a Textview object and same time initialize with the help of findViewById() method.  

TextView textview=findViewById(R.id.textview);

MainActivty.java

package com.example.logcatexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView=findViewById(R.id.textview);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Step 4

Now connect any physical or virtual device to android studio and click on Run button. The application is installed however immediately crashed.  

The solution 

We can solve this problem using logcat.

Step 1 

Open Logcat and again click on run button. In logcat some line of code is generated so we have to find out the line that tells us which line of code contains error. Sometimes we need to scroll up in locat window to find out the error. Examine the below picture carefully and an orange color arrow indicate an error line.

 

Step 2

Now go to line number 8 in MainActivity.java.

We can not initialize the textview object here. We have to initialize textview object inside onCreate() method.

Updated MainActivity.java

package com.example.logcatexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textview);
    }
}

After updating MainActivty.java then click on Run button.

Output

 

Log Class

Log class is used to write the message which appears in the logcat window. Suppose we have several method in a particular activity and want to know which particular method is running or not. We can check this by writing log message inside that method.

Log Class Methods

Log class method is used to writing message which is appear in logcat window.

Syntax 

log.MethodName(String,String);

  • First parameter in log method is called TAG
  • Second Parameter in log method is called Message

There are several method in log class

Method Name Syntax Example
verbose Log.v(String,String); Log.v("MainActivity","Inside onCreate()");
debug Log.d(String,String); Log.d("MainActivity","Inside onStart()");
information Log.i(String,String); Log.i("MainActivity","Inside onResume()");
warning Log.w(String,String); Log.w("MainActivity","Inside onPause()");
error Log.e(String,String); Log.e("MainActivity","Inside onDestroy()");

Verbose method has a lower priority and error method has a higher priority.

How can we use it?

Let's take a simple example

Step 1

Go to MainActivity.java and added the below code inside onCreate() method.

Log.v("MainActivity1","Inside onCreate()");
Log.d("MainActivity2","Inside onCreate()");
Log.i("MainActivity3","Inside onCreate()");
Log.w("MainActivity4","Inside onCreate()");
Log.e("MainActivity5","Inside onCreate()");

Step 2

Now run the application and open logcat window. In logcat we have a search option where we can check easily if message is printed or not by writing TAG name in search option.

Output

 

Conclusion

In this article we have seen how to use logcat & what are log methods and how to use these method in the android studio.

Thanks for reading and hope you like it. If you have any suggestion or query on this article, please share your thoughts.


Similar Articles