Skip to content

Instantly share code, notes, and snippets.

@saket
Forked from shkschneider/Log
Last active August 29, 2015 14:15
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 saket/642cfd64d72dc516dcc3 to your computer and use it in GitHub Desktop.
Save saket/642cfd64d72dc516dcc3 to your computer and use it in GitHub Desktop.
package com.example.myapp;
import android.text.TextUtils;
import android.util.Log;
/**
* Improved android.util.Log.
*
* Logs class and method automatically:
* [MyApplication onCreate()] Hello, world!
* [MyClass myMethod()] my log msg
*
* Better to use with adb -s com.example.myapp
*/
public class LogHelper {
private static final int VERBOSE = Log.VERBOSE;
private static final int DEBUG = Log.DEBUG;
private static final int INFO = Log.INFO;
private static final int WARN = Log.WARN;
private static final int ERROR = Log.ERROR;
private static final int WTF = Log.ASSERT;
private static String TAG = MainApplication.CONTEXT.getPackageName();
private static int LEVEL = (BuildConfig.DEBUG ? VERBOSE : WARN);
private static void log(final int level, final String msg) {
if (level < LEVEL) {
return ;
}
if (TextUtils.isEmpty(msg)) {
return ;
}
// Uses StackTrace to build the log tag
final StackTraceElement[] elements = new Throwable().getStackTrace();
String callerClassName = "?";
String callerMethodName = "?";
if (elements.length >= 3) {
callerClassName = elements[2].getClassName();
callerClassName = callerClassName.substring(callerClassName.lastIndexOf('.') + 1);
if (callerClassName.indexOf("$") > 0) {
callerClassName = callerClassName.substring(0, callerClassName.indexOf("$"));
}
callerMethodName = elements[2].getMethodName();
callerMethodName = callerMethodName.substring(callerMethodName.lastIndexOf('_') + 1);
if (callerMethodName.equals("<init>")) {
callerMethodName = callerClassName;
}
}
final String stack = callerClassName + " " + callerMethodName + "()";
switch (level) {
case VERBOSE: Log.v(TAG, "[" + stack + "] " + msg); break ;
case DEBUG: Log.d(TAG, "[" + stack + "] " + msg); break ;
case INFO: Log.i(TAG, "[" + stack + "] " + msg); break ;
case WARN: Log.w(TAG, "[" + stack + "] " + msg); break ;
case ERROR: Log.e(TAG, "[" + stack + "] " + msg); break ;
case WTF: Log.wtf(TAG, "[" + stack + "] " + msg); break ;
default: break ;
}
}
public static void debug(final String msg) {
LogHelper.log(DEBUG, msg);
}
public static void verbose(final String msg) {
LogHelper.log(VERBOSE, msg);
}
public static void info(final String msg) {
LogHelper.log(INFO, msg);
}
public static void warning(final String msg) {
LogHelper.log(WARN, msg);
}
public static void error(final String msg) {
LogHelper.log(ERROR, msg);
}
public static void wtf(final String msg) {
LogHelper.log(WTF, msg);
}
public static void wtf(final Throwable throwable) {
if (throwable == null) {
return ;
}
LogHelper.log(WTF, throwable.getClass().getName() + ": " + throwable.getMessage());
if (BuildConfig.DEBUG) {
throwable.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment