Skip to content

Commit

Permalink
Merge pull request #94 from pavelnikolov/recovery-logger-interface-in…
Browse files Browse the repository at this point in the history
…stead-of-struct

Use logger interface in recovery handler (#92)
  • Loading branch information
kisielk authored Dec 6, 2016
2 parents e1b2144 + 3fda98c commit 3a5767c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import (
"runtime/debug"
)

// RecoveryHandlerLogger is an interface used by the recovering handler to print logs.
type RecoveryHandlerLogger interface {
Println(...interface{})
}

type recoveryHandler struct {
handler http.Handler
logger *log.Logger
logger RecoveryHandlerLogger
printStack bool
}

Expand Down Expand Up @@ -46,7 +51,7 @@ func RecoveryHandler(opts ...RecoveryOption) func(h http.Handler) http.Handler {

// RecoveryLogger is a functional option to override
// the default logger
func RecoveryLogger(logger *log.Logger) RecoveryOption {
func RecoveryLogger(logger RecoveryHandlerLogger) RecoveryOption {
return func(h http.Handler) {
r := h.(*recoveryHandler)
r.logger = logger
Expand All @@ -73,11 +78,11 @@ func (h recoveryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.handler.ServeHTTP(w, req)
}

func (h recoveryHandler) log(message interface{}) {
func (h recoveryHandler) log(v ...interface{}) {
if h.logger != nil {
h.logger.Println(message)
h.logger.Println(v...)
} else {
log.Println(message)
log.Println(v...)
}

if h.printStack {
Expand Down

0 comments on commit 3a5767c

Please sign in to comment.