Is there any way to pass in some sort of context (for example, a logger) to the hooks? #1544
-
What would you like to discuss?I looked at the documentation as well as the open and closed issues and I didn't see anything like what I would like to do. I would love to be able to pass in context on a per request basis into the hooks (specifically beforeRetry and afterResponse) so that we could give it our logger that has all of our express request properties so we can have standardized logging of external requests and retires. Is this possible with the current implementation and I've just missed it? Or if it is not currently possible, is this something that has ever been considered/would be considered for the future? Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It is currently possible, but there's a plan to unify all those events so logging would be much simpler. See #559 |
Beta Was this translation helpful? Give feedback.
-
Hey @szmarczak, I appreciate the link to the issue where this may be fixed later on. But how would I go about fixing it with the current implementation ("It is currently possible")? |
Beta Was this translation helpful? Give feedback.
-
You can use |
Beta Was this translation helpful? Give feedback.
You can use
got.extend
. Pass{context: {logger}}
as the options and attach some hooks e.g. beforeRequest, beforeRetry and beforeError. Then use the options argument or an error object to extract the data you want to log.