Java tutorial
//package com.java2s; import android.text.TextUtils; import android.util.Log; public class Main { private static boolean isDebug = true; static String mTag = "LogUtils"; public static void showLog(String tag, String msg) { if (isDebug && !TextUtils.isEmpty(msg)) { if (TextUtils.isEmpty(tag)) tag = mTag; StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace(); int currentIndex = -1; for (int i = 0; i < stackTraceElement.length; i++) { if (stackTraceElement[i].getMethodName().compareTo("showLog") == 0) { currentIndex = i + 1; break; } } if (currentIndex >= 0) { String fullClassName = stackTraceElement[currentIndex].getClassName(); String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); String methodName = stackTraceElement[currentIndex].getMethodName(); String lineNumber = String.valueOf(stackTraceElement[currentIndex].getLineNumber()); Log.i(tag, msg + "\n ---->at " + className + "." + methodName + "(" + className + ".java:" + lineNumber + ")"); } else { Log.i(tag, msg); } } } public static void i(String msg) { if (isDebug) { Log.i(mTag, msg); } } public static void i(String tag, String msg) { if (isDebug) { if (tag == null || "".equalsIgnoreCase(tag.trim())) { tag = mTag; } Log.i(tag, msg); } } }