File name, method name, and line number constants

Ralph Goers rgoers at
Mon Apr 11 08:43:01 UTC 2022

Logging frameworks such as Log4j 2 provide the ability to include the caller’s line number, method name, class name, file name, and possibly module name in every log event. Currently, collecting this data requires a call to StackWalker for every event logged. This is very expensive. Since this information could be determined at compile time it seems logical that the compiler could inject it where requested. For example, a logging call like:

logger.debug(System.currentStackTraceElement(), “log message”);

Could have the call to currentStackTraceElement() replaced by the compiler with a constant StackTraceElement containing the correct information. This would result in zero overhead at runtime instead of expensive call we have to perform now. It would have the drawback that uses would have to add that to the method invocation but that is a small price to pay for the efficiency gain.

Cound this, or something like it, be added?


More information about the compiler-dev mailing list