[Feature Request] Native Support for logfmt
Context
We use one of 2 formats for logging
- json
- logfmt
I understand console log writer is something that can probably be customized so that the timestamp and level is also coming in logfmt format like tz= level= and the others (msg and fields) already in logfmt.
but the caveat is this works on top of the json output by parsing the same using go's json package
https://github.com/rs/zerolog/blob/fe931004ff029af77fee04d5f895c83643aaca78/console.go#L103
This had to be the case because the console writer has registered itself as the writer
Proposal
Just like you create json logs with zero allocation, can we also do logfmt logs with zero allocation?
The console writer is a debug too to be used in dev and is not supposed to be used in production. Zerolog is meant to be used as a json logger. To support logfmt, a new backend would have to be written using build tags similar to cbor support.
I feel adding logfmt would be a great value add. If you agree with this idea, I can start to work on a PR
Sure go ahead.
@rs I see the encoder is a package level variable. I feel making this part of the log instance would make people change the logging format at runtime instead of at compile time.
Any reason why the encoder instance is a package level variable?
Mainly for performance
Would it be possible to export the encoder interface and allow setting the package level enc so users wanting different encodings could write their own adapters and override the default json at runtime?
The encoder is not an interface for the best performance and inlinability. The goal of this library is to stay small, simple to use with no compromise on performance.
Hi @rs ! You closed this pr as completed, but I could not find any suitable implementation. Did I miss something, or was this feature request rejected? I'm asking because we are currently using zerolog, and want to use logfmt. Do we need to use another logger to achieve this? thanks in advance!
It's a won't do, unless someone want to contribute an encoder. Keep in mind that it will be a compile time setting. Zerolog does not support dynamic encoders.
ok, thanks for the info!