How to Print to the Console in Android Studio? - GeeksforGeeks

23 Mar, 2021
In computer technology, the console is simply a combination of a monitor and an input device. Generally, the input device is referred to here as the pair of mouse and keyboard. In order to proceed with the topic, we have to understand the terminology of computer science, which is an important part of the process of developing software, and it’s called debugging. Debugging is the process of identifying a bug or an error and fixing it properly for the software. We have to test the software before producing it on market in multiple phases. We have to debug the errors also, then only software will be pure error-free and it will be ready for production. The most of the things which computer does with our code is invisible for us. For Debugging, we have to identify the error first then only we can solve that error. If you want to see the error, then you have to print or log it to our console directly. There are many and different methods in different programming languages for doing it. 
In C, We do it using printf(), in C++ we will use cout, and in Java, we generally use System.out.println. We all know, that in the android studio we have to code in a different way. Android has its own methods and components, we have to code the application using them. This is slightly different from normal programming. In today’s article we are going to learn about that how can we print to the console in Android Studio. 
The Logcat is a window in android studio, which displays system information when a garbage collection occurs and also the messages which you have added to your Log class. It displays the message in real-time. It also keeps the history of the messages. You can also learn more about the Logcat window from Here.
The log class is a pre-defined class in android studio which allows the developer to print messages in Logcat Window, which is the console for Android Studio. Every message is written using a log, Contains a special type or format that represents for what purpose the message is being written. 
Java
Kotlin
Log.d(tag, message);
Log.d(tag, message)
Above is the sample of a default code for printing something to the logcat. The d is the symbol here that the message is written for debugging the code. More symbols and types are below mentioned in the Log class. The priority of verbose is the lowest and the assert has the highest priority. Below is the list of types of messages in the log class in chronological order.
V (Verbose)
D (Debug)
I (Information)
W (Warning)
E (Error)
A (Assert)
Log class always takes two arguments, the tag, and the message. The tag is like an identifier of your message you can choose it according to preference and in place of messages, you have to type your log message.
Now, we are aware that in the android studio we have to use the Log Class to print something on the Logcat window which is the console for android. So, Let’s see a real-world implementation of this method called Logcat.
Step 1: Start a New Project in Android Studio or Open an existing project on which you want to work. Here is the guide to Starting a new project on Android Studio.
Step 2: Go to your Java or Kotlin file for the activity, and in your onCreate method write the log messages with help of the Log class. Below is our code that we have used in the MainActivity for Random number generation and Printing log messages. 
Java
package com.voicex.printlogmessages;  import androidx.appcompat.app.AppCompatActivity;  import android.os.Bundle;import android.util.Log;  import java.util.Random;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final int random = new Random().nextInt(5);        switch (random) {            case 0:                Log.i("NumberGenerated", "Function has generated zero.");                break;            case 1:                Log.i("NumberGenerated", "Function has generated one.");                break;            case 2:                Log.i("NumberGenerated", "Function has generated two.");                break;            case 3:                Log.i("NumberGenerated", "Function has generated three.");                break;            case 4:                Log.i("NumberGenerated", "Function has generated four.");                break;            case 5:                Log.i("NumberGenerated", "Function has generated five.");                break;        }    }}
Note: Choose your desired activity, for which you want to print on console. For example, here we are working on MainActivity and generating a random number, and printing it using conditional statements. You can do this or can do something similar to it. 
At bottom of the page, we will also share the GitHub repository of the application which we have made in this article. You can refer to that.
Step 3: Now try to build and run your android application, in the meantime also click on the Logcat button which will be on the bottom. Log messages will appear according to the condition cause here we have used the conditional statements.
In the logcat window, these are the buttons for many tasks:
Clear logcat: Clears the visible Logcat window
Scroll to the end: Take you to the end of the logcat window, where you can see the latest messages.
Up the stack trace and Down the stack trace: Navigates up and down the stack traces in the log
Use soft wraps: Enables the line wrapping and prevents the horizontal scrolling
Print: Print logcat messages on paper or save them as a PDF.
Restart: Clears the log and restarts it.
Logcat header: Open customization options for log messages
Screen capture: Captures the logcat window as an image
Screen record: Records the video up to 3 Minutes of logcat window.
You can select regex optionally, for using a regular expression search pattern. Then type something in the search field which you want to search. The search results will be displayed. If you want to store the search string in this session then press enter after typing the search string.
On the top right corner of the logcat window, you will see a filter button and will find three options:
Show only selected applications: Displays the messages generated by the app code only.No Filters: Apply no filtersEdit Filter Configurations: Modify your custom filter or create new filters
Show only selected applications: Displays the messages generated by the app code only.
No Filters: Apply no filters
Edit Filter Configurations: Modify your custom filter or create new filters
GitHub link as a Resource: Click Here
Android-Studio
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Android Listview in Java with Example
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
How to Add Image to Drawable Folder in Android Studio?
How to Change the Background Color After Clicking the Button in Android?
How to Retrieve Data from the Firebase Realtime Database in Android?
GridView in Android with Example
ImageView in Android with Example