Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 zapcore |
| 22 | |
| 23 | import ( |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 24 | "encoding/json" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | "time" |
| 26 | |
| 27 | "go.uber.org/zap/buffer" |
| 28 | ) |
| 29 | |
| 30 | // DefaultLineEnding defines the default line ending when writing logs. |
| 31 | // Alternate line endings specified in EncoderConfig can override this |
| 32 | // behavior. |
| 33 | const DefaultLineEnding = "\n" |
| 34 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 35 | // OmitKey defines the key to use when callers want to remove a key from log output. |
| 36 | const OmitKey = "" |
| 37 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 38 | // A LevelEncoder serializes a Level to a primitive type. |
| 39 | type LevelEncoder func(Level, PrimitiveArrayEncoder) |
| 40 | |
| 41 | // LowercaseLevelEncoder serializes a Level to a lowercase string. For example, |
| 42 | // InfoLevel is serialized to "info". |
| 43 | func LowercaseLevelEncoder(l Level, enc PrimitiveArrayEncoder) { |
| 44 | enc.AppendString(l.String()) |
| 45 | } |
| 46 | |
| 47 | // LowercaseColorLevelEncoder serializes a Level to a lowercase string and adds coloring. |
| 48 | // For example, InfoLevel is serialized to "info" and colored blue. |
| 49 | func LowercaseColorLevelEncoder(l Level, enc PrimitiveArrayEncoder) { |
| 50 | s, ok := _levelToLowercaseColorString[l] |
| 51 | if !ok { |
| 52 | s = _unknownLevelColor.Add(l.String()) |
| 53 | } |
| 54 | enc.AppendString(s) |
| 55 | } |
| 56 | |
| 57 | // CapitalLevelEncoder serializes a Level to an all-caps string. For example, |
| 58 | // InfoLevel is serialized to "INFO". |
| 59 | func CapitalLevelEncoder(l Level, enc PrimitiveArrayEncoder) { |
| 60 | enc.AppendString(l.CapitalString()) |
| 61 | } |
| 62 | |
| 63 | // CapitalColorLevelEncoder serializes a Level to an all-caps string and adds color. |
| 64 | // For example, InfoLevel is serialized to "INFO" and colored blue. |
| 65 | func CapitalColorLevelEncoder(l Level, enc PrimitiveArrayEncoder) { |
| 66 | s, ok := _levelToCapitalColorString[l] |
| 67 | if !ok { |
| 68 | s = _unknownLevelColor.Add(l.CapitalString()) |
| 69 | } |
| 70 | enc.AppendString(s) |
| 71 | } |
| 72 | |
| 73 | // UnmarshalText unmarshals text to a LevelEncoder. "capital" is unmarshaled to |
| 74 | // CapitalLevelEncoder, "coloredCapital" is unmarshaled to CapitalColorLevelEncoder, |
| 75 | // "colored" is unmarshaled to LowercaseColorLevelEncoder, and anything else |
| 76 | // is unmarshaled to LowercaseLevelEncoder. |
| 77 | func (e *LevelEncoder) UnmarshalText(text []byte) error { |
| 78 | switch string(text) { |
| 79 | case "capital": |
| 80 | *e = CapitalLevelEncoder |
| 81 | case "capitalColor": |
| 82 | *e = CapitalColorLevelEncoder |
| 83 | case "color": |
| 84 | *e = LowercaseColorLevelEncoder |
| 85 | default: |
| 86 | *e = LowercaseLevelEncoder |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // A TimeEncoder serializes a time.Time to a primitive type. |
| 92 | type TimeEncoder func(time.Time, PrimitiveArrayEncoder) |
| 93 | |
| 94 | // EpochTimeEncoder serializes a time.Time to a floating-point number of seconds |
| 95 | // since the Unix epoch. |
| 96 | func EpochTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 97 | nanos := t.UnixNano() |
| 98 | sec := float64(nanos) / float64(time.Second) |
| 99 | enc.AppendFloat64(sec) |
| 100 | } |
| 101 | |
| 102 | // EpochMillisTimeEncoder serializes a time.Time to a floating-point number of |
| 103 | // milliseconds since the Unix epoch. |
| 104 | func EpochMillisTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 105 | nanos := t.UnixNano() |
| 106 | millis := float64(nanos) / float64(time.Millisecond) |
| 107 | enc.AppendFloat64(millis) |
| 108 | } |
| 109 | |
| 110 | // EpochNanosTimeEncoder serializes a time.Time to an integer number of |
| 111 | // nanoseconds since the Unix epoch. |
| 112 | func EpochNanosTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 113 | enc.AppendInt64(t.UnixNano()) |
| 114 | } |
| 115 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 116 | func encodeTimeLayout(t time.Time, layout string, enc PrimitiveArrayEncoder) { |
| 117 | type appendTimeEncoder interface { |
| 118 | AppendTimeLayout(time.Time, string) |
| 119 | } |
| 120 | |
| 121 | if enc, ok := enc.(appendTimeEncoder); ok { |
| 122 | enc.AppendTimeLayout(t, layout) |
| 123 | return |
| 124 | } |
| 125 | |
| 126 | enc.AppendString(t.Format(layout)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 127 | } |
| 128 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 129 | // ISO8601TimeEncoder serializes a time.Time to an ISO8601-formatted string |
| 130 | // with millisecond precision. |
| 131 | // |
| 132 | // If enc supports AppendTimeLayout(t time.Time,layout string), it's used |
| 133 | // instead of appending a pre-formatted string value. |
| 134 | func ISO8601TimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 135 | encodeTimeLayout(t, "2006-01-02T15:04:05.000Z0700", enc) |
| 136 | } |
| 137 | |
| 138 | // RFC3339TimeEncoder serializes a time.Time to an RFC3339-formatted string. |
| 139 | // |
| 140 | // If enc supports AppendTimeLayout(t time.Time,layout string), it's used |
| 141 | // instead of appending a pre-formatted string value. |
| 142 | func RFC3339TimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 143 | encodeTimeLayout(t, time.RFC3339, enc) |
| 144 | } |
| 145 | |
| 146 | // RFC3339NanoTimeEncoder serializes a time.Time to an RFC3339-formatted string |
| 147 | // with nanosecond precision. |
| 148 | // |
| 149 | // If enc supports AppendTimeLayout(t time.Time,layout string), it's used |
| 150 | // instead of appending a pre-formatted string value. |
| 151 | func RFC3339NanoTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { |
| 152 | encodeTimeLayout(t, time.RFC3339Nano, enc) |
| 153 | } |
| 154 | |
| 155 | // TimeEncoderOfLayout returns TimeEncoder which serializes a time.Time using |
| 156 | // given layout. |
| 157 | func TimeEncoderOfLayout(layout string) TimeEncoder { |
| 158 | return func(t time.Time, enc PrimitiveArrayEncoder) { |
| 159 | encodeTimeLayout(t, layout, enc) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // UnmarshalText unmarshals text to a TimeEncoder. |
| 164 | // "rfc3339nano" and "RFC3339Nano" are unmarshaled to RFC3339NanoTimeEncoder. |
| 165 | // "rfc3339" and "RFC3339" are unmarshaled to RFC3339TimeEncoder. |
| 166 | // "iso8601" and "ISO8601" are unmarshaled to ISO8601TimeEncoder. |
| 167 | // "millis" is unmarshaled to EpochMillisTimeEncoder. |
| 168 | // "nanos" is unmarshaled to EpochNanosEncoder. |
| 169 | // Anything else is unmarshaled to EpochTimeEncoder. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 170 | func (e *TimeEncoder) UnmarshalText(text []byte) error { |
| 171 | switch string(text) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 172 | case "rfc3339nano", "RFC3339Nano": |
| 173 | *e = RFC3339NanoTimeEncoder |
| 174 | case "rfc3339", "RFC3339": |
| 175 | *e = RFC3339TimeEncoder |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 176 | case "iso8601", "ISO8601": |
| 177 | *e = ISO8601TimeEncoder |
| 178 | case "millis": |
| 179 | *e = EpochMillisTimeEncoder |
| 180 | case "nanos": |
| 181 | *e = EpochNanosTimeEncoder |
| 182 | default: |
| 183 | *e = EpochTimeEncoder |
| 184 | } |
| 185 | return nil |
| 186 | } |
| 187 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 188 | // UnmarshalYAML unmarshals YAML to a TimeEncoder. |
| 189 | // If value is an object with a "layout" field, it will be unmarshaled to TimeEncoder with given layout. |
| 190 | // timeEncoder: |
| 191 | // layout: 06/01/02 03:04pm |
| 192 | // If value is string, it uses UnmarshalText. |
| 193 | // timeEncoder: iso8601 |
| 194 | func (e *TimeEncoder) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 195 | var o struct { |
| 196 | Layout string `json:"layout" yaml:"layout"` |
| 197 | } |
| 198 | if err := unmarshal(&o); err == nil { |
| 199 | *e = TimeEncoderOfLayout(o.Layout) |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | var s string |
| 204 | if err := unmarshal(&s); err != nil { |
| 205 | return err |
| 206 | } |
| 207 | return e.UnmarshalText([]byte(s)) |
| 208 | } |
| 209 | |
| 210 | // UnmarshalJSON unmarshals JSON to a TimeEncoder as same way UnmarshalYAML does. |
| 211 | func (e *TimeEncoder) UnmarshalJSON(data []byte) error { |
| 212 | return e.UnmarshalYAML(func(v interface{}) error { |
| 213 | return json.Unmarshal(data, v) |
| 214 | }) |
| 215 | } |
| 216 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 217 | // A DurationEncoder serializes a time.Duration to a primitive type. |
| 218 | type DurationEncoder func(time.Duration, PrimitiveArrayEncoder) |
| 219 | |
| 220 | // SecondsDurationEncoder serializes a time.Duration to a floating-point number of seconds elapsed. |
| 221 | func SecondsDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) { |
| 222 | enc.AppendFloat64(float64(d) / float64(time.Second)) |
| 223 | } |
| 224 | |
| 225 | // NanosDurationEncoder serializes a time.Duration to an integer number of |
| 226 | // nanoseconds elapsed. |
| 227 | func NanosDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) { |
| 228 | enc.AppendInt64(int64(d)) |
| 229 | } |
| 230 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 231 | // MillisDurationEncoder serializes a time.Duration to an integer number of |
| 232 | // milliseconds elapsed. |
| 233 | func MillisDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) { |
| 234 | enc.AppendInt64(d.Nanoseconds() / 1e6) |
| 235 | } |
| 236 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 237 | // StringDurationEncoder serializes a time.Duration using its built-in String |
| 238 | // method. |
| 239 | func StringDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) { |
| 240 | enc.AppendString(d.String()) |
| 241 | } |
| 242 | |
| 243 | // UnmarshalText unmarshals text to a DurationEncoder. "string" is unmarshaled |
| 244 | // to StringDurationEncoder, and anything else is unmarshaled to |
| 245 | // NanosDurationEncoder. |
| 246 | func (e *DurationEncoder) UnmarshalText(text []byte) error { |
| 247 | switch string(text) { |
| 248 | case "string": |
| 249 | *e = StringDurationEncoder |
| 250 | case "nanos": |
| 251 | *e = NanosDurationEncoder |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 252 | case "ms": |
| 253 | *e = MillisDurationEncoder |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 254 | default: |
| 255 | *e = SecondsDurationEncoder |
| 256 | } |
| 257 | return nil |
| 258 | } |
| 259 | |
| 260 | // A CallerEncoder serializes an EntryCaller to a primitive type. |
| 261 | type CallerEncoder func(EntryCaller, PrimitiveArrayEncoder) |
| 262 | |
| 263 | // FullCallerEncoder serializes a caller in /full/path/to/package/file:line |
| 264 | // format. |
| 265 | func FullCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) { |
| 266 | // TODO: consider using a byte-oriented API to save an allocation. |
| 267 | enc.AppendString(caller.String()) |
| 268 | } |
| 269 | |
| 270 | // ShortCallerEncoder serializes a caller in package/file:line format, trimming |
| 271 | // all but the final directory from the full path. |
| 272 | func ShortCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) { |
| 273 | // TODO: consider using a byte-oriented API to save an allocation. |
| 274 | enc.AppendString(caller.TrimmedPath()) |
| 275 | } |
| 276 | |
| 277 | // UnmarshalText unmarshals text to a CallerEncoder. "full" is unmarshaled to |
| 278 | // FullCallerEncoder and anything else is unmarshaled to ShortCallerEncoder. |
| 279 | func (e *CallerEncoder) UnmarshalText(text []byte) error { |
| 280 | switch string(text) { |
| 281 | case "full": |
| 282 | *e = FullCallerEncoder |
| 283 | default: |
| 284 | *e = ShortCallerEncoder |
| 285 | } |
| 286 | return nil |
| 287 | } |
| 288 | |
| 289 | // A NameEncoder serializes a period-separated logger name to a primitive |
| 290 | // type. |
| 291 | type NameEncoder func(string, PrimitiveArrayEncoder) |
| 292 | |
| 293 | // FullNameEncoder serializes the logger name as-is. |
| 294 | func FullNameEncoder(loggerName string, enc PrimitiveArrayEncoder) { |
| 295 | enc.AppendString(loggerName) |
| 296 | } |
| 297 | |
| 298 | // UnmarshalText unmarshals text to a NameEncoder. Currently, everything is |
| 299 | // unmarshaled to FullNameEncoder. |
| 300 | func (e *NameEncoder) UnmarshalText(text []byte) error { |
| 301 | switch string(text) { |
| 302 | case "full": |
| 303 | *e = FullNameEncoder |
| 304 | default: |
| 305 | *e = FullNameEncoder |
| 306 | } |
| 307 | return nil |
| 308 | } |
| 309 | |
| 310 | // An EncoderConfig allows users to configure the concrete encoders supplied by |
| 311 | // zapcore. |
| 312 | type EncoderConfig struct { |
| 313 | // Set the keys used for each log entry. If any key is empty, that portion |
| 314 | // of the entry is omitted. |
| 315 | MessageKey string `json:"messageKey" yaml:"messageKey"` |
| 316 | LevelKey string `json:"levelKey" yaml:"levelKey"` |
| 317 | TimeKey string `json:"timeKey" yaml:"timeKey"` |
| 318 | NameKey string `json:"nameKey" yaml:"nameKey"` |
| 319 | CallerKey string `json:"callerKey" yaml:"callerKey"` |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 320 | FunctionKey string `json:"functionKey" yaml:"functionKey"` |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 321 | StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"` |
| 322 | LineEnding string `json:"lineEnding" yaml:"lineEnding"` |
| 323 | // Configure the primitive representations of common complex types. For |
| 324 | // example, some users may want all time.Times serialized as floating-point |
| 325 | // seconds since epoch, while others may prefer ISO8601 strings. |
| 326 | EncodeLevel LevelEncoder `json:"levelEncoder" yaml:"levelEncoder"` |
| 327 | EncodeTime TimeEncoder `json:"timeEncoder" yaml:"timeEncoder"` |
| 328 | EncodeDuration DurationEncoder `json:"durationEncoder" yaml:"durationEncoder"` |
| 329 | EncodeCaller CallerEncoder `json:"callerEncoder" yaml:"callerEncoder"` |
| 330 | // Unlike the other primitive type encoders, EncodeName is optional. The |
| 331 | // zero value falls back to FullNameEncoder. |
| 332 | EncodeName NameEncoder `json:"nameEncoder" yaml:"nameEncoder"` |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 333 | // Configures the field separator used by the console encoder. Defaults |
| 334 | // to tab. |
| 335 | ConsoleSeparator string `json:"consoleSeparator" yaml:"consoleSeparator"` |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // ObjectEncoder is a strongly-typed, encoding-agnostic interface for adding a |
| 339 | // map- or struct-like object to the logging context. Like maps, ObjectEncoders |
| 340 | // aren't safe for concurrent use (though typical use shouldn't require locks). |
| 341 | type ObjectEncoder interface { |
| 342 | // Logging-specific marshalers. |
| 343 | AddArray(key string, marshaler ArrayMarshaler) error |
| 344 | AddObject(key string, marshaler ObjectMarshaler) error |
| 345 | |
| 346 | // Built-in types. |
| 347 | AddBinary(key string, value []byte) // for arbitrary bytes |
| 348 | AddByteString(key string, value []byte) // for UTF-8 encoded bytes |
| 349 | AddBool(key string, value bool) |
| 350 | AddComplex128(key string, value complex128) |
| 351 | AddComplex64(key string, value complex64) |
| 352 | AddDuration(key string, value time.Duration) |
| 353 | AddFloat64(key string, value float64) |
| 354 | AddFloat32(key string, value float32) |
| 355 | AddInt(key string, value int) |
| 356 | AddInt64(key string, value int64) |
| 357 | AddInt32(key string, value int32) |
| 358 | AddInt16(key string, value int16) |
| 359 | AddInt8(key string, value int8) |
| 360 | AddString(key, value string) |
| 361 | AddTime(key string, value time.Time) |
| 362 | AddUint(key string, value uint) |
| 363 | AddUint64(key string, value uint64) |
| 364 | AddUint32(key string, value uint32) |
| 365 | AddUint16(key string, value uint16) |
| 366 | AddUint8(key string, value uint8) |
| 367 | AddUintptr(key string, value uintptr) |
| 368 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 369 | // AddReflected uses reflection to serialize arbitrary objects, so it can be |
| 370 | // slow and allocation-heavy. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 371 | AddReflected(key string, value interface{}) error |
| 372 | // OpenNamespace opens an isolated namespace where all subsequent fields will |
| 373 | // be added. Applications can use namespaces to prevent key collisions when |
| 374 | // injecting loggers into sub-components or third-party libraries. |
| 375 | OpenNamespace(key string) |
| 376 | } |
| 377 | |
| 378 | // ArrayEncoder is a strongly-typed, encoding-agnostic interface for adding |
| 379 | // array-like objects to the logging context. Of note, it supports mixed-type |
| 380 | // arrays even though they aren't typical in Go. Like slices, ArrayEncoders |
| 381 | // aren't safe for concurrent use (though typical use shouldn't require locks). |
| 382 | type ArrayEncoder interface { |
| 383 | // Built-in types. |
| 384 | PrimitiveArrayEncoder |
| 385 | |
| 386 | // Time-related types. |
| 387 | AppendDuration(time.Duration) |
| 388 | AppendTime(time.Time) |
| 389 | |
| 390 | // Logging-specific marshalers. |
| 391 | AppendArray(ArrayMarshaler) error |
| 392 | AppendObject(ObjectMarshaler) error |
| 393 | |
| 394 | // AppendReflected uses reflection to serialize arbitrary objects, so it's |
| 395 | // slow and allocation-heavy. |
| 396 | AppendReflected(value interface{}) error |
| 397 | } |
| 398 | |
| 399 | // PrimitiveArrayEncoder is the subset of the ArrayEncoder interface that deals |
| 400 | // only in Go's built-in types. It's included only so that Duration- and |
| 401 | // TimeEncoders cannot trigger infinite recursion. |
| 402 | type PrimitiveArrayEncoder interface { |
| 403 | // Built-in types. |
| 404 | AppendBool(bool) |
| 405 | AppendByteString([]byte) // for UTF-8 encoded bytes |
| 406 | AppendComplex128(complex128) |
| 407 | AppendComplex64(complex64) |
| 408 | AppendFloat64(float64) |
| 409 | AppendFloat32(float32) |
| 410 | AppendInt(int) |
| 411 | AppendInt64(int64) |
| 412 | AppendInt32(int32) |
| 413 | AppendInt16(int16) |
| 414 | AppendInt8(int8) |
| 415 | AppendString(string) |
| 416 | AppendUint(uint) |
| 417 | AppendUint64(uint64) |
| 418 | AppendUint32(uint32) |
| 419 | AppendUint16(uint16) |
| 420 | AppendUint8(uint8) |
| 421 | AppendUintptr(uintptr) |
| 422 | } |
| 423 | |
| 424 | // Encoder is a format-agnostic interface for all log entry marshalers. Since |
| 425 | // log encoders don't need to support the same wide range of use cases as |
| 426 | // general-purpose marshalers, it's possible to make them faster and |
| 427 | // lower-allocation. |
| 428 | // |
| 429 | // Implementations of the ObjectEncoder interface's methods can, of course, |
| 430 | // freely modify the receiver. However, the Clone and EncodeEntry methods will |
| 431 | // be called concurrently and shouldn't modify the receiver. |
| 432 | type Encoder interface { |
| 433 | ObjectEncoder |
| 434 | |
| 435 | // Clone copies the encoder, ensuring that adding fields to the copy doesn't |
| 436 | // affect the original. |
| 437 | Clone() Encoder |
| 438 | |
| 439 | // EncodeEntry encodes an entry and fields, along with any accumulated |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 440 | // context, into a byte buffer and returns it. Any fields that are empty, |
| 441 | // including fields on the `Entry` type, should be omitted. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 442 | EncodeEntry(Entry, []Field) (*buffer.Buffer, error) |
| 443 | } |