rs / zerolog

Zero Allocation JSON Logger
MIT License
10.61k stars 572 forks source link

Allow callers to pass go context through to hooks #559

Closed danielbprice closed 1 year ago

danielbprice commented 1 year ago

Add Logger.{Trace,Debug,Info,Warn,...}Ctx() and similar functions to allow go context to propagate to Hooks. Add Event.Context() to make the context retrievable by hooks. Facilitates writing hooks which fetch tracing context from the go context.


This PR helps to address #395, #558 and maybe #290. It is modeled off of the similar interfaces in the new slog stdlib package in go 1.21.

danielbprice commented 1 year ago

I added a bunch of testing today, as I didn't want to be less than thorough; however, if they seem like overkill I am happy to yank them.

rs commented 1 year ago

Thinking about the API, what do you think about, instead of exposing a new Context method on the event, add an alternative Hook interface that would take a ctx as the first argument?

danielbprice commented 1 year ago

Thinking about the API, what do you think about, instead of exposing a new Context method on the event, add an alternative Hook interface that would take a ctx as the first argument?

Sure thing, should be an easy change. I want you to be satisfied with the interface choices.

danielbprice commented 1 year ago

Thinking about the API, what do you think about, instead of exposing a new Context method on the event, add an alternative Hook interface that would take a ctx as the first argument?

Sure thing, should be an easy change. I want you to be satisfied with the interface choices.

Hi Olivier,

So this turned out to be quite a bit more awkward than I had expected, as it required duplicating almost all of the Hook into a new HookCtx interface-- we can't add e.g. RunCtx to the Hook interface. I've put the changes up on a branch for you to see, but it seems awkward to duplicate so much code and mechanism to get this. https://github.com/danielbprice/zerolog/commit/42e0eb265132d8575e8927babdb2856011664544. What do you think?

Also, over the weekend, I began to wonder if I spent too little time thinking through the interface here. Would we be better off passing the context directly into the terminal routines-- as in: MsgCtx(ctx, "hello") and MsgfCtx(ctx, "hello %s", "there"). Or into a method on the event (ex: logger.Warn().Str("foo", "bar").Ctx(ctx).Msg("hello")? You've had much more time to think about what is right for zerolog, so I am open to refining this some more. In my mind, there are competing forces: .InfoCtx() feels somewhat more Go-Idiomatic and aligns with slog. But .Ctx() feels more idiomatic to the method-chaining style and more "native" to zerolog (IMO).

So, three options I see:

  1. (keep what we have in this PR): logger.InfoCtx(ctx).Str("mystr", "abcd").Msg("hello")
  2. (pass at terminal end): logger.Info().Str("mystr", "abcd").MsgCtx(ctx, "hello")
  3. (pass via new function): logger.Info().Str("mystr", "abcd").Ctx(ctx).Msg("hello")

Any thoughts? It seems to me that either #1 or #3 is most likely to be the right answer.

danielbprice commented 1 year ago

Any thoughts? It seems to me that either #1 or #3 is most likely to be the right answer.

@rs Just wondered if you have any new thoughts here? I've just wrapped up another project and might have a day to devote to this in the coming week.

rs commented 1 year ago
  1. probably makes the most sense. I would probably use WithContext(ctx) and Context() as method names.
danielbprice commented 1 year ago

Hi! Sorry for the delay, but work has been busy and I've had to deal with Python structured logging. 🤮 😡 🤬

I posted a significantly revised review:

danielbprice commented 1 year ago

Hi @rs, any chance you could take a look at this (significantly) revised version? I have some cycles this week for another round of review and fixing, and hopefully integration.

danielbprice commented 1 year ago

@rs Thank you so much! I really appreciate the opportunity to contribute.

inputvalidation commented 1 year ago

Really appreciate this PR. I was writing my code against it and then found it was not released yet 😇 Not sure of zerolog's release policy but would be great to be able to use this in the field.

SophisticaSean commented 1 year ago

Super stoked to use this. Thank you @danielbprice for your contribution here.

danielbprice commented 1 year ago

@rs Any chance you would be willing to tag v1.30.0 so that people can start to pull this into projects more easily?

rs commented 1 year ago

done

danielbprice commented 1 year ago

Many thanks!