public static String formatWithStackInfo(String message) { /** * ex.getStackTrace()[0] is this formatWithStackInfo() method ex.getStackTrace()[1] * is your method where you call this method */ StackTraceElement ste = new Throwable().getStackTrace()[1]; String callerClassName = ste.getClassName(); int callerLineNumber = ste.getLineNumber(); String methodName = ste.getMethodName(); callerClassName = callerClassName.substring(callerClassName .lastIndexOf(".") + 1); return "[" + callerClassName +"." + methodName + ":" + callerLineNumber + "]" + message; }