Skip to content

Instantly share code, notes, and snippets.

@gurpreetsingh2808
Last active April 8, 2017 08:39
Show Gist options
  • Save gurpreetsingh2808/74089a73a7836cc21fa4105648d6ac81 to your computer and use it in GitHub Desktop.
Save gurpreetsingh2808/74089a73a7836cc21fa4105648d6ac81 to your computer and use it in GitHub Desktop.
Custom logger for android
public class Logger {
private static final String TAG = Logger.class.getSimpleName();
private static Boolean flag = !AppUtils.isProductionBuild();
// To avoid printing stacktraces in debug builds and report caught exceptions in production/release build
public static void handleCaughtException(Exception ex) {
if (flag) {
ex.printStackTrace();
} else {
// Report the non-fatal errors to your Crash reporter
CrashReporter.report(new Error(ex.fillInStackTrace()));
}
}
public static void d(String statement) {
if (flag) {
Log.d(getTag(), getMessage(statement));
}
}
public static void e(String statement) {
if (flag) {
Log.e(getTag(), getMessage(statement));
}
}
public static void i(String statement) {
if (flag) {
Log.i(getTag(), getMessage(statement));
}
}
public static void v(String statement) {
if (flag) {
Log.v(getTag(), getMessage(statement));
}
}
public static void w(String statement) {
if (flag) {
Log.w(getTag(), getMessage(statement));
}
}
private static String getTag() {
StackTraceElement element = getElement();
return new StringBuilder(element.getClassName()).substring(element.getClassName().lastIndexOf(".") +1);
}
private static String getMessage(String msg) {
StackTraceElement element = getElement();
//String fileName = element.getFileName();
int lineNumber = element.getLineNumber();
String methodName = element.getMethodName();
return "line : " + lineNumber + ", method : " + methodName + "(), MSG : " + msg;
}
private static StackTraceElement getElement() {
// The number 4 below is the depth of the StackTraceElement array.
// Debug it with your code to get the correct details for your log.
return Thread.currentThread().getStackTrace()[4];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment