Skip to content
New issue

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

proposal: log/slog: Provide shortfile option for AddSource field in slog.HandlerOptions #62403

Closed
NullpointerW opened this issue Aug 31, 2023 · 4 comments
Labels
Milestone

Comments

@NullpointerW
Copy link

add sourceFile fmt option such as Llongfile Lshortfile like src/log

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...
}
@gopherbot gopherbot added this to the Proposal milestone Aug 31, 2023
@seankhliao
Copy link
Member

This should be possible with the ReplaceAttr function, targeting slog.SourceKey, updating the slog.Source.File field

@NullpointerW
Copy link
Author

This should be possible with the ReplaceAttr function, targeting slog.SourceKey, updating the slog.Source.File field

yes,like i mentioned in the preceding: rewrite attr in ReplaceAttr func
but can we provide usual modifying attrs' option,like timeKey and sourceKey?

@seankhliao
Copy link
Member

I'm not sure why your example is so complex: https://go.dev/play/p/uWw30ZUBnmG

@NullpointerW
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants