Skip to content

Commit

Permalink
Add type-safe accessor for the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Feb 20, 2017
1 parent 0f4d9cc commit bb66103
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ type ContextKeys string
// CtxValueLogger is the key to extract the logrus Logger.
const CtxValueLogger = ContextKeys("logger")

// GetLogger retrieves the logrus logger from the supplied context. Returns nil if there is no logger.
func GetLogger(ctx context.Context) *log.Entry {
l := ctx.Value(CtxValueLogger)
if l == nil {
return nil
}
return l.(*log.Entry)
}

// JSONRequestHandler represents an interface that must be satisfied in order to respond to incoming
// HTTP requests with JSON. The interface returned will be marshalled into JSON to be sent to the client,
// unless the interface is []byte in which case the bytes are sent to the client unchanged.
Expand Down

0 comments on commit bb66103

Please sign in to comment.