field.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package log
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type fieldType int
  7. const (
  8. stringType fieldType = iota
  9. boolType
  10. intType
  11. int32Type
  12. uint32Type
  13. int64Type
  14. uint64Type
  15. float32Type
  16. float64Type
  17. errorType
  18. objectType
  19. lazyLoggerType
  20. )
  21. // Field instances are constructed via LogBool, LogString, and so on.
  22. // Tracing implementations may then handle them via the Field.Marshal
  23. // method.
  24. //
  25. // "heavily influenced by" (i.e., partially stolen from)
  26. // https://github.com/uber-go/zap
  27. type Field struct {
  28. key string
  29. fieldType fieldType
  30. numericVal int64
  31. stringVal string
  32. interfaceVal interface{}
  33. }
  34. // String adds a string-valued key:value pair to a Span.LogFields() record
  35. func String(key, val string) Field {
  36. return Field{
  37. key: key,
  38. fieldType: stringType,
  39. stringVal: val,
  40. }
  41. }
  42. // Bool adds a bool-valued key:value pair to a Span.LogFields() record
  43. func Bool(key string, val bool) Field {
  44. var numericVal int64
  45. if val {
  46. numericVal = 1
  47. }
  48. return Field{
  49. key: key,
  50. fieldType: boolType,
  51. numericVal: numericVal,
  52. }
  53. }
  54. // Int adds an int-valued key:value pair to a Span.LogFields() record
  55. func Int(key string, val int) Field {
  56. return Field{
  57. key: key,
  58. fieldType: intType,
  59. numericVal: int64(val),
  60. }
  61. }
  62. // Int32 adds an int32-valued key:value pair to a Span.LogFields() record
  63. func Int32(key string, val int32) Field {
  64. return Field{
  65. key: key,
  66. fieldType: int32Type,
  67. numericVal: int64(val),
  68. }
  69. }
  70. // Int64 adds an int64-valued key:value pair to a Span.LogFields() record
  71. func Int64(key string, val int64) Field {
  72. return Field{
  73. key: key,
  74. fieldType: int64Type,
  75. numericVal: val,
  76. }
  77. }
  78. // Uint32 adds a uint32-valued key:value pair to a Span.LogFields() record
  79. func Uint32(key string, val uint32) Field {
  80. return Field{
  81. key: key,
  82. fieldType: uint32Type,
  83. numericVal: int64(val),
  84. }
  85. }
  86. // Uint64 adds a uint64-valued key:value pair to a Span.LogFields() record
  87. func Uint64(key string, val uint64) Field {
  88. return Field{
  89. key: key,
  90. fieldType: uint64Type,
  91. numericVal: int64(val),
  92. }
  93. }
  94. // Float32 adds a float32-valued key:value pair to a Span.LogFields() record
  95. func Float32(key string, val float32) Field {
  96. return Field{
  97. key: key,
  98. fieldType: float32Type,
  99. numericVal: int64(math.Float32bits(val)),
  100. }
  101. }
  102. // Float64 adds a float64-valued key:value pair to a Span.LogFields() record
  103. func Float64(key string, val float64) Field {
  104. return Field{
  105. key: key,
  106. fieldType: float64Type,
  107. numericVal: int64(math.Float64bits(val)),
  108. }
  109. }
  110. // Error adds an error with the key "error" to a Span.LogFields() record
  111. func Error(err error) Field {
  112. return Field{
  113. key: "error",
  114. fieldType: errorType,
  115. interfaceVal: err,
  116. }
  117. }
  118. // Object adds an object-valued key:value pair to a Span.LogFields() record
  119. func Object(key string, obj interface{}) Field {
  120. return Field{
  121. key: key,
  122. fieldType: objectType,
  123. interfaceVal: obj,
  124. }
  125. }
  126. // LazyLogger allows for user-defined, late-bound logging of arbitrary data
  127. type LazyLogger func(fv Encoder)
  128. // Lazy adds a LazyLogger to a Span.LogFields() record; the tracing
  129. // implementation will call the LazyLogger function at an indefinite time in
  130. // the future (after Lazy() returns).
  131. func Lazy(ll LazyLogger) Field {
  132. return Field{
  133. fieldType: lazyLoggerType,
  134. interfaceVal: ll,
  135. }
  136. }
  137. // Encoder allows access to the contents of a Field (via a call to
  138. // Field.Marshal).
  139. //
  140. // Tracer implementations typically provide an implementation of Encoder;
  141. // OpenTracing callers typically do not need to concern themselves with it.
  142. type Encoder interface {
  143. EmitString(key, value string)
  144. EmitBool(key string, value bool)
  145. EmitInt(key string, value int)
  146. EmitInt32(key string, value int32)
  147. EmitInt64(key string, value int64)
  148. EmitUint32(key string, value uint32)
  149. EmitUint64(key string, value uint64)
  150. EmitFloat32(key string, value float32)
  151. EmitFloat64(key string, value float64)
  152. EmitObject(key string, value interface{})
  153. EmitLazyLogger(value LazyLogger)
  154. }
  155. // Marshal passes a Field instance through to the appropriate
  156. // field-type-specific method of an Encoder.
  157. func (lf Field) Marshal(visitor Encoder) {
  158. switch lf.fieldType {
  159. case stringType:
  160. visitor.EmitString(lf.key, lf.stringVal)
  161. case boolType:
  162. visitor.EmitBool(lf.key, lf.numericVal != 0)
  163. case intType:
  164. visitor.EmitInt(lf.key, int(lf.numericVal))
  165. case int32Type:
  166. visitor.EmitInt32(lf.key, int32(lf.numericVal))
  167. case int64Type:
  168. visitor.EmitInt64(lf.key, int64(lf.numericVal))
  169. case uint32Type:
  170. visitor.EmitUint32(lf.key, uint32(lf.numericVal))
  171. case uint64Type:
  172. visitor.EmitUint64(lf.key, uint64(lf.numericVal))
  173. case float32Type:
  174. visitor.EmitFloat32(lf.key, math.Float32frombits(uint32(lf.numericVal)))
  175. case float64Type:
  176. visitor.EmitFloat64(lf.key, math.Float64frombits(uint64(lf.numericVal)))
  177. case errorType:
  178. if err, ok := lf.interfaceVal.(error); ok {
  179. visitor.EmitString(lf.key, err.Error())
  180. } else {
  181. visitor.EmitString(lf.key, "<nil>")
  182. }
  183. case objectType:
  184. visitor.EmitObject(lf.key, lf.interfaceVal)
  185. case lazyLoggerType:
  186. visitor.EmitLazyLogger(lf.interfaceVal.(LazyLogger))
  187. }
  188. }
  189. // Key returns the field's key.
  190. func (lf Field) Key() string {
  191. return lf.key
  192. }
  193. // Value returns the field's value as interface{}.
  194. func (lf Field) Value() interface{} {
  195. switch lf.fieldType {
  196. case stringType:
  197. return lf.stringVal
  198. case boolType:
  199. return lf.numericVal != 0
  200. case intType:
  201. return int(lf.numericVal)
  202. case int32Type:
  203. return int32(lf.numericVal)
  204. case int64Type:
  205. return int64(lf.numericVal)
  206. case uint32Type:
  207. return uint32(lf.numericVal)
  208. case uint64Type:
  209. return uint64(lf.numericVal)
  210. case float32Type:
  211. return math.Float32frombits(uint32(lf.numericVal))
  212. case float64Type:
  213. return math.Float64frombits(uint64(lf.numericVal))
  214. case errorType, objectType, lazyLoggerType:
  215. return lf.interfaceVal
  216. default:
  217. return nil
  218. }
  219. }
  220. // String returns a string representation of the key and value.
  221. func (lf Field) String() string {
  222. return fmt.Sprint(lf.key, ":", lf.Value())
  223. }