encoder.go 14 KB

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