formatter_json.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package logger
  2. import (
  3. "encoding/json"
  4. "log/slog"
  5. "time"
  6. "unicode/utf8"
  7. )
  8. const (
  9. jsonGroupOpenToken = '{'
  10. jsonGroupCloseToken = '}'
  11. )
  12. var jsonAttributeSep = []byte(",")
  13. // formatterJSON is a JSON log formatter.
  14. type formatterJSON struct {
  15. formatterCommon
  16. levelKey string
  17. messageKey string
  18. sep []byte
  19. groupsOpened int
  20. }
  21. // newFormatterJSON creates a new formatterJSON instance.
  22. func newFormatterJSON(groups []attrGroup, buf *buffer, gcpStyle bool) *formatterJSON {
  23. f := &formatterJSON{
  24. formatterCommon: newFormatterCommon(groups, buf),
  25. }
  26. // Set the level and message keys based on the style.
  27. if gcpStyle {
  28. f.levelKey = "severity"
  29. f.messageKey = "message"
  30. } else {
  31. f.levelKey = slog.LevelKey
  32. f.messageKey = slog.MessageKey
  33. }
  34. return f
  35. }
  36. // format formats a log record.
  37. func (s *formatterJSON) format(r slog.Record) {
  38. // Open the JSON object and defer closing it.
  39. s.buf.append(jsonGroupOpenToken)
  40. defer func() {
  41. s.buf.append(jsonGroupCloseToken)
  42. }()
  43. // Append timestamp
  44. s.appendKey(slog.TimeKey)
  45. s.appendTime(r.Time)
  46. // Append log level
  47. s.appendKey(s.levelKey)
  48. s.appendString(s.levelName(r.Level))
  49. // Append message
  50. s.appendKey(s.messageKey)
  51. s.appendString(r.Message)
  52. // Append groups added with [Handler.WithAttrs] and [Handler.WithGroup]
  53. for _, g := range s.groups {
  54. if g.name != "" {
  55. s.openGroup(g.name)
  56. }
  57. s.appendAttributes(g.attrs)
  58. }
  59. // Append attributes from the record
  60. r.Attrs(func(attr slog.Attr) bool {
  61. s.appendAttribute(attr)
  62. return true
  63. })
  64. // Close all opened groups.
  65. for s.groupsOpened > 0 {
  66. s.closeGroup()
  67. }
  68. // Append error, source, and stack if present
  69. if s.error.Key != "" {
  70. s.appendKey(s.error.Key)
  71. s.appendValue(s.error.Value)
  72. }
  73. if s.source.Key != "" {
  74. s.appendKey(s.source.Key)
  75. s.appendValue(s.source.Value)
  76. }
  77. if s.stack.Key != "" {
  78. s.appendKey(s.stack.Key)
  79. s.appendValue(s.stack.Value)
  80. }
  81. }
  82. // appendAttributes appends a list of attributes to the buffer.
  83. func (s *formatterJSON) appendAttributes(attrs []slog.Attr) {
  84. for _, attr := range attrs {
  85. s.appendAttribute(attr)
  86. }
  87. }
  88. // appendAttribute appends a single attribute to the buffer.
  89. func (s *formatterJSON) appendAttribute(attr slog.Attr) {
  90. // Resolve [slog.LogValuer] values
  91. attr.Value = attr.Value.Resolve()
  92. // If there are no groups opened, save special attributes for later
  93. if s.groupsOpened == 0 && s.saveSpecialAttr(attr) {
  94. return
  95. }
  96. // Groups need special handling
  97. if attr.Value.Kind() == slog.KindGroup {
  98. s.appendGroup(attr.Key, attr.Value.Group())
  99. return
  100. }
  101. s.appendKey(attr.Key)
  102. s.appendValue(attr.Value)
  103. }
  104. // appendKey appends an attribute key to the buffer.
  105. func (s *formatterJSON) appendKey(key string) {
  106. s.buf.append(s.sep...)
  107. s.sep = jsonAttributeSep
  108. s.appendString(key)
  109. s.buf.append(':')
  110. }
  111. // appendValue appends a value to the buffer, applying quoting rules as necessary.
  112. func (s *formatterJSON) appendValue(val slog.Value) {
  113. switch val.Kind() {
  114. case slog.KindString:
  115. s.appendString(val.String())
  116. case slog.KindInt64:
  117. s.buf.appendInt(val.Int64())
  118. case slog.KindUint64:
  119. s.buf.appendUint(val.Uint64())
  120. case slog.KindFloat64:
  121. // strconv.FormatFloat result sometimes differs from json.Marshal,
  122. // so we use json.Marshal for consistency.
  123. s.appendJSONMarshal(val.Float64())
  124. case slog.KindBool:
  125. s.buf.appendBool(val.Bool())
  126. case slog.KindDuration:
  127. s.buf.appendInt(int64(val.Duration()))
  128. case slog.KindTime:
  129. s.appendTime(val.Time())
  130. default:
  131. s.appendJSONMarshal(val.Any())
  132. }
  133. }
  134. // appendString appends a string value to the buffer.
  135. // If the string does not require escaping, it is appended directly.
  136. // Otherwise, it is JSON marshaled.
  137. func (s *formatterJSON) appendString(val string) {
  138. if !s.isStringSafe(val) {
  139. s.appendJSONMarshal(val)
  140. return
  141. }
  142. s.buf.append('"')
  143. s.buf.appendStringRaw(val)
  144. s.buf.append('"')
  145. }
  146. // isStringSafe checks if a string is safe to append without escaping.
  147. func (s *formatterJSON) isStringSafe(val string) bool {
  148. for i := 0; i < len(val); i++ {
  149. if b := val[i]; b >= utf8.RuneSelf || !jsonSafeSet[b] {
  150. return false
  151. }
  152. }
  153. return true
  154. }
  155. // appendTime appends a time value to the buffer.
  156. func (s *formatterJSON) appendTime(val time.Time) {
  157. s.buf.append('"')
  158. s.buf.appendStringRaw(val.Format(time.RFC3339))
  159. s.buf.append('"')
  160. }
  161. // appendJSONMarshal appends a JSON marshaled value to the buffer.
  162. func (s *formatterJSON) appendJSONMarshal(val any) {
  163. if err, ok := val.(error); ok && err != nil {
  164. s.appendString(err.Error())
  165. return
  166. }
  167. buf := newBuffer()
  168. defer func() {
  169. buf.free()
  170. }()
  171. enc := json.NewEncoder(buf)
  172. enc.SetEscapeHTML(false)
  173. if err := enc.Encode(val); err != nil {
  174. // This should be a very unlikely situation, but just in case...
  175. s.buf.appendStringRaw(`"<json marshal error>"`)
  176. return
  177. }
  178. buf.removeNewline()
  179. s.buf.append(*buf...)
  180. }
  181. // appendGroup appends a group of attributes to the buffer.
  182. func (s *formatterJSON) appendGroup(name string, attrs []slog.Attr) {
  183. if len(attrs) == 0 {
  184. return
  185. }
  186. if len(name) > 0 {
  187. // If the group has a name, open it and defer closing it.
  188. // Unnamed groups should be treated as sets of regular attributes.
  189. s.openGroup(name)
  190. defer s.closeGroup()
  191. }
  192. s.appendAttributes(attrs)
  193. }
  194. // openGroup opens a new group in the buffer.
  195. func (s *formatterJSON) openGroup(name string) {
  196. s.groupsOpened++
  197. s.appendKey(name)
  198. s.buf.append(jsonGroupOpenToken)
  199. s.sep = nil
  200. }
  201. // closeGroup closes the most recently opened group in the buffer.
  202. func (s *formatterJSON) closeGroup() {
  203. s.groupsOpened--
  204. s.buf.append(jsonGroupCloseToken)
  205. s.sep = jsonAttributeSep
  206. }
  207. // jsonSafeSet is a set of runes that are safe to include in JSON strings without escaping.
  208. // Some runes here are explicitly marked as unsafe for clarity.
  209. // The unlisted runes are considered unsafe by default.
  210. // Shamesly stolen from https://github.com/golang/go/blob/master/src/encoding/json/tables.go.
  211. var jsonSafeSet = [utf8.RuneSelf]bool{
  212. ' ': true,
  213. '!': true,
  214. '"': false,
  215. '#': true,
  216. '$': true,
  217. '%': true,
  218. '&': true,
  219. '\'': true,
  220. '(': true,
  221. ')': true,
  222. '*': true,
  223. '+': true,
  224. ',': true,
  225. '-': true,
  226. '.': true,
  227. '/': true,
  228. '0': true,
  229. '1': true,
  230. '2': true,
  231. '3': true,
  232. '4': true,
  233. '5': true,
  234. '6': true,
  235. '7': true,
  236. '8': true,
  237. '9': true,
  238. ':': true,
  239. ';': true,
  240. '<': true,
  241. '=': true,
  242. '>': true,
  243. '?': true,
  244. '@': true,
  245. 'A': true,
  246. 'B': true,
  247. 'C': true,
  248. 'D': true,
  249. 'E': true,
  250. 'F': true,
  251. 'G': true,
  252. 'H': true,
  253. 'I': true,
  254. 'J': true,
  255. 'K': true,
  256. 'L': true,
  257. 'M': true,
  258. 'N': true,
  259. 'O': true,
  260. 'P': true,
  261. 'Q': true,
  262. 'R': true,
  263. 'S': true,
  264. 'T': true,
  265. 'U': true,
  266. 'V': true,
  267. 'W': true,
  268. 'X': true,
  269. 'Y': true,
  270. 'Z': true,
  271. '[': true,
  272. '\\': false,
  273. ']': true,
  274. '^': true,
  275. '_': true,
  276. '`': true,
  277. 'a': true,
  278. 'b': true,
  279. 'c': true,
  280. 'd': true,
  281. 'e': true,
  282. 'f': true,
  283. 'g': true,
  284. 'h': true,
  285. 'i': true,
  286. 'j': true,
  287. 'k': true,
  288. 'l': true,
  289. 'm': true,
  290. 'n': true,
  291. 'o': true,
  292. 'p': true,
  293. 'q': true,
  294. 'r': true,
  295. 's': true,
  296. 't': true,
  297. 'u': true,
  298. 'v': true,
  299. 'w': true,
  300. 'x': true,
  301. 'y': true,
  302. 'z': true,
  303. '{': true,
  304. '|': true,
  305. '}': true,
  306. '~': true,
  307. '\u007f': true,
  308. }