Comments (4)
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.
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.
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.
Please review #81.
from zerolog.
Related Issues (20)
- prettylog only displays the first line of the output HOT 1
- feature request: add event Level method for setting the level HOT 1
- Is there a way to obtain the 'buf' from the Event in a hook? HOT 2
- [question] how to rollover log use zerolog and lumberjack ?
- [Question]Error stack with pkgerrors.MarshalStack seems to be too long in some cases
- [QUESTION] Implementation Advise HOT 3
- logger Interface() method cant write map[interface{}]interface{} log
- logger.UpdateContext checks for disabled logger using equality
- Publish a new tag/release each time something is merged to main HOT 1
- UnmarshalKey may return wrong values HOT 2
- Fields with `map[string]string`
- Caller confusion when using caller multiple times HOT 2
- Revert #597 HOT 1
- InterfaceMarshalFunc is not called when encoding JSON HOT 2
- Understanding "Zero Allocation" HOT 1
- Is there a recommended way to do batching of log lines and write at once ? HOT 1
- Logs as bytes in Stderr HOT 6
- Deep Example
- Latest official tagged release not up to date in github? HOT 1
- Add an option to limit max log's size after escaping
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zerolog.