zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

[Feature Request] Native Support for logfmt

Open kishaningithub opened this issue 4 years ago • 7 comments

Context

We use one of 2 formats for logging

  1. json
  2. 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?

kishaningithub avatar Aug 21 '21 00:08 kishaningithub

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.

rs avatar Aug 21 '21 08:08 rs

I feel adding logfmt would be a great value add. If you agree with this idea, I can start to work on a PR

kishaningithub avatar Aug 22 '21 05:08 kishaningithub

Sure go ahead.

rs avatar Aug 22 '21 10:08 rs

@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?

kishaningithub avatar Sep 03 '21 04:09 kishaningithub

Mainly for performance

rs avatar Sep 03 '21 07:09 rs

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?

scarlson avatar Sep 13 '21 15:09 scarlson

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.

rs avatar Sep 13 '21 15:09 rs

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!

tmstff avatar Mar 21 '24 12:03 tmstff

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.

rs avatar Mar 21 '24 13:03 rs

ok, thanks for the info!

tmstff avatar Mar 21 '24 14:03 tmstff