Let’s talk about a feature I’ve never used, or even knew existed… I mentioned a while back that the AddSource field of HandlerOptions controls whether the log output includes the source code position of the log call. But what if that log call is wrapped by a helper, obscuring the meaningful source position?
Wrapping output methods
The logger functions use reflection over the call stack to find the file name and line number of the logging call within the application. This can produce incorrect source information for functions that wrap slog. For instance, if you define this function in file mylog.go:
func Infof(logger *slog.Logger, format string, args ...any) { logger.Info(fmt.Sprintf(format, args...)) }and you call it like this in main.go:
Infof(slog.Default(), "hello, %s", "world")then slog will report the source file as mylog.go, not main.go.
A correct implementation of Infof will obtain the source location (pc) and pass it to NewRecord. The Infof function in the package-level example called “wrapping” demonstrates how to do this.
This section of the docs just explains the problem, in a bit more detail than I did… then points you to the Example for the solution. Let’s look at that Example. Follow the link for the full example, executable in your browser. Here I’ll just quote the most interesting bit:
var pcs [1]uintptr runtime.Callers(2, pcs[:]) // skip [Callers, Infof] r := slog.NewRecord(time.Now(), slog.LevelInfo, fmt.Sprintf(format, args...), pcs[0]) _ = logger.Handler().Handle(context.Background(), r)
What you see here is the “low-level” construction of a log record, which gives you fine-grained control over the details. The detail that matters here, is the caller’s stack frame.
What is a log Record? That’s the next topic we’ll discuss!