Last active
August 29, 2015 14:03
-
-
Save vaibhavpandeyvpz/503d7b6b16d311c4c183 to your computer and use it in GitHub Desktop.
Intelligent Logging Class For Android Applications
This file contains 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
package com.vaibhavpandey.utility; | |
import com.vaibhavpandey.demo.BuildConfig; | |
import android.text.TextUtils; | |
import android.util.Log; | |
public final class Lograt { | |
/** | |
* Predefined log-level constants | |
*/ | |
public static final int DEBUG = 0; | |
public static final int ERROR = 1; | |
public static final int INFO = 2; | |
public static final int VERBOSE = 3; | |
public static final int WARNING = 4; | |
/** | |
* Logs the message with DEBUG priority | |
* | |
* @param Text or message to printed to Logcat | |
*/ | |
public static void d(String message) { | |
l(DEBUG, message); | |
} | |
/** | |
* Logs the exception with ERROR priority | |
* | |
* @param Caught exception to be logged | |
*/ | |
public static void e(Exception exception) { | |
if (exception == null) | |
return; | |
l(ERROR, Log.getStackTraceString(exception)); | |
} | |
/** | |
* Logs the message with ERROR priority | |
* | |
* @param Text or message to printed to Logcat | |
*/ | |
public static void e(String message) { | |
l(ERROR, message); | |
} | |
/** | |
* Logs the message & Exception with DEBUG priority | |
* | |
* @param Text or message to printed to Logcat | |
* @param Caught exception to be logged | |
*/ | |
public static void e(String message, Exception exception) { | |
l(ERROR, message); | |
e(exception); | |
} | |
/** | |
* Gets the callee's element to build TAG | |
*/ | |
private static StackTraceElement element() { | |
StackTraceElement caller = null; | |
StackTraceElement[] elements = Thread.currentThread().getStackTrace(); | |
for (int i = 1; i < elements.length; i++) { | |
StackTraceElement element = elements[i]; | |
if (!element.getClassName().equals(Lograt.class.getName())) { | |
if (caller == null) | |
caller = element; | |
else if (!caller.getClassName().equals(element.getClassName())) | |
return element; | |
} | |
} | |
return null; | |
} | |
/** | |
* Logs the exception with INFO priority | |
* | |
* @param Caught exception to be logged | |
*/ | |
public static void i(String message) { | |
l(INFO, message); | |
} | |
/** | |
* Logs the exception with VERBOSE priority | |
* | |
* @param Caught exception to be logged | |
*/ | |
public static void v(String message) { | |
l(VERBOSE, message); | |
} | |
/** | |
* Logs the exception with WARNING priority | |
* | |
* @param Caught exception to be logged | |
*/ | |
public static void w(String message) { | |
l(WARNING, message); | |
} | |
/** | |
* Logs the exception with ERROR priority | |
* | |
* @param Severity level for log-message | |
* @param Caught exception to be logged | |
*/ | |
public static void l(int level, String message) { | |
if (!BuildConfig.DEBUG) | |
return; | |
StackTraceElement element = element(); | |
if (element == null) | |
return; | |
String detail = element.getMethodName(); | |
detail += (" (" + element.getLineNumber() + ')'); | |
if (!TextUtils.isEmpty(message)) | |
detail += " -> " + message; | |
String[] descriptor = element.getClassName().split("\\.(?=[^\\.]+$)"); | |
String tag = descriptor[0] + "/." + descriptor[1]; | |
switch (level) { | |
case DEBUG: | |
Log.d(tag, detail); | |
break; | |
case ERROR: | |
Log.e(tag, detail); | |
break; | |
case INFO: | |
Log.i(tag, detail); | |
break; | |
case WARNING: | |
Log.w(tag, detail); | |
break; | |
default: | |
Log.v(tag, detail); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment