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

Use logrus.Logger in httputil.ReverseProxy.ErrorLog ? #588

Closed
kedare opened this issue Jul 18, 2017 · 5 comments
Closed

Use logrus.Logger in httputil.ReverseProxy.ErrorLog ? #588

kedare opened this issue Jul 18, 2017 · 5 comments

Comments

@kedare
Copy link

kedare commented Jul 18, 2017

Hello,

I'm trying to use logrus as logger for httputil.ReverseProxy.

It's expecting a log.Logger type, I've been searching without success on how do to that.

Any hint ? Is there a way to get a log.Logger from logrus ?

You can see the type there : https://golang.org/pkg/net/http/httputil/#ReverseProxy

Thanks

@dmathieu
Copy link
Contributor

Mathieu,

Unfortunately, I'm afraid this doesn't seem to be possible today.
log.Logger is a struct, not an interface. So it can't be anything else than an actual Go logger.

@kedare
Copy link
Author

kedare commented Jul 19, 2017

The best way to allow this would be to fix the ReverseProxy type to get an interface instead of the specific struct so ? (So in the Go standard library)

@dmathieu
Copy link
Contributor

Basically, yes.

@kedare
Copy link
Author

kedare commented Jul 19, 2017

Thanks for the info, I published in issue on the Go repository if you want to follow that :)

@bartlettc22
Copy link

It's a workaround but you could accomplish this with your own standard logger that writes to logrus. Something like:

type logrusErrorWriter struct{}

func (w logrusErrorWriter) Write(p []byte) (n int, err error) {
	logrus.Errorf("%s", string(p))
	return len(p), nil
}

func main() {
  director := func(req *http.Request) {
    req.URL.Host = "google.comxyz"
  }
  
  errorLogger := log.New(logrusErrorWriter{}, "", 0)
  proxy := httputil.ReverseProxy{
    Director: director,
    ErrorLog: errorLogger,
  }
  ...
}

Outputs

ERRO[0003] http: proxy error: dial tcp: lookup google.comxyz on 10.0.0.1:53: no such host

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

No branches or pull requests

3 participants