...

Source file src/edge-infra.dev/pkg/lib/fog/context.go

Documentation: edge-infra.dev/pkg/lib/fog

     1  package fog
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-logr/logr"
     7  )
     8  
     9  // FromContext returns a logger with predefined values from a [context.Context].
    10  // If one doesn't exist, a default logger is instantiated and returned.
    11  //
    12  // This is a thin wrapper around [github.com/go-logr/logr.FromContext]
    13  func FromContext(ctx context.Context, keysAndValues ...interface{}) logr.Logger {
    14  	if ctx != nil {
    15  		if logger, err := logr.FromContext(ctx); err == nil {
    16  			return logger.WithValues(keysAndValues...)
    17  		}
    18  	}
    19  	return New().WithValues(keysAndValues...)
    20  }
    21  
    22  // IntoContext takes a context and sets the logger as one of its values.
    23  // Use the [FromContext] function to retrieve the logger.
    24  //
    25  // This is a thin wrapper around [github.com/go-logr/logr.IntoContext]
    26  func IntoContext(ctx context.Context, log logr.Logger) context.Context {
    27  	return logr.NewContext(ctx, log)
    28  }
    29  

View as plain text