encoder.go 13 KB

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