News for hackers. Stuff that matters.
Re:Show HN: Decorate Java stack traces with source code snip... by theanonymousone on Friday October 06, 2023 @04:57AM (#37788475)
One big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised lig function to also log the name of the method thst has emitted a log string.
Re:Show HN: Decorate Java stack traces with source code snip... by javanonymous on Friday October 06, 2023 @07:07AM (#37789189)
If I understand your use case, most loggers (like Logback) have the possiblity to automatically include the name of the method that called the log-statement.

Logback even supports including the call chain in the output, so you could include the parent method, grand parent method etc. This probably uses reflection, so it could have a negative impact for applications that need a high performance.

https://logback.qos.ch/manual/layouts.html

Re:Show HN: Decorate Java stack traces with source code snip... by theanonymousone on Friday October 06, 2023 @09:54AM (#37790831)
Thanks. Will take a look. I don't use logging frameworks in smaller projects.
Re:Show HN: Decorate Java stack traces with source code snip... by papercrane on Friday October 06, 2023 @08:14AM (#37789677)
If you're using Java 9+ there is an official API to do that. It's the StackWaker class.

https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/l...

Re:Show HN: Decorate Java stack traces with source code snip... by theanonymousone on Friday October 06, 2023 @09:53AM (#37790815)
Many thanks. Will look at it.