Android Tutorial - Android Log Message








The following code shows how to Log message.

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
/*from   w w  w . j av  a2  s . c  om*/
public class MainActivity extends Activity {

  private static final String LOG_TAG = "MainActivity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.d(LOG_TAG, "Log should be deleted");
  }
}




Note

The code above uses the Log.d method to log debug information.

Wrap log class to create your own logger

The following code shows how to Wrap log class to create your own logger.

Layout activity_main.xml file

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

Java code

package com.java2s.myapplication3.app;
//from  ww w .j a  v  a 2s.co  m
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Logger.d("Activity Created");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Logger.d("Activity Resume at %d", System.currentTimeMillis());
        Logger.i("It is now %d", System.currentTimeMillis());
    }

    @Override
    protected void onPause() {
        super.onPause();
        Logger.d("Activity Pause at %d", System.currentTimeMillis());
        Logger.w("on pause");
    }
}
class Logger {
    private static final String LOGTAG = "AndroidRecipes";

    private static String getLogString(String format, Object... args) {
        if(args.length == 0) {
            return format;
        }
        return String.format(format, args);
    }
    public static void e(String format, Object... args) {
        Log.e(LOGTAG, getLogString(format, args));
    }

    public static void w(String format, Object... args) {
        Log.w(LOGTAG, getLogString(format, args));
    }

    public static void w(Throwable throwable) {
        Log.w(LOGTAG, throwable);
    }

    public static void i(String format, Object... args) {
        Log.i(LOGTAG, getLogString(format, args));
    }
    public static void d(String format, Object... args) {
        if(!BuildConfig.DEBUG)
            return;
        Log.d(LOGTAG, getLogString(format, args));
    }

    public static void v(String format, Object... args) {
        if(!BuildConfig.DEBUG)
            return;

        Log.v(LOGTAG, getLogString(format, args));
    }
}
null