value.go raw

   1  // Copyright 2022 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //go:build go1.21
   6  
   7  package slog
   8  
   9  import (
  10  	"log/slog"
  11  	"time"
  12  )
  13  
  14  // A Value can represent any Go value, but unlike type any,
  15  // it can represent most small values without an allocation.
  16  // The zero Value corresponds to nil.
  17  type Value = slog.Value
  18  
  19  // Kind is the kind of a Value.
  20  type Kind = slog.Kind
  21  
  22  // The following list is sorted alphabetically, but it's also important that
  23  // KindAny is 0 so that a zero Value represents nil.
  24  const (
  25  	KindAny       = slog.KindAny
  26  	KindBool      = slog.KindBool
  27  	KindDuration  = slog.KindDuration
  28  	KindFloat64   = slog.KindFloat64
  29  	KindInt64     = slog.KindInt64
  30  	KindString    = slog.KindString
  31  	KindTime      = slog.KindTime
  32  	KindUint64    = slog.KindUint64
  33  	KindGroup     = slog.KindGroup
  34  	KindLogValuer = slog.KindLogValuer
  35  )
  36  
  37  //////////////// Constructors
  38  
  39  // StringValue returns a new Value for a string.
  40  func StringValue(value string) Value {
  41  	return slog.StringValue(value)
  42  }
  43  
  44  // IntValue returns a Value for an int.
  45  func IntValue(v int) Value {
  46  	return slog.IntValue(v)
  47  }
  48  
  49  // Int64Value returns a Value for an int64.
  50  func Int64Value(v int64) Value {
  51  	return slog.Int64Value(v)
  52  }
  53  
  54  // Uint64Value returns a Value for a uint64.
  55  func Uint64Value(v uint64) Value {
  56  	return slog.Uint64Value(v)
  57  }
  58  
  59  // Float64Value returns a Value for a floating-point number.
  60  func Float64Value(v float64) Value {
  61  	return slog.Float64Value(v)
  62  }
  63  
  64  // BoolValue returns a Value for a bool.
  65  func BoolValue(v bool) Value {
  66  	return slog.BoolValue(v)
  67  }
  68  
  69  // TimeValue returns a Value for a time.Time.
  70  // It discards the monotonic portion.
  71  func TimeValue(v time.Time) Value {
  72  	return slog.TimeValue(v)
  73  }
  74  
  75  // DurationValue returns a Value for a time.Duration.
  76  func DurationValue(v time.Duration) Value {
  77  	return slog.DurationValue(v)
  78  }
  79  
  80  // GroupValue returns a new Value for a list of Attrs.
  81  // The caller must not subsequently mutate the argument slice.
  82  func GroupValue(as ...Attr) Value {
  83  	return slog.GroupValue(as...)
  84  }
  85  
  86  // AnyValue returns a Value for the supplied value.
  87  //
  88  // If the supplied value is of type Value, it is returned
  89  // unmodified.
  90  //
  91  // Given a value of one of Go's predeclared string, bool, or
  92  // (non-complex) numeric types, AnyValue returns a Value of kind
  93  // String, Bool, Uint64, Int64, or Float64. The width of the
  94  // original numeric type is not preserved.
  95  //
  96  // Given a time.Time or time.Duration value, AnyValue returns a Value of kind
  97  // KindTime or KindDuration. The monotonic time is not preserved.
  98  //
  99  // For nil, or values of all other types, including named types whose
 100  // underlying type is numeric, AnyValue returns a value of kind KindAny.
 101  func AnyValue(v any) Value {
 102  	return slog.AnyValue(v)
 103  }
 104  
 105  // A LogValuer is any Go value that can convert itself into a Value for logging.
 106  //
 107  // This mechanism may be used to defer expensive operations until they are
 108  // needed, or to expand a single value into a sequence of components.
 109  type LogValuer = slog.LogValuer
 110