Is there any way to get Java or Logback to give my the object id (or address or whatever) in stack traces and log calls. In other words, instead of this:
com.example.MyObject
in my stack traces I want this:
com.example.MyObject@123456
And for logging, I want this:
LOG.debug("A message");
to act like this:
LOG.debug(this + ": A message");
I can’t see a way to do it though, because both Logback and java itself seem to use StackTraceElements, and those don’t record this information.
For bonus points, how is Object.toString() implemented in dalvik? The generic java docs say it is toHex(Object.hashCode()) but I tested that and it doesn’t match.
As you note, stack traces per se don’t contain references to the
thisin each frame. That info is available, though, to a debugger. There is probably a hacky way to get the info you want, but it’d be ugly, slow, and probably flaky. Honestly, I wouldn’t bother.Here’s (something like) tip-of-tree of
Object.java: https://android.googlesource.com/platform/libcore/+/bcf0a81a927992883f0cb49c1c945141d1261b8b/luni/src/main/java/java/lang/Object.javaFrom there:
Note that this is the base implementation in the class
Object, but it is commonly overridden in subclasses. This may explain whatever difference you may have seen.I hope this helps.