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

Expose the client log to be used in middleware? #132

Closed
nomad-software opened this issue Jul 1, 2022 · 4 comments
Closed

Expose the client log to be used in middleware? #132

nomad-software opened this issue Jul 1, 2022 · 4 comments
Assignees

Comments

@nomad-software
Copy link

Hi, thanks for the great library!

Is there any way I can use the client log in middleware? I know we can set the logger using client.SetLog(...) but it would be nice to use this later.

Because the client log field is not exported I have to do something like this to enable a log in middleware:

func LogRequest(log req.Logger) req.RequestMiddleware {
    return func(c *req.Client, r *req.Request) error {
        log.Debugf("%s request, path: %s, retry attempt: %d",
            r.Method,
            r.URL.RequestURI(),
            r.RetryAttempt,
        )
        return nil
    }
}
...
client := req.C().
    SetLog(logger).
    client.OnBeforeRequest(middleware.LogRequest(logger))

It would be nice to just do this:

func LogRequest(c *req.Client, r *req.Request) error {
    c.Log.Debugf("%s request, path: %s, retry attempt: %d",
        r.Method,
        r.URL.RequestURI(),
        r.RetryAttempt,
    )
    return nil
}
...
client := req.C().
    SetLog(logger).
    client.OnBeforeRequest(middleware.LogRequest)

Thoughts?

@nomad-software nomad-software changed the title Expose the client log to be used in middleware Expose the client log to be used in middleware? Jul 1, 2022
@imroc
Copy link
Owner

imroc commented Jul 2, 2022

Good idea, expose Client.Logger() would be more appropriate, but there has been a major refactoring recently. In order to avoid code conflicts, I will support this after the http3 support is released.

Check this out: https://req.cool/blog/http3-support-is-coming/

@imroc imroc self-assigned this Jul 2, 2022
@nomad-software
Copy link
Author

Cool, thanks!

@imroc
Copy link
Owner

imroc commented Jul 11, 2022

v3.14.0 is released, now you can use Client.GetLogger() to get the logger.

@imroc imroc closed this as completed Jul 11, 2022
@nomad-software
Copy link
Author

Awesome, Thankyou!

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

2 participants