|
| 1 | +// Copyright (c) 2016 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package zap |
| 22 | + |
| 23 | +import "time" |
| 24 | + |
| 25 | +// A Field is a deferred marshaling operation used to add a key-value pair to |
| 26 | +// a logger's context. Keys and values are appropriately escaped for the current |
| 27 | +// encoding scheme (e.g., JSON). |
| 28 | +type Field interface { |
| 29 | + addTo(encoder) error |
| 30 | +} |
| 31 | + |
| 32 | +// Bool constructs a Field with the given key and value. |
| 33 | +func Bool(key string, val bool) Field { |
| 34 | + return boolField{key, val} |
| 35 | +} |
| 36 | + |
| 37 | +// Float64 constructs a Field with the given key and value. The floating-point |
| 38 | +// value is encoded using strconv.FormatFloat's 'g' option (exponential notation |
| 39 | +// for large exponents, grade-school notation otherwise). |
| 40 | +func Float64(key string, val float64) Field { |
| 41 | + return float64Field{key, val} |
| 42 | +} |
| 43 | + |
| 44 | +// Int constructs a Field with the given key and value. |
| 45 | +func Int(key string, val int) Field { |
| 46 | + return intField{key, val} |
| 47 | +} |
| 48 | + |
| 49 | +// Int64 constructs a Field with the given key and value. |
| 50 | +func Int64(key string, val int64) Field { |
| 51 | + return int64Field{key, val} |
| 52 | +} |
| 53 | + |
| 54 | +// String constructs a Field with the given key and value. |
| 55 | +func String(key string, val string) Field { |
| 56 | + return stringField{key, val} |
| 57 | +} |
| 58 | + |
| 59 | +// Time constructs a Field with the given key and value. It represents a |
| 60 | +// time.Time as nanoseconds since epoch. |
| 61 | +func Time(key string, val time.Time) Field { |
| 62 | + return timeField{key, val} |
| 63 | +} |
| 64 | + |
| 65 | +// Err constructs a Field that stores err.Error() under the key "error". |
| 66 | +func Err(err error) Field { |
| 67 | + return stringField{"error", err.Error()} |
| 68 | +} |
| 69 | + |
| 70 | +// Duration constructs a Field with the given key and value. It represents |
| 71 | +// durations as an integer number of nanoseconds. |
| 72 | +func Duration(key string, val time.Duration) Field { |
| 73 | + return int64Field{key, int64(val)} |
| 74 | +} |
| 75 | + |
| 76 | +// Object constructs a field with the given key and zap.Marshaler. It provides a |
| 77 | +// flexible, but still type-safe and efficient, way to add user-defined types to |
| 78 | +// the logging context. |
| 79 | +func Object(key string, val Marshaler) Field { |
| 80 | + return marshalerField{key, val} |
| 81 | +} |
| 82 | + |
| 83 | +// Nest takes a key and a variadic number of Fields and creates a nested |
| 84 | +// namespace. |
| 85 | +func Nest(key string, fields ...Field) Field { |
| 86 | + return nestedField{key, fields} |
| 87 | +} |
| 88 | + |
| 89 | +type boolField struct { |
| 90 | + key string |
| 91 | + val bool |
| 92 | +} |
| 93 | + |
| 94 | +func (b boolField) addTo(enc encoder) error { |
| 95 | + enc.AddBool(b.key, b.val) |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +type float64Field struct { |
| 100 | + key string |
| 101 | + val float64 |
| 102 | +} |
| 103 | + |
| 104 | +func (f float64Field) addTo(enc encoder) error { |
| 105 | + enc.AddFloat64(f.key, f.val) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +type intField struct { |
| 110 | + key string |
| 111 | + val int |
| 112 | +} |
| 113 | + |
| 114 | +func (i intField) addTo(enc encoder) error { |
| 115 | + enc.AddInt(i.key, i.val) |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +type int64Field struct { |
| 120 | + key string |
| 121 | + val int64 |
| 122 | +} |
| 123 | + |
| 124 | +func (i int64Field) addTo(enc encoder) error { |
| 125 | + enc.AddInt64(i.key, i.val) |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +type stringField struct { |
| 130 | + key string |
| 131 | + val string |
| 132 | +} |
| 133 | + |
| 134 | +func (s stringField) addTo(enc encoder) error { |
| 135 | + enc.AddString(s.key, s.val) |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +type timeField struct { |
| 140 | + key string |
| 141 | + val time.Time |
| 142 | +} |
| 143 | + |
| 144 | +func (t timeField) addTo(enc encoder) error { |
| 145 | + enc.AddTime(t.key, t.val) |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +type marshalerField struct { |
| 150 | + key string |
| 151 | + val Marshaler |
| 152 | +} |
| 153 | + |
| 154 | +func (m marshalerField) addTo(enc encoder) error { |
| 155 | + finish := enc.Nest(m.key) |
| 156 | + err := m.val.MarshalLog(enc) |
| 157 | + finish() |
| 158 | + return err |
| 159 | +} |
| 160 | + |
| 161 | +type nestedField struct { |
| 162 | + key string |
| 163 | + vals []Field |
| 164 | +} |
| 165 | + |
| 166 | +func (n nestedField) addTo(enc encoder) error { |
| 167 | + finish := enc.Nest(n.key) |
| 168 | + var errs multiError |
| 169 | + for _, f := range n.vals { |
| 170 | + if err := f.addTo(enc); err != nil { |
| 171 | + errs = append(errs, err) |
| 172 | + } |
| 173 | + } |
| 174 | + finish() |
| 175 | + if len(errs) > 0 { |
| 176 | + return errs |
| 177 | + } |
| 178 | + return nil |
| 179 | +} |
0 commit comments