How to Debug an Android Application using LogCat – Part II

In this blog, lets create a basic application that allows us to Debug, before and after publishing too. If you don’t know what is debugging and how to do?, please read my part-I of this blog.

Step 1: Create a new project File -> Android Project. On activity_main.xml copy paste this code:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin">

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="172dp"
 android:text="Get Result" />

<EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="25dp"
 android:ems="10"
 android:hint="Type password"
 android:inputType="textPassword" >

<requestFocus />
 </EditText>

<EditText
 android:id="@+id/editText2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="37dp"
 android:ems="10"
 android:hint="Retype password"
 android:inputType="textPassword" />

</RelativeLayout>

Step 2. Create a new class, LoggerConfig.java and copy paste this code:


public class LoggerConfig {

public static final boolean DEBUGGING_ON = true; // if true application is
 // debugging by
 // developer else user
 // is using this app and
 // logging take place on
 // his mobile
 public static final String LOG = "com.geeks.gallery";//TAG for the app to filter
}

Step 3. Create a new class, Logger.java and copy paste this code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;

import android.os.Environment;

public class Logger {
 public static final String APP_ID = "geeks.gallery";//it's just to identify purpose only
 public static String logDir = "/getlogfromuser";//file location
 public static String logFileName = "/log.txt";//file name

 public static void log(String message) {
 if(!LoggerConfig.DEBUGGING_ON){
 writeToFile(message);
 }
 }

 private static void writeToFile(String message) {
 try {
 File sdCard = Environment.getExternalStorageDirectory();
 File dir = new File(sdCard.getAbsolutePath() + logDir);
 dir.mkdirs();
 File file = new File(dir, logFileName);
 PrintWriter writer = new PrintWriter(new BufferedWriter(
 new FileWriter(file, true), 8 * 1024));
 writer.println(APP_ID + " " + new Date().toString() + " : "
 + message);
 writer.flush();
 writer.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

Step 4. Now open your MainActivity.java and copy paste this code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
 EditText edt1, edt2;
 Button btn;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 edt1 = (EditText) findViewById(R.id.editText1);
 edt2 = (EditText) findViewById(R.id.editText2);
 btn = (Button) findViewById(R.id.button1);

 btn.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub

 if (edt1.getText().toString().length() == 0) {

 saveLogInfo("Password field can't be empty");

 } else if (edt2.getText().toString().length() == 0) {
 saveLogInfo("Please retype password");
 } else if (edt1.getText().toString()
 .equals(edt2.getText().toString())) {
 saveLogInfo("Success, Password matched");
 } else {

 saveLogInfo("Passwords doesn't match");
 }

 }
 });

 }

 //method which input log value either to logcat for debug or
//append to log.txt file when user using it
 private void saveLogInfo(String logValue) {
 if (LoggerConfig.DEBUGGING_ON) {
 Log.d(LoggerConfig.LOG, logValue);
 Toast.makeText(getApplicationContext(), logValue,
 Toast.LENGTH_SHORT).show();
 } else {
 Logger.log(logValue);
 Toast.makeText(getApplicationContext(), logValue,
 Toast.LENGTH_SHORT).show();
 }
 }
}

Step 5. Now open your AndroidManifest.xml file and add this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

ScreenShots:

Testing:

Screenshot_2014-12-21-15-58-37 Screenshot_2014-12-21-16-00-47 Screenshot_2014-12-21-16-01-03

 

From user side:

Screenshot_2014-12-21-16-02-02 Screenshot_2014-12-21-16-02-07 Screenshot_2014-12-21-16-02-22

While developing by developer:

degubbing

Tips to Remember always:

  1. While Developing an Application, developers used to log most of thing to check whether everything works correctly, but when the application is developed and published, its bad thing to show all logs to user. That’s why i have created a LoggerConfig.class with static final Debugging_On Boolean variable which helps to switch easily from developer to user mode.
  2. Logger.class helps to write/append all Logs into the log.txt file.
  3. Everything seems to works fine, now what is the use of creating log file when the developer can’t able to read it. OK, this seems to interesting question. If you read my before blog on creating setting screen, you’ll find an option there to feedback developers. This is our key to know what problems, user are facing it up. Just attach this log.txt file along with the email.

 

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

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