encoder.go 16 KB

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