Skip to content

Instantly share code, notes, and snippets.

@stillie
Forked from shkschneider/Log
Last active April 29, 2019 10:04
Show Gist options
  • Save stillie/72ba8b5a43ba35ee805c1fecb739cc6e to your computer and use it in GitHub Desktop.
Save stillie/72ba8b5a43ba35ee805c1fecb739cc6e to your computer and use it in GitHub Desktop.
Android Log helper (automatic class, method and line) replacing android.util.Log
package me.shkschneider.skeleton.helper;
import android.text.TextUtils;
import android.util.Log;
/**
* Improved android.util.Log.
*
* Logs class and method automatically:
* [MyApplication onCreate():34] Hello, world!
* [MyClass myMethod():42] my log msg
*
* Install:
* $ find -name "*.java" -type f -exec sed -i 's/import android.util.Log/import me.shkschneider.skeleton.helper.Log/g' {} \;
*
* Better to use with adb -s com.example.myapp
*/
public class Log {
protected Log() {
// Empty
}
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;
// Used to identify the source of a log message.
// It usually identifies the class or activity where the log call occurs.
protected static String TAG = BuildConfig.APPLICATION_ID;
protected static void log(final int level, final String msg, final Throwable throwable) {
final StackTraceElement[] elements = new Throwable().getStackTrace();
String callerClassName = "?";
String callerMethodName = "?";
String callerLineNumber = "?";
if (elements.length >= 4) {
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;
}
callerLineNumber = String.valueOf(elements[2].getLineNumber());
}
final String stack = "[" + callerClassName + "." + callerMethodName + "():" + callerLineNumber + "]" + (TextUtils.isEmpty(msg) ? "" : " ");
switch (level) {
case VERBOSE:
android.util.Log.v(TAG, stack + msg, throwable);
break ;
case DEBUG:
android.util.Log.d(TAG, stack + msg, throwable);
break ;
case INFO:
android.util.Log.i(TAG, stack + msg, throwable);
break ;
case WARN:
android.util.Log.w(TAG, stack + msg, throwable);
break ;
case ERROR:
android.util.Log.e(TAG, stack + msg, throwable);
break ;
case WTF:
android.util.Log.wtf(TAG, stack + msg, throwable);
break ;
default:
break ;
}
}
public static void d(@NonNull final String msg) {
log(DEBUG, msg, null);
}
public static void d(final String msg, @NonNull final Throwable throwable) {
log(DEBUG, msg, throwable);
}
public static void v(@NonNull final String msg) {
log(VERBOSE, msg, null);
}
public static void v(final String msg, @NonNull final Throwable throwable) {
log(VERBOSE, msg, throwable);
}
public static void i(@NonNull final String msg) {
log(INFO, msg, null);
}
public static void i(final String msg, @NonNull final Throwable throwable) {
log(INFO, msg, throwable);
}
public static void w(@NonNull final String msg) {
log(WARN, msg, null);
}
public static void w(final String msg, @NonNull final Throwable throwable) {
log(WARN, msg, throwable);
}
public static void e(@NonNull final String msg) {
log(ERROR, msg, null);
}
public static void e(final String msg, @NonNull final Throwable throwable) {
log(ERROR, msg, throwable);
}
public static void wtf(@NonNull final String msg) {
log(WTF, msg, null);
}
public static void wtf(final String msg, @NonNull final Throwable throwable) {
log(WTF, msg, throwable);
}
@Deprecated
public static void wtf(@NonNull final Throwable throwable) {
log(WTF, null, throwable);
}
}
@stillie
Copy link
Author

stillie commented Apr 29, 2019

Changed the TAG to include the application package as well display the Class that the log is being called in. It currently said Activity.performClick() now it says MainActivity.performClick()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment