Skip to content

Instantly share code, notes, and snippets.

@scottyab
Forked from chrisjenx/QLog.java
Created March 4, 2016 10:39
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 scottyab/a7954d57b41f479948b0 to your computer and use it in GitHub Desktop.
Save scottyab/a7954d57b41f479948b0 to your computer and use it in GitHub Desktop.
QLog
package com.bizzby.utils;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
public class QLog
{
public static final int NONE = 0;
public static final int ERRORS_ONLY = 1;
public static final int ERRORS_WARNINGS = 2;
public static final int ERRORS_WARNINGS_INFO = 3;
public static final int ERRORS_WARNINGS_INFO_DEBUG = 4;
public static final int ALL = 5;
public static int LOGGING_LEVEL = 0;
/*
* For filtering app specific output
*/
private static final String TAG = "BIZZBY";
/*
* So any important logs can be outputted in non filtered output also
*/
private static final String TAG_GENERAL_OUTPUT = "Log";
static
{
i("Log class reloaded");
}
/**
* @param obj
* @param cause
* The exception which caused this error, may not be null
*/
public static void e(final Object obj, final Throwable cause)
{
if (LOGGING_LEVEL > NONE)
{
Log.e(TAG, getTrace() + String.valueOf(obj));
Log.e(TAG, getThrowableTrace(cause));
// Log.e(TAG_GENERAL_OUTPUT, getTrace() + String.valueOf(obj));
// Log.e(TAG_GENERAL_OUTPUT, getThrowableTrace(cause));
}
}
public static void e(final Object obj)
{
if (LOGGING_LEVEL > NONE)
{
Log.e(TAG, getTrace() + String.valueOf(obj));
// Log.e(TAG_GENERAL_OUTPUT, getTrace() + String.valueOf(obj));
}
}
public static void w(final Object obj, final Throwable cause)
{
if (LOGGING_LEVEL > ERRORS_ONLY)
{
Log.w(TAG, getTrace() + String.valueOf(obj));
Log.w(TAG, getThrowableTrace(cause));
// Log.w(TAG_GENERAL_OUTPUT, getTrace() + String.valueOf(obj));
// Log.w(TAG_GENERAL_OUTPUT, getThrowableTrace(cause));
}
}
public static void w(final Object obj)
{
if (LOGGING_LEVEL > ERRORS_ONLY)
{
Log.w(TAG, getTrace() + String.valueOf(obj));
// Log.w(TAG_GENERAL_OUTPUT, getTrace() + String.valueOf(obj));
}
}
public static void i(final Object obj)
{
if (LOGGING_LEVEL > ERRORS_WARNINGS)
{
Log.i(TAG, getTrace() + String.valueOf(obj));
}
}
public static void d(final Object obj)
{
if (LOGGING_LEVEL > ERRORS_WARNINGS_INFO)
{
Log.d(TAG, getTrace() + String.valueOf(obj));
}
}
public static void v(final Object obj)
{
if (LOGGING_LEVEL > ERRORS_WARNINGS_INFO_DEBUG)
{
Log.v(TAG, getTrace() + String.valueOf(obj));
}
}
private static String getThrowableTrace(final Throwable thr)
{
if(thr == null) return "Throwable was Null";
final StringWriter b = new StringWriter();
thr.printStackTrace(new PrintWriter(b));
return b.toString();
}
private static String getTrace()
{
int depth = 2;
Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();
String callerMethodName = elements[depth].getMethodName();
String callerClassPath = elements[depth].getClassName();
int lineNo = elements[depth].getLineNumber();
int i = callerClassPath.lastIndexOf('.');
String callerClassName = callerClassPath.substring(i + 1);
return callerClassName + ": " + callerMethodName + "() [" + lineNo + "] - ";
}
/**
* Prints the stack trace to mubaloo log and standard log
*
* @param e
*/
public static void handleException(final Exception e)
{
QLog.e(e.toString());
e.printStackTrace();
}
private QLog()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment