Home » Android » core » Android Logging Example

About Archi Gupta

Archi Gupta
Archi is a passionate Software developer who can think out of the box. She began her career as an Android developer with one start up and since then developed interest in this field. In her leisure time, she loves solving coding problems and building mobile applications. Apart from this she holds her Master degree in Computer Science

Android Logging Example

1. Introduction

Being an Android developer, all of you might have heard of logging. Well if not, this tutorial will guide you with the concept usages and its implementation. Let us start with the quick introduction of the concept about what it is and why it is used.

LOG in a simple language can be defined as ”record of events”. In Android, Logging works as a diagnostic technique used by developers. It basically provides an insight of what’s happening in your application. We can write Log messages in the code with the help of LOG class and the messages get displayed in the Logcat window on running the application.

 
With the help of LOG class you can write your custom log messages in the application. The log methods used are:

  • Log.e display error messages.
  • Log.w display warnings.
  • Log.i display information messages/ expected log messages.
  • Log.d display debug messages.
  • Log.v display all log messages. (least used as it records more info than usual logging)

Log method has 2 or 3 arguments :

Log.d(String tag, String msg)
Log.d(String tag, String msg, Throwable tr)

  1. A String called TAG. You can define a global static string in the application and hence you can use log filters to limit the log output to specific data. A TAG can be any string that you find helpful (eg: name of the class).
  2. Second argument is msg. It is the main content of the log message.
  3. A third argument, of the class Throwable, which represents an exception or error being thrown.

Following this, you will learn how to write log messages and check log messages for diagnostic purposes with the help of an example.

2. Android Logging Example

In this example, we have used various log methods and you will see how it helps in debugging the application.

IDE used – Android Studio 3.0 (You can use Eclipse as well, but Android studio works well with Gradle. Fore more info about Gradle read here –  https://developer.android.com/studio/build/index.html)

Download Android Studio 3.0 :  https://developer.android.com/studio/index.html

2.1 Open a New / Existing Android application

Create a New Project if you have not created one before.

Create a new project

2.2 Write Log messages

Alternatively use alt+enter while hovering over Log method, this will import the Log package.

LoggingExampleActivity.java

import android.util.Log;

public class LoggingExampleActivity extends AppCompatActivity {
private static final String TAG = "Logging Example";
private Button button;

@Override 
 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_logging_example);
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 button = (Button)findViewById(R.id.button);Log.d(TAG,"On Create" );
 try {
 //
 } catch (Exception e) {
 Log.e(TAG, "Received an exception " + e.getMessage() );
 }
 
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Toast.makeText(getApplication(), "Button Clicked", Toast.LENGTH_SHORT).show();
 Log.d(TAG,"On Create" );
 Log.i("Info log","Button Clicked" ); } });
 }

2.3 Run the application

Build and run your app on a device or an emulator. You should be able to see all the log messages of your application in the Logcat console. If you don’t, then go to View > Tool Windows > Logcat or click on Logcat in the tool window bar. By default Logcat shows all the messages of your application. To change this, you can filter Logcat messages by using buttons on the Logcat window sidebar.

Clear Logcat  will clear the visible log.

Scroll to the end  will let you see the latest log messages.

Up the stack trace  and Down the stack trace for navigating up and down the stack traces.

Soft wraps  will enable line wrapping.

Restart  will clear the log and restart it.

As we placed the Log methods in the OnCreate () method, so as soon as this method is triggered ,we will see the log messages. (Highlighted manually)

Log message appeared when OnCreate() method is called

To check for the second log message i.e. Log.i , click the button. On clicking the button, OnClick() method will be called and as a result, you can see the log message with Info log(TAG) : Button Clicked(msg) in logcat.

Log message appeared on clicking the button

2.4 Filtering the Logs

As you can see, there is a lot of log data from different sources. To make it simple, you can filter the logs. Let’s explore the different filter options available in the Logcat panel.

2.4.1 Source

The dropdown allows you to select the device whose log messages you want to see.

Filtering logs by selecting the source(device)

2.4.2 Application

This dropdown allows you to select which application’s log messages you want to view.

Filtering the logs by selecting the Application

2.3.3 Log Levels

In this screenshot, log methods are ordered from low to high (Verbose has the lowest priority).

Filtering via log level

On selecting a particular level, you can see log messages of that level and beyond. For example, on selecting Info, you can see Log.i messages and higher priority messages.

Selecting Info from log levels displays only Info log mesages

On selecting Debug, you can see Log.d and  Log.i messages as Debug has lower priority than Info.

Selecting Debug from log levels, displays both Info and Debug messages

2.4.4 Search

In the search field, you can search for particular Log message by typing in the keyword or TAG you used in the Log statement. It helps when you are not able to locate your Log message and check if the particular piece of code ever ran. For example, by putting TAG in the search field, you can actually see your Log message.

Filtering the logs via searching through TAG

3. Conclusion

We have seen how Logcat works and by using this, how we can easily check the workflow of the application. Despite of all the benefits, its excessive use can decrease application performance. It is recommended to use Debug and Verbose only during development and not in the Released version.

4. Download the Source Code

This was an example of Android Logging with Filtering techniques.

Download
You can download the full source code of this example here: Logging Example 
(+1 rating, 1 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of