@@ -91,9 +91,6 @@ const defaultExpiryMargin = 1 * time.Minute
9191
9292// NewTokenFetcher creates a TokenFetcher with defaults applied for nil fields.
9393func NewTokenFetcher (cfg TokenFetcher ) * TokenFetcher {
94- if cfg .Logger == nil {
95- cfg .Logger = slog .Default ()
96- }
9794 if cfg .Client == nil {
9895 cfg .Client = DefaultHTTPClient ()
9996 }
@@ -103,6 +100,16 @@ func NewTokenFetcher(cfg TokenFetcher) *TokenFetcher {
103100 return & cfg
104101}
105102
103+ // log returns the configured logger, or slog.Default() if none was set.
104+ // Called at log-emit time so the current global default is always used
105+ // when no explicit logger is provided.
106+ func (tf * TokenFetcher ) log () * slog.Logger {
107+ if tf .Logger != nil {
108+ return tf .Logger
109+ }
110+ return slog .Default ()
111+ }
112+
106113// Exchange sends a token request with the given form parameters and returns
107114// the parsed result. The caller is responsible for setting grant-type-specific
108115// form parameters (e.g., refresh_token) before calling Exchange.
@@ -169,7 +176,7 @@ func (tf *TokenFetcher) doRequest(ctx context.Context, req *http.Request) (statu
169176 if ctx .Err () != nil {
170177 return 0 , nil , nil , fmt .Errorf ("token request for %s: %w" , tf .TokenURL , ctx .Err ())
171178 }
172- tf .Logger .LogAttrs (ctx , slog .LevelWarn , "token endpoint request failed" ,
179+ tf .log () .LogAttrs (ctx , slog .LevelWarn , "token endpoint request failed" ,
173180 slog .String ("token_url" , tf .TokenURL ),
174181 slog .String ("error" , err .Error ()))
175182 return 0 , nil , nil , fmt .Errorf ("token endpoint request for %s: %w" ,
@@ -193,7 +200,7 @@ func (tf *TokenFetcher) doRequest(ctx context.Context, req *http.Request) (statu
193200func (tf * TokenFetcher ) handleErrorResponse (ctx context.Context , statusCode int , header http.Header , body []byte ) error {
194201 contentType := header .Get ("Content-Type" )
195202
196- if tf .Logger .Enabled (ctx , slog .LevelDebug ) {
203+ if tf .log () .Enabled (ctx , slog .LevelDebug ) {
197204 attrs := []slog.Attr {
198205 slog .Int ("status" , statusCode ),
199206 slog .String ("content_type" , contentType ),
@@ -209,7 +216,7 @@ func (tf *TokenFetcher) handleErrorResponse(ctx context.Context, statusCode int,
209216 }
210217 }
211218
212- tf .Logger .LogAttrs (ctx , slog .LevelDebug , "token endpoint error response" , attrs ... )
219+ tf .log () .LogAttrs (ctx , slog .LevelDebug , "token endpoint error response" , attrs ... )
213220 }
214221
215222 if statusCode == http .StatusUnauthorized {
@@ -218,7 +225,7 @@ func (tf *TokenFetcher) handleErrorResponse(ctx context.Context, statusCode int,
218225 }
219226
220227 if statusCode == http .StatusTooManyRequests || statusCode >= http .StatusInternalServerError {
221- tf .Logger .LogAttrs (ctx , slog .LevelWarn , "token endpoint unavailable" ,
228+ tf .log () .LogAttrs (ctx , slog .LevelWarn , "token endpoint unavailable" ,
222229 slog .String ("token_url" , tf .TokenURL ),
223230 slog .Int ("status" , statusCode ))
224231 return fmt .Errorf ("token endpoint returned %d (content-type: %s): %w" ,
0 commit comments