We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add sourceFile fmt option such as Llongfile Lshortfile like src/log
Llongfile
Lshortfile
i want display short source code position on output like:
time=xxx level=xxx source=log_test.go:95 msg=hello,world!
i implemed a handler to retrieving the pc,and rewrite source attribute to the shortfile fmt :
type EnhanceHandler struct { slog.Handler calldepth int } func ReplaceSourceAttr(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.SourceKey { if src, ok := a.Value.Any().(*slog.Source); ok { shortPath := "" fullPath := src.File seps := strings.Split(fullPath, "/") shortPath += seps[len(seps)-1] shortPath += fmt.Sprintf(":%d", src.Line) a.Value = slog.StringValue(shortPath) } } return a } func (eh *EnhanceHandler) Handle(ctx context.Context, r slog.Record) error { var ( pc uintptr pcs [1]uintptr // skip slog.(l *Logger) log(...) =>depth+3 // skip runtime.Callers in *EnhanceHandler.Hanle =>depth+1 depth = 4 ) runtime.Callers(depth+eh.calldepth, pcs[:]) pc = pcs[0] r.PC = pc return eh.Handler.Handle(ctx, r) } func NewEnhanceHandler(h slog.Handler, calldepth int) slog.Handler { return &EnhanceHandler{h, calldepth} }
ues this solution can show file name element and line number :
opt := &slog.HandlerOptions{ AddSource: true, ReplaceAttr: ReplaceSourceAttr, } eh:=NewEnhanceHandler(slog.NewTextHandler(os.Stderr, opt), 0) logger := slog.New(eh) logger.Info("hello,world!")
output
time=2023-09-01T02:12:40.702+08:00 level=INFO source=log_test.go:95 msg=hello,world!
but i think we can provide a simple option to enable it,just like:
type HandlerOptions struct { // Other fields... AddSource bool ShortFile bool // New field for short file name option // Other fields... }
The text was updated successfully, but these errors were encountered:
This should be possible with the ReplaceAttr function, targeting slog.SourceKey, updating the slog.Source.File field
Sorry, something went wrong.
yes,like i mentioned in the preceding: rewrite attr in ReplaceAttr func but can we provide usual modifying attrs' option,like timeKey and sourceKey?
I'm not sure why your example is so complex: https://go.dev/play/p/uWw30ZUBnmG
all right,that actually simpler for sure,i can accept
No branches or pull requests
add sourceFile fmt option such as
Llongfile
Lshortfile
like src/logi want display short source code position on output like:
i implemed a handler to retrieving the pc,and rewrite source attribute to the shortfile fmt :
ues this solution can show file name element and line number :
output
but i think we can provide a simple option to enable it,just like:
The text was updated successfully, but these errors were encountered: