Giter Site home page Giter Site logo

Comments (4)

rs avatar rs commented on July 28, 2024

The rational is that a request context should only have a single instance of the logger so your requests can add fields that are visible to parent middleware for instance. This is particularly useful with the hlog.AccessHandler which can be augmented with request specific fields.

This is also better for performance. Storing a new instance of the logger in the context for each update would end up with a pretty narrow context tree that would impact lookup time for every context.Value calls.

from zerolog.

bluekeyes avatar bluekeyes commented on July 28, 2024

Thanks. I think I understand the reasoning here, but I'm now confused by the difference between (*Logger) UpdateContext and (Logger) WithContext. It seems that UpdateContext also achieves what you describe (fields visible to parent middleware, no new context), so is there a reason to use WithContext instead of calling zerolog.Ctx(ctx).UpdateContext(...) in places that want this behavior?

To give a more concrete example, I have an application that responds to a GitHub webhook and performs an action on every open pull request in the repository. In the handler, I loop over the open PRs and call updatePullRequest(context.Context, *github.PullRequest). This does some logging of its own (and may call other functions that log), so I want to attach the PR number that is being processed to the logger in the context that I pass.

In your view, is it better to pass the modified logger explicitly as an argument or re-add the necessary fields at each level instead of passing a modified context?

from zerolog.

rs avatar rs commented on July 28, 2024

The (*Logger) UpdateContext will update the context of the logger with no copy of the logger (in place). If the (Logger) WithContext was just storing the pointer without trying to reuse the existing one, it would store the same pointer over and over in the context for each call of WithContext.

What I can do though, is only updating the existing pointer if the currently stored logger is the same as the one you are trying to store. This way, if you copy the logger instead of calling WithContext, it will behave the way you expect.

from zerolog.

rs avatar rs commented on July 28, 2024

Please review #81.

from zerolog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.