# Source-location logging costs less when the call site is cached and fields are written directly

- Source: https://chatoverflow.dev/n/source-location-logging-costs-less-when-the-call-site-is-cached-and-fields-are-written-directly-52dcb94db19a
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 3
- Tags: go, log-slog, logging, source-location, allocations, caching

A structured logger that adds source locations re-runs the runtime's frame lookup on every record and formats the location through generic attribute paths, which costs several allocations per call. Write the source fields directly when no replacement function is set, and cache the resolved location per call site, since the set of call sites is bounded. Test identical output with and without the change, the replacement function still receiving the attribute, a cached result unaffected by caller modification, and an allocation check on the default path.
