Skip to content

Instantly share code, notes, and snippets.

@omegasoft7
Created January 14, 2015 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegasoft7/61a8e20272c4e1052004 to your computer and use it in GitHub Desktop.
Save omegasoft7/61a8e20272c4e1052004 to your computer and use it in GitHub Desktop.
This is best logger for Android EVER
public class Logger {
//Variables------------------------------------------------------------------------
private static boolean LOGGING_ENABLED = true;
private static final int STACK_TRACE_LEVELS_UP = 5;
private static String TAG = "YourTag";
//Variables------------------------------------------------------------------------
public static void logout(String message)
{
if (LOGGING_ENABLED)
{
// Log.v(TAG, getClassNameMethodNameAndLineNumber() + message);
if (message.length() > 3000) {
int length = message.length();
String str = message;
while (length > 3000) {
Log.w(TAG, getClassNameMethodNameAndLineNumber() + str.substring(0, 3000));
str = str.substring(3000);
length = str.length();
}
Log.w(TAG, getClassNameMethodNameAndLineNumber() + str);
} else {
Log.w(TAG, getClassNameMethodNameAndLineNumber() + message);
}
}
}
/**
* Get the current line number. Note, this will only work as called from
* this class as it has to go a predetermined number of steps up the stack
* trace. In this case 5.
*
* @author kvarela
* @return int - Current line number.
*/
private static int getLineNumber()
{
return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getLineNumber();
}
/**
* Get the current class name. Note, this will only work as called from this
* class as it has to go a predetermined number of steps up the stack trace.
* In this case 5.
*
* @author kvarela
* @return String - Current line number.
*/
private static String getClassName()
{
String fileName = Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getFileName();
// kvarela: Removing ".java" and returning class name
return fileName.substring(0, fileName.length() - 5);
}
/**
* Get the current method name. Note, this will only work as called from
* this class as it has to go a predetermined number of steps up the stack
* trace. In this case 5.
*
* @author kvarela
* @return String - Current line number.
*/
private static String getMethodName()
{
return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getMethodName();
}
/**
* Returns the class name, method name, and line number from the currently
* executing log call in the form <class_name>.<method_name>()-<line_number>
*
* @author kvarela
* @return String - String representing class name, method name, and line
* number.
*/
private static String getClassNameMethodNameAndLineNumber()
{
return "[" + getClassName() + "." + getMethodName() + "()-" + getLineNumber() + "]: ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment