github.com/drone/runner-go@v1.12.0/logger/context.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package logger 6 7 import ( 8 "context" 9 "net/http" 10 ) 11 12 type loggerKey struct{} 13 14 // WithContext returns a new context with the provided logger. 15 // Use in combination with logger.WithField for great effect. 16 func WithContext(ctx context.Context, logger Logger) context.Context { 17 return context.WithValue(ctx, loggerKey{}, logger) 18 } 19 20 // FromContext retrieves the current logger from the context. 21 func FromContext(ctx context.Context) Logger { 22 logger := ctx.Value(loggerKey{}) 23 if logger == nil { 24 return Default 25 } 26 return logger.(Logger) 27 } 28 29 // FromRequest retrieves the current logger from the request. If no 30 // logger is available, the default logger is returned. 31 func FromRequest(r *http.Request) Logger { 32 return FromContext(r.Context()) 33 }