formatter_structured.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package logger
  2. import (
  3. "log/slog"
  4. )
  5. // formatterStructured is a flat structured log formatter.
  6. type formatterStructured struct {
  7. formatterCommon
  8. // Current group prefix
  9. prefix *buffer
  10. }
  11. // newFormatterStructured creates a new formatterStructured instance.
  12. func newFormatterStructured(groups []attrGroup, buf *buffer) *formatterStructured {
  13. return &formatterStructured{
  14. formatterCommon: newFormatterCommon(groups, buf),
  15. }
  16. }
  17. // format formats a log record.
  18. func (s *formatterStructured) format(r slog.Record) {
  19. s.prefix = newBuffer()
  20. defer func() {
  21. s.prefix.free()
  22. }()
  23. // Append timestamp
  24. s.appendKey(slog.TimeKey)
  25. s.appendTime(r.Time)
  26. // Append log level
  27. s.appendKey(slog.LevelKey)
  28. s.appendString(s.levelName(r.Level), true)
  29. // Append message
  30. s.appendKey(slog.MessageKey)
  31. s.appendString(r.Message, true)
  32. // Append groups added with [Handler.WithAttrs] and [Handler.WithGroup]
  33. for _, g := range s.groups {
  34. if g.name != "" {
  35. s.openGroup(g.name)
  36. }
  37. s.appendAttributes(g.attrs)
  38. }
  39. // Append attributes from the record
  40. r.Attrs(func(attr slog.Attr) bool {
  41. s.appendAttribute(attr)
  42. return true
  43. })
  44. // Append error, source, and stack if present
  45. if s.error.Key != "" {
  46. s.appendKey(s.error.Key)
  47. s.appendValue(s.error.Value, false)
  48. }
  49. if s.source.Key != "" {
  50. s.appendKey(s.source.Key)
  51. s.appendValue(s.source.Value, false)
  52. }
  53. if s.stack.Key != "" {
  54. s.appendKey(s.stack.Key)
  55. s.appendValue(s.stack.Value, false)
  56. }
  57. }
  58. // appendAttributes appends a list of attributes to the buffer.
  59. func (s *formatterStructured) appendAttributes(attrs []slog.Attr) {
  60. for _, attr := range attrs {
  61. s.appendAttribute(attr)
  62. }
  63. }
  64. // appendAttribute appends a single attribute to the buffer.
  65. func (s *formatterStructured) appendAttribute(attr slog.Attr) {
  66. // Resolve [slog.LogValuer] values
  67. attr.Value = attr.Value.Resolve()
  68. // If there are no groups opened, save special attributes for later
  69. if s.prefix.len() == 0 && s.saveSpecialAttr(attr) {
  70. return
  71. }
  72. // Groups need special handling
  73. if attr.Value.Kind() == slog.KindGroup {
  74. s.appendGroup(attr.Key, attr.Value.Group())
  75. return
  76. }
  77. s.appendKey(attr.Key)
  78. s.appendValue(attr.Value, true)
  79. }
  80. // appendKey appends an attribute key to the buffer.
  81. func (s *formatterStructured) appendKey(key string) {
  82. if len(*s.buf) > 0 {
  83. s.buf.append(' ')
  84. }
  85. s.buf.appendString(s.prefix.String() + key)
  86. s.buf.append('=')
  87. }
  88. // appendGroup appends a group of attributes to the buffer.
  89. func (s *formatterStructured) appendGroup(name string, attrs []slog.Attr) {
  90. if len(attrs) == 0 {
  91. return
  92. }
  93. if len(name) > 0 {
  94. // If the group has a name, open it and defer closing it.
  95. // Unnamed groups should be treated as sets of regular attributes.
  96. s.openGroup(name)
  97. defer s.closeGroup(name)
  98. }
  99. s.appendAttributes(attrs)
  100. }
  101. // openGroup opens a new group of attributes.
  102. func (s *formatterStructured) openGroup(name string) {
  103. s.prefix.appendStringRaw(name)
  104. s.prefix.append('.')
  105. }
  106. // closeGroup closes the most recently opened group of attributes.
  107. func (s *formatterStructured) closeGroup(name string) {
  108. s.prefix.remove(len(name) + 1) // +1 for the dot
  109. }