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

Control amount of padding #83

Open
kevinburke opened this issue Jun 21, 2016 · 5 comments
Open

Control amount of padding #83

kevinburke opened this issue Jun 21, 2016 · 5 comments

Comments

@kevinburke
Copy link
Collaborator

I'm trying to add HTTP logging to my application. The format I want is all keys and values, no Msg component.

The terminal format adds 40 chars of padding, regardless of whether a Msg is specified. It would be nice if you could omit this padding without having to reimplement a lot of the terminal display code.

@inconshreveable
Copy link
Owner

inconshreveable commented Jun 22, 2016

Yeah, we need a way to make the formatter more customizable. I like the idea of setting up functional options for the LogFmtFormat constructor and letting you pass those in. So the API might look like:

func LogFmtFormat(opts ...FormatOption)

And usage:

// default
fmtr  := log.LogFmtFormat()

// customized formatter
fmtr := log.LogFmtFormat(
    log.WithTimeFormat(func(time.Time) string {
        return "Hammer Time!"
    }),
    log.WithMessagePadding(10),
    log.WithTimelKey("time"),
    log.WithLevelKey(""), // empty string means omit
)

This would address #84 and some other issues we closed in the past.Maybe we could even share some of the options with the JSONFormatter. Thoughts?

@ChrisHines thoughts on this API? I think it would get us some flexibility in custom formatting we were always missing. I wonder if this is a better way to allow folks to define custom names for t, lvl, and msg keys instead of the RecordKeyNames that we eventually settled on

@ChrisHines
Copy link
Collaborator

@grahamking started down the path of functional options in #60 but abandoned it for a custom formatter the better fit his needs.

The log.WithTimeFormat approach is interesting because it doesn't assume you are using time.Format.

@inconshreveable
Copy link
Owner

interesting. @kevinburke or I might take a stab at resurrecting that approach. do you like the way that worked out for StdlibAdapterOption in go-kit/log?

@ChrisHines
Copy link
Collaborator

I don't use StdlibAdapter much and I didn't write it either, credit for that idea goes to @peterbourgon, but I do like how functional options work for this sort of thing.

@kishaningithub
Copy link

kishaningithub commented Jul 28, 2022

Instead of functional style options like above why not pass in an options struct?

In the struct the user can specify only the options they need and rest all is default. I have seen this approach in the Kafka go library

Examples

https://pkg.go.dev/github.com/segmentio/kafka-go?utm_source=godoc#Writer

https://github.com/segmentio/kafka-go/blob/v0.4.32/reader.go#L622

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

No branches or pull requests

4 participants