log.Helper() doesn't seem to do anything
hahuang65 opened this issue · 2 comments
I've got this code in Go playground: https://go.dev/play/p/gXpVjfglCM4
package main
import (
"context"
"log/slog"
"os"
"github.com/charmbracelet/log"
)
func NewLogger() *slog.Logger {
handler := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: true,
})
return slog.New(handler)
}
func BaseLogger() *slog.Logger {
return NewLogger().With(slog.String("foo", "bar"))
}
func Info(ctx context.Context, msg string, args ...any) {
BaseLogger().InfoContext(ctx, msg, args...)
}
func main() {
Info(context.TODO(), "blah")
}
This reports the caller
INFO <sandbox1826453426/prog.go:23> blah foo=bar
which is this line
BaseLogger().InfoContext(ctx, msg, args...)
If I add log.Helper() into
func Info(...)`, the result is: https://go.dev/play/p/kTN8WZ7zZky
INFO <sandbox4271756627/prog.go:24> blah foo=bar
which is still BaseLogger().InfoContext(ctx, msg, args...)
.
I'd like it to be Info(context.TODO(), "blah")
, the line in func main()
.
Hi @hahuang65, Log Helper()
feature is specific to Log and not supported when using Log as a log/slog
handler. With that being said, if you wanna use Log's Helper()
feature, you need to call the logger instance Helper()
method not the one associated with the global logger log.Helper()
. Take this example for instance https://go.dev/play/p/WiyEGLYnQye
@aymanbagabas that makes sense, thank you for the explanation.