Created
November 17, 2013 17:36
-
-
Save rnkoaa/7515888 to your computer and use it in GitHub Desktop.
A logging facade which is helpful in debugging android applications. Anytime a log is called, it displays the class name as well as the method name and line number. this can help immensely when debug failures in our application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
/** | |
* @Author: Richard Amoako-Agyei | |
* @Date: 11/17/2013 | |
*/ | |
public class Logger { | |
private static Logger instance = null; | |
private final Class<?> clzz; | |
private static final String TAG = ""; //Add the Tag for easy filtering. | |
public static class BuildConfig { | |
public static final boolean RELEASE = false; | |
public static final boolean DEBUG = true; | |
} | |
public static enum Level { | |
ALL, INFO, DEBUG, ERROR, VERBOSE, OFF, FATAL, WARN | |
} | |
public Logger(Class<?> clzz) { | |
this.clzz = clzz; | |
} | |
public static Logger getLogger(Class<?> clzz) { | |
if (instance == null) | |
instance = new Logger(clzz); | |
return instance; | |
} | |
public void debug(String format, Object... args) { | |
if (BuildConfig.DEBUG) | |
writeLog(TAG, Level.DEBUG, String.format(format, args)); | |
} | |
public void info(String format, Object... args) { | |
System.out.println(TAG + " " + Level.INFO.name() + " - " + String.format(format, args)); | |
} | |
public void fatal(String format, Object... args) { | |
System.out.println(TAG + " " + Level.FATAL.name() + " - " + String.format(format, args)); | |
} | |
public void warn(String format, Object... args) { | |
System.out.println(TAG + " " + Level.WARN.name() + " - " + String.format(format, args)); | |
} | |
public void error(String format, Object... args) { | |
System.out.println(TAG + " " + Level.ERROR.name() + " - " + String.format(format, args)); | |
} | |
public void debug(String message) { | |
if (BuildConfig.DEBUG) | |
writeLog(TAG, Level.DEBUG, message); | |
} | |
public void info(String message) { | |
System.out.println(TAG + " " + Level.INFO.name() + " - " + message); | |
} | |
public void fatal(String message) { | |
System.out.println(TAG + " " + Level.FATAL.name() + " - " + message); | |
} | |
public void warn(String message) { | |
System.out.println(TAG + " " + Level.WARN.name() + " - " + message); | |
} | |
public void error(String message) { | |
writeLog(TAG, Level.ERROR, message); | |
} | |
private void writeLog(String tag, Level logLevel, String message) { | |
StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace(); | |
int lineNumber = stackTraceElement[4].getLineNumber(); | |
String methodName = stackTraceElement[4].getMethodName(); | |
String logMessage = clzz.getSimpleName() + ":" + String.format("%s[%d] ", methodName, lineNumber) + message; | |
switch (logLevel) { | |
case DEBUG: | |
Log.d(tag, logMessage); | |
break; | |
case INFO: | |
Log.i(tag, logMessage); | |
break; | |
case ERROR: | |
Log.e(tag, logMessage); | |
break; | |
case FATAL: | |
Log.wtf(tag, logMessage); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment