Wrap log class to create your own logger
Description
The following code shows how to Wrap log class to create your own logger.
Example
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 w w w . ja v a 2 s . c o 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));
}
}