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

enable passing custom logger #5

Closed
wants to merge 1 commit into from
Closed

Conversation

LukasJenicek
Copy link

@LukasJenicek LukasJenicek commented Apr 2, 2024

  • Provide a way how to pass custom logger to keep structured logging in your application same

Comment on lines +17 to 42
func LogRequests(logger Logger) func(http.RoundTripper) http.RoundTripper {
if logger == nil {
logger = &DefaultLogger{}
}

return func(next http.RoundTripper) http.RoundTripper {
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) {
r := cloneRequest(req)

curlCommand, _ := http2curl.GetCurlCommand(r)
logger.Info("%v", curlCommand)
logger.Info("request: %s %s", r.Method, r.URL)

startTime := time.Now()
defer func() {
if resp != nil {
logger.Info("response (HTTP %v): %v %s", time.Since(startTime), resp.Status, r.URL)
} else {
logger.Info("response (<nil>): %v %s", time.Since(startTime), r.URL)
}
}()

return next.RoundTrip(r)
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.. however instead of Logger can we just use io.Writer?

I found myself needing this, so I could save each request to a buffer and only log/print it on error :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally forgot that I created this PR and today I created similar PR just week after :D It's the heat I guess..

Anyway what do you think about this approach: #7 ???

This way the consumer of the package can do whatever he likes

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

Successfully merging this pull request may close these issues.

2 participants