field.go 6.6 KB

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