attr.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  // An Attr is a key-value pair.
  15  type Attr = slog.Attr
  16  
  17  // String returns an Attr for a string value.
  18  func String(key, value string) Attr {
  19  	return slog.String(key, value)
  20  }
  21  
  22  // Int64 returns an Attr for an int64.
  23  func Int64(key string, value int64) Attr {
  24  	return slog.Int64(key, value)
  25  }
  26  
  27  // Int converts an int to an int64 and returns
  28  // an Attr with that value.
  29  func Int(key string, value int) Attr {
  30  	return slog.Int(key, value)
  31  }
  32  
  33  // Uint64 returns an Attr for a uint64.
  34  func Uint64(key string, v uint64) Attr {
  35  	return slog.Uint64(key, v)
  36  }
  37  
  38  // Float64 returns an Attr for a floating-point number.
  39  func Float64(key string, v float64) Attr {
  40  	return slog.Float64(key, v)
  41  }
  42  
  43  // Bool returns an Attr for a bool.
  44  func Bool(key string, v bool) Attr {
  45  	return slog.Bool(key, v)
  46  }
  47  
  48  // Time returns an Attr for a time.Time.
  49  // It discards the monotonic portion.
  50  func Time(key string, v time.Time) Attr {
  51  	return slog.Time(key, v)
  52  }
  53  
  54  // Duration returns an Attr for a time.Duration.
  55  func Duration(key string, v time.Duration) Attr {
  56  	return slog.Duration(key, v)
  57  }
  58  
  59  // Group returns an Attr for a Group Value.
  60  // The first argument is the key; the remaining arguments
  61  // are converted to Attrs as in [Logger.Log].
  62  //
  63  // Use Group to collect several key-value pairs under a single
  64  // key on a log line, or as the result of LogValue
  65  // in order to log a single value as multiple Attrs.
  66  func Group(key string, args ...any) Attr {
  67  	return slog.Group(key, args...)
  68  }
  69  
  70  // Any returns an Attr for the supplied value.
  71  // See [Value.AnyValue] for how values are treated.
  72  func Any(key string, value any) Attr {
  73  	return slog.Any(key, value)
  74  }
  75