|
| 1 | +package log |
| 2 | + |
| 3 | +import "math" |
| 4 | + |
| 5 | +type fieldType int |
| 6 | + |
| 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 | + |
| 22 | +// Field instances are constructed via LogBool, LogString, and so on. |
| 23 | +// Tracing implementations may then handle them via the Field.Process |
| 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 | + |
| 36 | +// String adds a string-valued key:value pair to a Span.LogFields() record |
| 37 | +func String(key, val string) Field { |
| 38 | + return Field{ |
| 39 | + key: key, |
| 40 | + fieldType: stringType, |
| 41 | + stringVal: val, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Bool adds a bool-valued key:value pair to a Span.LogFields() record |
| 46 | +func Bool(key string, val bool) Field { |
| 47 | + var numericVal int64 |
| 48 | + if val { |
| 49 | + numericVal = 1 |
| 50 | + } |
| 51 | + return Field{ |
| 52 | + key: key, |
| 53 | + fieldType: boolType, |
| 54 | + numericVal: numericVal, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Int adds an int-valued key:value pair to a Span.LogFields() record |
| 59 | +func Int(key string, val int) Field { |
| 60 | + return Field{ |
| 61 | + key: key, |
| 62 | + fieldType: intType, |
| 63 | + numericVal: int64(val), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Int32 adds an int32-valued key:value pair to a Span.LogFields() record |
| 68 | +func Int32(key string, val int32) Field { |
| 69 | + return Field{ |
| 70 | + key: key, |
| 71 | + fieldType: int32Type, |
| 72 | + numericVal: int64(val), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Int64 adds an int64-valued key:value pair to a Span.LogFields() record |
| 77 | +func Int64(key string, val int64) Field { |
| 78 | + return Field{ |
| 79 | + key: key, |
| 80 | + fieldType: int64Type, |
| 81 | + numericVal: val, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Uint32 adds a uint32-valued key:value pair to a Span.LogFields() record |
| 86 | +func Uint32(key string, val uint32) Field { |
| 87 | + return Field{ |
| 88 | + key: key, |
| 89 | + fieldType: uint32Type, |
| 90 | + numericVal: int64(val), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Uint64 adds a uint64-valued key:value pair to a Span.LogFields() record |
| 95 | +func Uint64(key string, val uint64) Field { |
| 96 | + return Field{ |
| 97 | + key: key, |
| 98 | + fieldType: uint64Type, |
| 99 | + numericVal: int64(val), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Float32 adds a float32-valued key:value pair to a Span.LogFields() record |
| 104 | +func Float32(key string, val float32) Field { |
| 105 | + return Field{ |
| 106 | + key: key, |
| 107 | + fieldType: float32Type, |
| 108 | + numericVal: int64(math.Float32bits(val)), |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Float64 adds a float64-valued key:value pair to a Span.LogFields() record |
| 113 | +func Float64(key string, val float64) Field { |
| 114 | + return Field{ |
| 115 | + key: key, |
| 116 | + fieldType: float64Type, |
| 117 | + numericVal: int64(math.Float64bits(val)), |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// Error adds an error with the key "error" to a Span.LogFields() record |
| 122 | +func Error(err error) Field { |
| 123 | + return Field{ |
| 124 | + key: "error", |
| 125 | + fieldType: errorType, |
| 126 | + interfaceVal: err, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Object adds an object-valued key:value pair to a Span.LogFields() record |
| 131 | +func Object(key string, obj interface{}) Field { |
| 132 | + return Field{ |
| 133 | + key: key, |
| 134 | + fieldType: objectType, |
| 135 | + interfaceVal: obj, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// LazyLogger allows for user-defined, late-bound logging of arbitrary data |
| 140 | +type LazyLogger func(fv Encoder) |
| 141 | + |
| 142 | +// Lazy adds a LazyLogger to a Span.LogFields() record; the tracing |
| 143 | +// implementation will call the LazyLogger function at an indefinite time in |
| 144 | +// the future (after Lazy() returns). |
| 145 | +func Lazy(ll LazyLogger) Field { |
| 146 | + return Field{ |
| 147 | + fieldType: lazyLoggerType, |
| 148 | + interfaceVal: ll, |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Encoder allows access to the contents of a Field (via a call to |
| 153 | +// Field.Marshal). |
| 154 | +// |
| 155 | +// Tracer implementations typically provide an implementation of Encoder; |
| 156 | +// OpenTracing callers typically do not need to concern themselves with it. |
| 157 | +type Encoder interface { |
| 158 | + EmitString(key, value string) |
| 159 | + EmitBool(key string, value bool) |
| 160 | + EmitInt(key string, value int) |
| 161 | + EmitInt32(key string, value int32) |
| 162 | + EmitInt64(key string, value int64) |
| 163 | + EmitUint32(key string, value uint32) |
| 164 | + EmitUint64(key string, value uint64) |
| 165 | + EmitFloat32(key string, value float32) |
| 166 | + EmitFloat64(key string, value float64) |
| 167 | + EmitObject(key string, value interface{}) |
| 168 | + EmitLazyLogger(value LazyLogger) |
| 169 | +} |
| 170 | + |
| 171 | +// Marshal passes a Field instance through to the appropriate |
| 172 | +// field-type-specific method of an Encoder. |
| 173 | +func (lf Field) Marshal(visitor Encoder) { |
| 174 | + switch lf.fieldType { |
| 175 | + case stringType: |
| 176 | + visitor.EmitString(lf.key, lf.stringVal) |
| 177 | + case boolType: |
| 178 | + visitor.EmitBool(lf.key, lf.numericVal != 0) |
| 179 | + case intType: |
| 180 | + visitor.EmitInt(lf.key, int(lf.numericVal)) |
| 181 | + case int32Type: |
| 182 | + visitor.EmitInt32(lf.key, int32(lf.numericVal)) |
| 183 | + case int64Type: |
| 184 | + visitor.EmitInt64(lf.key, int64(lf.numericVal)) |
| 185 | + case uint32Type: |
| 186 | + visitor.EmitUint32(lf.key, uint32(lf.numericVal)) |
| 187 | + case uint64Type: |
| 188 | + visitor.EmitUint64(lf.key, uint64(lf.numericVal)) |
| 189 | + case float32Type: |
| 190 | + visitor.EmitFloat32(lf.key, math.Float32frombits(uint32(lf.numericVal))) |
| 191 | + case float64Type: |
| 192 | + visitor.EmitFloat64(lf.key, math.Float64frombits(uint64(lf.numericVal))) |
| 193 | + case errorType: |
| 194 | + visitor.EmitString(lf.key, lf.interfaceVal.(error).Error()) |
| 195 | + case objectType: |
| 196 | + visitor.EmitObject(lf.key, lf.interfaceVal) |
| 197 | + case lazyLoggerType: |
| 198 | + visitor.EmitLazyLogger(lf.interfaceVal.(LazyLogger)) |
| 199 | + } |
| 200 | +} |
0 commit comments