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

RecoveryLogger should accept interface instead of struct! #92

Closed
pavelnikolov opened this issue Nov 22, 2016 · 3 comments
Closed

RecoveryLogger should accept interface instead of struct! #92

pavelnikolov opened this issue Nov 22, 2016 · 3 comments

Comments

@pavelnikolov
Copy link

pavelnikolov commented Nov 22, 2016

This code is not designed well: https://github.com/gorilla/handlers/blob/master/recovery.go#L49

func RecoveryLogger(logger *log.Logger) RecoveryOption {
	return func(h http.Handler) {
		r := h.(*recoveryHandler)
		r.logger = logger
	}
}

You should avoid accepting structs and instead try to accept interfaces whenever possible. In this particular case, since you are only using the Println(...interface{}) method, you should create an interface for the input argument like this:

type Logger interface {
    Println(v ...interface{}}
}

This would allow users to pass a custom structured logger (e.g. Logrus), which might have some other fields/tags like environment, host, user, application etc. Unstructured logs in a highly distributed environment are totally useless.

@kisielk
Copy link
Contributor

kisielk commented Nov 23, 2016

I agree it probably should have used an interface. Not sure if changing that now would break existing code or not. I suspect it would be fine...

@pavelnikolov
Copy link
Author

pavelnikolov commented Nov 23, 2016 via email

pavelnikolov pushed a commit to pavelnikolov/handlers that referenced this issue Dec 2, 2016
pavelnikolov pushed a commit to pavelnikolov/handlers that referenced this issue Dec 6, 2016
kisielk added a commit that referenced this issue Dec 6, 2016
…stead-of-struct

Use logger interface in recovery handler (#92)
@kisielk
Copy link
Contributor

kisielk commented Feb 9, 2017

Fixed by #94

@kisielk kisielk closed this as completed Feb 9, 2017
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

2 participants