rs / zerolog

Zero Allocation JSON Logger
MIT License
10.4k stars 567 forks source link

inconsistent semantics on type "Logger" #563

Open mrkagelui opened 1 year ago

mrkagelui commented 1 year ago

Hi, currently Logger uses a mix of value and pointer semantics, e.g., WithContext has a value receiver, Fatal, Err etc have pointer receivers, why is this? sorry if this has been brought up before

nikicat commented 1 year ago

I'm very curious too. Pointer receivers prevent me from using zerolog.Logger objects returned from a function call.

mitar commented 1 year ago

In general in Go, pointer receivers are used when you want to modify the struct inside the method, and value receiver when you do not do so (or, you return a copy). I think zerolog is pretty consistent in using one or the other so that it does not needlessly use pointer receivers when they are not really needed.

I am not sure how is this preventing anyone from returning objects from function calls? With zerolog you do have to be mindful when you are making a copy of the struct and when you are modifying struct in-place. Are you talking about that?

mitar commented 1 year ago

I think this is a duplicate of #400 anyway.

Also FAQ on this topic. It does recommend having consistency here in having all methods use pointer receivers. I think this is a bit of historical thing with zerolog while its API developed.