- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Actually, getStackTrace doesn't work all that well with most logging frameworks, which expect you to be logging an exception if you want a stacktrace. The usual syntax therefore tends to be along the lines of log.debug(new Exception("log me")).
Admin
Someone didn't know one could
new Exception()
. Otherwise fairly reasonable.Admin
Fun fact: Java omits the stacktrace of a NullPointerException if they happen too often. So with this "pattern" you will sooner or later end up with log entries, that only contain the info about a NullPointerException without any stacktraces (I know that there is a system property that disables this function, but still...)
Admin
The JVM can also skip generating the stack trace for this NullPointerException if it's executing with the
-XX:-OmitStackTraceInFastThrow
option (though perhaps this application isn't using that option):https://stackoverflow.com/questions/58696093/when-does-jvm-start-to-omit-stack-traces
Admin
I have written logging functions myself, but at least I took a hint with the advantages of actually encapsulating the conditional logic 😅
What's more tricky is to use format strings and ensure that the string interpolation won't fail at runtime. In Python patterns like
can help with that to get bothazy evaluationand static checking, but it suffers from the verbosity of using the full variable names in the format string. I usually find
printf
style format strings (like in the standard logging library) orstr.format
with abbreviated keyword arguments more clear, but then the static checking goes out of the window.Other languages may not even have a good way of checking these things.
Of course, we don't do ourselves any favors in my at-work project with the pattern of
where the format string is externalized into a separate subroutine for, effectively, the sole purpose of providing a unique error code in order to not include a user-visible backtrace.
Admin
There's never a good answer to logging and tracing that doesn't clutter the code. But this one is a real gem of a mess.
AOP is about the only way to do this neatly, and that's a whole 'nuther kettle of fun fish to deal with.