package middleware

import (
	

	
)

// loggerKey is the context value key for which the logger is associated with.
type loggerKey struct{}

// GetLogger takes a context to retrieve a Logger from. If no logger is present on the context a logging.Nop logger
// is returned. If the logger retrieved from context supports the ContextLogger interface, the context will be passed
// to the WithContext method and the resulting logger will be returned. Otherwise the stored logger is returned as is.
func ( context.Context) logging.Logger {
	,  := .Value(loggerKey{}).(logging.Logger)
	if ! ||  == nil {
		return logging.Nop{}
	}

	return logging.WithContext(, )
}

// SetLogger sets the provided logger value on the provided ctx.
func ( context.Context,  logging.Logger) context.Context {
	return context.WithValue(, loggerKey{}, )
}

type setLogger struct {
	Logger logging.Logger
}

// AddSetLoggerMiddleware adds a middleware that will add the provided logger to the middleware context.
func ( *Stack,  logging.Logger) error {
	return .Initialize.Add(&setLogger{Logger: }, After)
}

func ( *setLogger) () string {
	return "SetLogger"
}

func ( *setLogger) ( context.Context,  InitializeInput,  InitializeHandler) (
	 InitializeOutput,  Metadata,  error,
) {
	return .HandleInitialize(SetLogger(, .Logger), )
}