zap icon indicating copy to clipboard operation
zap copied to clipboard

Allow specifying user defined log level names

Open pmalek opened this issue 2 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe.

As a user of zap I'd like to define my own custom log level, let's call it TraceLevel. It's destined to be more verbose than Debug.

Because currently the log levels are checked against a known, concrete set of values https://github.com/uber-go/zap/blob/4451b47c5e4c4263828b042d6fb12d6d14c6ca2e/zapcore/level.go#L134-L156 when pretty printing the log level name I cannot define a name on my level and see it in the logs.

All I can get is fmt.Sprintf("LEVEL(%d)", l)

Describe the solution you'd like

Define an interface that user defined log levels can be checked against and if they implement it then use it to get the name of the log level.

If that fails then resort to fmt.Sprintf("LEVEL(%d)", l).

Describe alternatives you've considered

None exist to the best of my knowledge.

Is this a breaking change?

No.

pmalek avatar Apr 26 '23 12:04 pmalek

Internal Ref: GO-2011

JacobOaks avatar May 02 '23 18:05 JacobOaks

Can I work on this ? @JacobOaks

SoulPancake avatar May 06 '23 15:05 SoulPancake

Can I work on this ? @JacobOaks

Of course! Feel free to open a PR.

JacobOaks avatar May 08 '23 13:05 JacobOaks

Sorry I hopped late onto this thread, but I believe we shouldn't add this feature to Zap.

Allowing definitions of custom levels is a breaking change and we do not want that. See the discussions in https://github.com/uber-go/zap/issues/680.

Existing encoders and samplers could break due to such things. For example: https://github.com/uber-go/zap/issues/713 is an example of someone defining their own custom log level which broke the sampler.

sywhang avatar May 15 '23 18:05 sywhang