Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superficial comments and no doc for WithContext method #1055

Closed
bentcoder opened this issue Oct 27, 2019 · 2 comments
Closed

Superficial comments and no doc for WithContext method #1055

bentcoder opened this issue Oct 27, 2019 · 2 comments

Comments

@bentcoder
Copy link

Hi,

I've seen Add WithContext PR has been merged but there isn't really a descriptive/helpful documentation on it.

Contains the context set by the user. Useful for hook processing etc.

For example, is it going to allow us to add some custom fields to logs with dynamic values per request? Is it used to fetch data from context and do what ever we want to do with it? If so, how, any example please.

Thanks

@bentcoder
Copy link
Author

Currently I am using a custom logrus wrapper that contains function below to add default fields to every log and they come from context. Have a reinvented the wheal or logrus WithContext has no ability for such logic?

...
...

func MyWithContext(ctx context.Context) *logrus.Entry {
	// Static value for every requests
	l := logrus.WithField("static_field", "always same")

	// Dynamic values per request
	for _, k := range []string{"request_id", "user_id"} {
		v := ctx.Value(k)
		if v == nil {
			v = ""
		}

		l = l.WithField(k, fmt.Sprintf("%v", v))
	}

	return l
}

@dgsb dgsb self-assigned this Oct 28, 2019
@dgsb
Copy link
Collaborator

dgsb commented Oct 31, 2019

The context has been added for hooks processing. There is no field automatically added from a context. Someone may have implemented that as a hook, but I'm unaware of it.
Depending on the hook implementation, some used api may require to have a context so it makes sense to use the one from the caller for deadline management rather than using an empty one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants