This is a nice trick to add a link to code, so it will be easy to jump to the code that issued the log.
With the following code, this call:
MyLogger.logWithLink("MyTag","param="+param);
Will result in:
07-26...012/com.myapp D/MyTag: MyFrag:onStart(param=3) (MyFrag.java:2366) // << logcat converts this to a link to source!
This is the code (inside a class called MyLogger):
static StringBuilder sb0 = new StringBuilder(); // reusable string object
public static void logWithLink(String TAG, Object param) {
StackTraceElement stack = Thread.currentThread().getStackTrace()[3];
sb0.setLength(0);
String c = stack.getFileName().substring(0, stack.getFileName().length() - 5); // removes the ".java"
sb0.append(c).append(":");
sb0.append(stack.getMethodName()).append('(');
if (param != null) {
sb0.append(param);
}
sb0.append(") ");
sb0.append(" (").append(stack.getFileName()).append(':').append(stack.getLineNumber()).append(')');
Log.d(TAG, sb0.toString());
}
This is a basic example, it can be easily extended to issue a link to the caller (hint: the stack will be [4] instead of [3]), and you can also add other relevant information.