funcr.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. Copyright 2021 The logr Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package funcr implements formatting of structured log messages and
  14. // optionally captures the call site and timestamp.
  15. //
  16. // The simplest way to use it is via its implementation of a
  17. // github.com/go-logr/logr.LogSink with output through an arbitrary
  18. // "write" function. See New and NewJSON for details.
  19. //
  20. // # Custom LogSinks
  21. //
  22. // For users who need more control, a funcr.Formatter can be embedded inside
  23. // your own custom LogSink implementation. This is useful when the LogSink
  24. // needs to implement additional methods, for example.
  25. //
  26. // # Formatting
  27. //
  28. // This will respect logr.Marshaler, fmt.Stringer, and error interfaces for
  29. // values which are being logged. When rendering a struct, funcr will use Go's
  30. // standard JSON tags (all except "string").
  31. package funcr
  32. import (
  33. "bytes"
  34. "encoding"
  35. "encoding/json"
  36. "fmt"
  37. "path/filepath"
  38. "reflect"
  39. "runtime"
  40. "strconv"
  41. "strings"
  42. "time"
  43. "github.com/go-logr/logr"
  44. )
  45. // New returns a logr.Logger which is implemented by an arbitrary function.
  46. func New(fn func(prefix, args string), opts Options) logr.Logger {
  47. return logr.New(newSink(fn, NewFormatter(opts)))
  48. }
  49. // NewJSON returns a logr.Logger which is implemented by an arbitrary function
  50. // and produces JSON output.
  51. func NewJSON(fn func(obj string), opts Options) logr.Logger {
  52. fnWrapper := func(_, obj string) {
  53. fn(obj)
  54. }
  55. return logr.New(newSink(fnWrapper, NewFormatterJSON(opts)))
  56. }
  57. // Underlier exposes access to the underlying logging function. Since
  58. // callers only have a logr.Logger, they have to know which
  59. // implementation is in use, so this interface is less of an
  60. // abstraction and more of a way to test type conversion.
  61. type Underlier interface {
  62. GetUnderlying() func(prefix, args string)
  63. }
  64. func newSink(fn func(prefix, args string), formatter Formatter) logr.LogSink {
  65. l := &fnlogger{
  66. Formatter: formatter,
  67. write: fn,
  68. }
  69. // For skipping fnlogger.Info and fnlogger.Error.
  70. l.Formatter.AddCallDepth(1)
  71. return l
  72. }
  73. // Options carries parameters which influence the way logs are generated.
  74. type Options struct {
  75. // LogCaller tells funcr to add a "caller" key to some or all log lines.
  76. // This has some overhead, so some users might not want it.
  77. LogCaller MessageClass
  78. // LogCallerFunc tells funcr to also log the calling function name. This
  79. // has no effect if caller logging is not enabled (see Options.LogCaller).
  80. LogCallerFunc bool
  81. // LogTimestamp tells funcr to add a "ts" key to log lines. This has some
  82. // overhead, so some users might not want it.
  83. LogTimestamp bool
  84. // TimestampFormat tells funcr how to render timestamps when LogTimestamp
  85. // is enabled. If not specified, a default format will be used. For more
  86. // details, see docs for Go's time.Layout.
  87. TimestampFormat string
  88. // LogInfoLevel tells funcr what key to use to log the info level.
  89. // If not specified, the info level will be logged as "level".
  90. // If this is set to "", the info level will not be logged at all.
  91. LogInfoLevel *string
  92. // Verbosity tells funcr which V logs to produce. Higher values enable
  93. // more logs. Info logs at or below this level will be written, while logs
  94. // above this level will be discarded.
  95. Verbosity int
  96. // RenderBuiltinsHook allows users to mutate the list of key-value pairs
  97. // while a log line is being rendered. The kvList argument follows logr
  98. // conventions - each pair of slice elements is comprised of a string key
  99. // and an arbitrary value (verified and sanitized before calling this
  100. // hook). The value returned must follow the same conventions. This hook
  101. // can be used to audit or modify logged data. For example, you might want
  102. // to prefix all of funcr's built-in keys with some string. This hook is
  103. // only called for built-in (provided by funcr itself) key-value pairs.
  104. // Equivalent hooks are offered for key-value pairs saved via
  105. // logr.Logger.WithValues or Formatter.AddValues (see RenderValuesHook) and
  106. // for user-provided pairs (see RenderArgsHook).
  107. RenderBuiltinsHook func(kvList []any) []any
  108. // RenderValuesHook is the same as RenderBuiltinsHook, except that it is
  109. // only called for key-value pairs saved via logr.Logger.WithValues. See
  110. // RenderBuiltinsHook for more details.
  111. RenderValuesHook func(kvList []any) []any
  112. // RenderArgsHook is the same as RenderBuiltinsHook, except that it is only
  113. // called for key-value pairs passed directly to Info and Error. See
  114. // RenderBuiltinsHook for more details.
  115. RenderArgsHook func(kvList []any) []any
  116. // MaxLogDepth tells funcr how many levels of nested fields (e.g. a struct
  117. // that contains a struct, etc.) it may log. Every time it finds a struct,
  118. // slice, array, or map the depth is increased by one. When the maximum is
  119. // reached, the value will be converted to a string indicating that the max
  120. // depth has been exceeded. If this field is not specified, a default
  121. // value will be used.
  122. MaxLogDepth int
  123. }
  124. // MessageClass indicates which category or categories of messages to consider.
  125. type MessageClass int
  126. const (
  127. // None ignores all message classes.
  128. None MessageClass = iota
  129. // All considers all message classes.
  130. All
  131. // Info only considers info messages.
  132. Info
  133. // Error only considers error messages.
  134. Error
  135. )
  136. // fnlogger inherits some of its LogSink implementation from Formatter
  137. // and just needs to add some glue code.
  138. type fnlogger struct {
  139. Formatter
  140. write func(prefix, args string)
  141. }
  142. func (l fnlogger) WithName(name string) logr.LogSink {
  143. l.Formatter.AddName(name)
  144. return &l
  145. }
  146. func (l fnlogger) WithValues(kvList ...any) logr.LogSink {
  147. l.Formatter.AddValues(kvList)
  148. return &l
  149. }
  150. func (l fnlogger) WithCallDepth(depth int) logr.LogSink {
  151. l.Formatter.AddCallDepth(depth)
  152. return &l
  153. }
  154. func (l fnlogger) Info(level int, msg string, kvList ...any) {
  155. prefix, args := l.FormatInfo(level, msg, kvList)
  156. l.write(prefix, args)
  157. }
  158. func (l fnlogger) Error(err error, msg string, kvList ...any) {
  159. prefix, args := l.FormatError(err, msg, kvList)
  160. l.write(prefix, args)
  161. }
  162. func (l fnlogger) GetUnderlying() func(prefix, args string) {
  163. return l.write
  164. }
  165. // Assert conformance to the interfaces.
  166. var _ logr.LogSink = &fnlogger{}
  167. var _ logr.CallDepthLogSink = &fnlogger{}
  168. var _ Underlier = &fnlogger{}
  169. // NewFormatter constructs a Formatter which emits a JSON-like key=value format.
  170. func NewFormatter(opts Options) Formatter {
  171. return newFormatter(opts, outputKeyValue)
  172. }
  173. // NewFormatterJSON constructs a Formatter which emits strict JSON.
  174. func NewFormatterJSON(opts Options) Formatter {
  175. return newFormatter(opts, outputJSON)
  176. }
  177. // Defaults for Options.
  178. const defaultTimestampFormat = "2006-01-02 15:04:05.000000"
  179. const defaultMaxLogDepth = 16
  180. func newFormatter(opts Options, outfmt outputFormat) Formatter {
  181. if opts.TimestampFormat == "" {
  182. opts.TimestampFormat = defaultTimestampFormat
  183. }
  184. if opts.MaxLogDepth == 0 {
  185. opts.MaxLogDepth = defaultMaxLogDepth
  186. }
  187. if opts.LogInfoLevel == nil {
  188. opts.LogInfoLevel = new(string)
  189. *opts.LogInfoLevel = "level"
  190. }
  191. f := Formatter{
  192. outputFormat: outfmt,
  193. prefix: "",
  194. values: nil,
  195. depth: 0,
  196. opts: &opts,
  197. }
  198. return f
  199. }
  200. // Formatter is an opaque struct which can be embedded in a LogSink
  201. // implementation. It should be constructed with NewFormatter. Some of
  202. // its methods directly implement logr.LogSink.
  203. type Formatter struct {
  204. outputFormat outputFormat
  205. prefix string
  206. values []any
  207. valuesStr string
  208. depth int
  209. opts *Options
  210. groupName string // for slog groups
  211. groups []groupDef
  212. }
  213. // outputFormat indicates which outputFormat to use.
  214. type outputFormat int
  215. const (
  216. // outputKeyValue emits a JSON-like key=value format, but not strict JSON.
  217. outputKeyValue outputFormat = iota
  218. // outputJSON emits strict JSON.
  219. outputJSON
  220. )
  221. // groupDef represents a saved group. The values may be empty, but we don't
  222. // know if we need to render the group until the final record is rendered.
  223. type groupDef struct {
  224. name string
  225. values string
  226. }
  227. // PseudoStruct is a list of key-value pairs that gets logged as a struct.
  228. type PseudoStruct []any
  229. // render produces a log line, ready to use.
  230. func (f Formatter) render(builtins, args []any) string {
  231. // Empirically bytes.Buffer is faster than strings.Builder for this.
  232. buf := bytes.NewBuffer(make([]byte, 0, 1024))
  233. if f.outputFormat == outputJSON {
  234. buf.WriteByte('{') // for the whole record
  235. }
  236. // Render builtins
  237. vals := builtins
  238. if hook := f.opts.RenderBuiltinsHook; hook != nil {
  239. vals = hook(f.sanitize(vals))
  240. }
  241. f.flatten(buf, vals, false) // keys are ours, no need to escape
  242. continuing := len(builtins) > 0
  243. // Turn the inner-most group into a string
  244. argsStr := func() string {
  245. buf := bytes.NewBuffer(make([]byte, 0, 1024))
  246. vals = args
  247. if hook := f.opts.RenderArgsHook; hook != nil {
  248. vals = hook(f.sanitize(vals))
  249. }
  250. f.flatten(buf, vals, true) // escape user-provided keys
  251. return buf.String()
  252. }()
  253. // Render the stack of groups from the inside out.
  254. bodyStr := f.renderGroup(f.groupName, f.valuesStr, argsStr)
  255. for i := len(f.groups) - 1; i >= 0; i-- {
  256. grp := &f.groups[i]
  257. if grp.values == "" && bodyStr == "" {
  258. // no contents, so we must elide the whole group
  259. continue
  260. }
  261. bodyStr = f.renderGroup(grp.name, grp.values, bodyStr)
  262. }
  263. if bodyStr != "" {
  264. if continuing {
  265. buf.WriteByte(f.comma())
  266. }
  267. buf.WriteString(bodyStr)
  268. }
  269. if f.outputFormat == outputJSON {
  270. buf.WriteByte('}') // for the whole record
  271. }
  272. return buf.String()
  273. }
  274. // renderGroup returns a string representation of the named group with rendered
  275. // values and args. If the name is empty, this will return the values and args,
  276. // joined. If the name is not empty, this will return a single key-value pair,
  277. // where the value is a grouping of the values and args. If the values and
  278. // args are both empty, this will return an empty string, even if the name was
  279. // specified.
  280. func (f Formatter) renderGroup(name string, values string, args string) string {
  281. buf := bytes.NewBuffer(make([]byte, 0, 1024))
  282. needClosingBrace := false
  283. if name != "" && (values != "" || args != "") {
  284. buf.WriteString(f.quoted(name, true)) // escape user-provided keys
  285. buf.WriteByte(f.colon())
  286. buf.WriteByte('{')
  287. needClosingBrace = true
  288. }
  289. continuing := false
  290. if values != "" {
  291. buf.WriteString(values)
  292. continuing = true
  293. }
  294. if args != "" {
  295. if continuing {
  296. buf.WriteByte(f.comma())
  297. }
  298. buf.WriteString(args)
  299. }
  300. if needClosingBrace {
  301. buf.WriteByte('}')
  302. }
  303. return buf.String()
  304. }
  305. // flatten renders a list of key-value pairs into a buffer. If escapeKeys is
  306. // true, the keys are assumed to have non-JSON-compatible characters in them
  307. // and must be evaluated for escapes.
  308. //
  309. // This function returns a potentially modified version of kvList, which
  310. // ensures that there is a value for every key (adding a value if needed) and
  311. // that each key is a string (substituting a key if needed).
  312. func (f Formatter) flatten(buf *bytes.Buffer, kvList []any, escapeKeys bool) []any {
  313. // This logic overlaps with sanitize() but saves one type-cast per key,
  314. // which can be measurable.
  315. if len(kvList)%2 != 0 {
  316. kvList = append(kvList, noValue)
  317. }
  318. copied := false
  319. for i := 0; i < len(kvList); i += 2 {
  320. k, ok := kvList[i].(string)
  321. if !ok {
  322. if !copied {
  323. newList := make([]any, len(kvList))
  324. copy(newList, kvList)
  325. kvList = newList
  326. copied = true
  327. }
  328. k = f.nonStringKey(kvList[i])
  329. kvList[i] = k
  330. }
  331. v := kvList[i+1]
  332. if i > 0 {
  333. if f.outputFormat == outputJSON {
  334. buf.WriteByte(f.comma())
  335. } else {
  336. // In theory the format could be something we don't understand. In
  337. // practice, we control it, so it won't be.
  338. buf.WriteByte(' ')
  339. }
  340. }
  341. buf.WriteString(f.quoted(k, escapeKeys))
  342. buf.WriteByte(f.colon())
  343. buf.WriteString(f.pretty(v))
  344. }
  345. return kvList
  346. }
  347. func (f Formatter) quoted(str string, escape bool) string {
  348. if escape {
  349. return prettyString(str)
  350. }
  351. // this is faster
  352. return `"` + str + `"`
  353. }
  354. func (f Formatter) comma() byte {
  355. if f.outputFormat == outputJSON {
  356. return ','
  357. }
  358. return ' '
  359. }
  360. func (f Formatter) colon() byte {
  361. if f.outputFormat == outputJSON {
  362. return ':'
  363. }
  364. return '='
  365. }
  366. func (f Formatter) pretty(value any) string {
  367. return f.prettyWithFlags(value, 0, 0)
  368. }
  369. const (
  370. flagRawStruct = 0x1 // do not print braces on structs
  371. )
  372. // TODO: This is not fast. Most of the overhead goes here.
  373. func (f Formatter) prettyWithFlags(value any, flags uint32, depth int) string {
  374. if depth > f.opts.MaxLogDepth {
  375. return `"<max-log-depth-exceeded>"`
  376. }
  377. // Handle types that take full control of logging.
  378. if v, ok := value.(logr.Marshaler); ok {
  379. // Replace the value with what the type wants to get logged.
  380. // That then gets handled below via reflection.
  381. value = invokeMarshaler(v)
  382. }
  383. // Handle types that want to format themselves.
  384. switch v := value.(type) {
  385. case fmt.Stringer:
  386. value = invokeStringer(v)
  387. case error:
  388. value = invokeError(v)
  389. }
  390. // Handling the most common types without reflect is a small perf win.
  391. switch v := value.(type) {
  392. case bool:
  393. return strconv.FormatBool(v)
  394. case string:
  395. return prettyString(v)
  396. case int:
  397. return strconv.FormatInt(int64(v), 10)
  398. case int8:
  399. return strconv.FormatInt(int64(v), 10)
  400. case int16:
  401. return strconv.FormatInt(int64(v), 10)
  402. case int32:
  403. return strconv.FormatInt(int64(v), 10)
  404. case int64:
  405. return strconv.FormatInt(int64(v), 10)
  406. case uint:
  407. return strconv.FormatUint(uint64(v), 10)
  408. case uint8:
  409. return strconv.FormatUint(uint64(v), 10)
  410. case uint16:
  411. return strconv.FormatUint(uint64(v), 10)
  412. case uint32:
  413. return strconv.FormatUint(uint64(v), 10)
  414. case uint64:
  415. return strconv.FormatUint(v, 10)
  416. case uintptr:
  417. return strconv.FormatUint(uint64(v), 10)
  418. case float32:
  419. return strconv.FormatFloat(float64(v), 'f', -1, 32)
  420. case float64:
  421. return strconv.FormatFloat(v, 'f', -1, 64)
  422. case complex64:
  423. return `"` + strconv.FormatComplex(complex128(v), 'f', -1, 64) + `"`
  424. case complex128:
  425. return `"` + strconv.FormatComplex(v, 'f', -1, 128) + `"`
  426. case PseudoStruct:
  427. buf := bytes.NewBuffer(make([]byte, 0, 1024))
  428. v = f.sanitize(v)
  429. if flags&flagRawStruct == 0 {
  430. buf.WriteByte('{')
  431. }
  432. for i := 0; i < len(v); i += 2 {
  433. if i > 0 {
  434. buf.WriteByte(f.comma())
  435. }
  436. k, _ := v[i].(string) // sanitize() above means no need to check success
  437. // arbitrary keys might need escaping
  438. buf.WriteString(prettyString(k))
  439. buf.WriteByte(f.colon())
  440. buf.WriteString(f.prettyWithFlags(v[i+1], 0, depth+1))
  441. }
  442. if flags&flagRawStruct == 0 {
  443. buf.WriteByte('}')
  444. }
  445. return buf.String()
  446. }
  447. buf := bytes.NewBuffer(make([]byte, 0, 256))
  448. t := reflect.TypeOf(value)
  449. if t == nil {
  450. return "null"
  451. }
  452. v := reflect.ValueOf(value)
  453. switch t.Kind() {
  454. case reflect.Bool:
  455. return strconv.FormatBool(v.Bool())
  456. case reflect.String:
  457. return prettyString(v.String())
  458. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  459. return strconv.FormatInt(int64(v.Int()), 10)
  460. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  461. return strconv.FormatUint(uint64(v.Uint()), 10)
  462. case reflect.Float32:
  463. return strconv.FormatFloat(float64(v.Float()), 'f', -1, 32)
  464. case reflect.Float64:
  465. return strconv.FormatFloat(v.Float(), 'f', -1, 64)
  466. case reflect.Complex64:
  467. return `"` + strconv.FormatComplex(complex128(v.Complex()), 'f', -1, 64) + `"`
  468. case reflect.Complex128:
  469. return `"` + strconv.FormatComplex(v.Complex(), 'f', -1, 128) + `"`
  470. case reflect.Struct:
  471. if flags&flagRawStruct == 0 {
  472. buf.WriteByte('{')
  473. }
  474. printComma := false // testing i>0 is not enough because of JSON omitted fields
  475. for i := 0; i < t.NumField(); i++ {
  476. fld := t.Field(i)
  477. if fld.PkgPath != "" {
  478. // reflect says this field is only defined for non-exported fields.
  479. continue
  480. }
  481. if !v.Field(i).CanInterface() {
  482. // reflect isn't clear exactly what this means, but we can't use it.
  483. continue
  484. }
  485. name := ""
  486. omitempty := false
  487. if tag, found := fld.Tag.Lookup("json"); found {
  488. if tag == "-" {
  489. continue
  490. }
  491. if comma := strings.Index(tag, ","); comma != -1 {
  492. if n := tag[:comma]; n != "" {
  493. name = n
  494. }
  495. rest := tag[comma:]
  496. if strings.Contains(rest, ",omitempty,") || strings.HasSuffix(rest, ",omitempty") {
  497. omitempty = true
  498. }
  499. } else {
  500. name = tag
  501. }
  502. }
  503. if omitempty && isEmpty(v.Field(i)) {
  504. continue
  505. }
  506. if printComma {
  507. buf.WriteByte(f.comma())
  508. }
  509. printComma = true // if we got here, we are rendering a field
  510. if fld.Anonymous && fld.Type.Kind() == reflect.Struct && name == "" {
  511. buf.WriteString(f.prettyWithFlags(v.Field(i).Interface(), flags|flagRawStruct, depth+1))
  512. continue
  513. }
  514. if name == "" {
  515. name = fld.Name
  516. }
  517. // field names can't contain characters which need escaping
  518. buf.WriteString(f.quoted(name, false))
  519. buf.WriteByte(f.colon())
  520. buf.WriteString(f.prettyWithFlags(v.Field(i).Interface(), 0, depth+1))
  521. }
  522. if flags&flagRawStruct == 0 {
  523. buf.WriteByte('}')
  524. }
  525. return buf.String()
  526. case reflect.Slice, reflect.Array:
  527. // If this is outputing as JSON make sure this isn't really a json.RawMessage.
  528. // If so just emit "as-is" and don't pretty it as that will just print
  529. // it as [X,Y,Z,...] which isn't terribly useful vs the string form you really want.
  530. if f.outputFormat == outputJSON {
  531. if rm, ok := value.(json.RawMessage); ok {
  532. // If it's empty make sure we emit an empty value as the array style would below.
  533. if len(rm) > 0 {
  534. buf.Write(rm)
  535. } else {
  536. buf.WriteString("null")
  537. }
  538. return buf.String()
  539. }
  540. }
  541. buf.WriteByte('[')
  542. for i := 0; i < v.Len(); i++ {
  543. if i > 0 {
  544. buf.WriteByte(f.comma())
  545. }
  546. e := v.Index(i)
  547. buf.WriteString(f.prettyWithFlags(e.Interface(), 0, depth+1))
  548. }
  549. buf.WriteByte(']')
  550. return buf.String()
  551. case reflect.Map:
  552. buf.WriteByte('{')
  553. // This does not sort the map keys, for best perf.
  554. it := v.MapRange()
  555. i := 0
  556. for it.Next() {
  557. if i > 0 {
  558. buf.WriteByte(f.comma())
  559. }
  560. // If a map key supports TextMarshaler, use it.
  561. keystr := ""
  562. if m, ok := it.Key().Interface().(encoding.TextMarshaler); ok {
  563. txt, err := m.MarshalText()
  564. if err != nil {
  565. keystr = fmt.Sprintf("<error-MarshalText: %s>", err.Error())
  566. } else {
  567. keystr = string(txt)
  568. }
  569. keystr = prettyString(keystr)
  570. } else {
  571. // prettyWithFlags will produce already-escaped values
  572. keystr = f.prettyWithFlags(it.Key().Interface(), 0, depth+1)
  573. if t.Key().Kind() != reflect.String {
  574. // JSON only does string keys. Unlike Go's standard JSON, we'll
  575. // convert just about anything to a string.
  576. keystr = prettyString(keystr)
  577. }
  578. }
  579. buf.WriteString(keystr)
  580. buf.WriteByte(f.colon())
  581. buf.WriteString(f.prettyWithFlags(it.Value().Interface(), 0, depth+1))
  582. i++
  583. }
  584. buf.WriteByte('}')
  585. return buf.String()
  586. case reflect.Ptr, reflect.Interface:
  587. if v.IsNil() {
  588. return "null"
  589. }
  590. return f.prettyWithFlags(v.Elem().Interface(), 0, depth)
  591. }
  592. return fmt.Sprintf(`"<unhandled-%s>"`, t.Kind().String())
  593. }
  594. func prettyString(s string) string {
  595. // Avoid escaping (which does allocations) if we can.
  596. if needsEscape(s) {
  597. return strconv.Quote(s)
  598. }
  599. b := bytes.NewBuffer(make([]byte, 0, 1024))
  600. b.WriteByte('"')
  601. b.WriteString(s)
  602. b.WriteByte('"')
  603. return b.String()
  604. }
  605. // needsEscape determines whether the input string needs to be escaped or not,
  606. // without doing any allocations.
  607. func needsEscape(s string) bool {
  608. for _, r := range s {
  609. if !strconv.IsPrint(r) || r == '\\' || r == '"' {
  610. return true
  611. }
  612. }
  613. return false
  614. }
  615. func isEmpty(v reflect.Value) bool {
  616. switch v.Kind() {
  617. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  618. return v.Len() == 0
  619. case reflect.Bool:
  620. return !v.Bool()
  621. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  622. return v.Int() == 0
  623. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  624. return v.Uint() == 0
  625. case reflect.Float32, reflect.Float64:
  626. return v.Float() == 0
  627. case reflect.Complex64, reflect.Complex128:
  628. return v.Complex() == 0
  629. case reflect.Interface, reflect.Ptr:
  630. return v.IsNil()
  631. }
  632. return false
  633. }
  634. func invokeMarshaler(m logr.Marshaler) (ret any) {
  635. defer func() {
  636. if r := recover(); r != nil {
  637. ret = fmt.Sprintf("<panic: %s>", r)
  638. }
  639. }()
  640. return m.MarshalLog()
  641. }
  642. func invokeStringer(s fmt.Stringer) (ret string) {
  643. defer func() {
  644. if r := recover(); r != nil {
  645. ret = fmt.Sprintf("<panic: %s>", r)
  646. }
  647. }()
  648. return s.String()
  649. }
  650. func invokeError(e error) (ret string) {
  651. defer func() {
  652. if r := recover(); r != nil {
  653. ret = fmt.Sprintf("<panic: %s>", r)
  654. }
  655. }()
  656. return e.Error()
  657. }
  658. // Caller represents the original call site for a log line, after considering
  659. // logr.Logger.WithCallDepth and logr.Logger.WithCallStackHelper. The File and
  660. // Line fields will always be provided, while the Func field is optional.
  661. // Users can set the render hook fields in Options to examine logged key-value
  662. // pairs, one of which will be {"caller", Caller} if the Options.LogCaller
  663. // field is enabled for the given MessageClass.
  664. type Caller struct {
  665. // File is the basename of the file for this call site.
  666. File string `json:"file"`
  667. // Line is the line number in the file for this call site.
  668. Line int `json:"line"`
  669. // Func is the function name for this call site, or empty if
  670. // Options.LogCallerFunc is not enabled.
  671. Func string `json:"function,omitempty"`
  672. }
  673. func (f Formatter) caller() Caller {
  674. // +1 for this frame, +1 for Info/Error.
  675. pc, file, line, ok := runtime.Caller(f.depth + 2)
  676. if !ok {
  677. return Caller{"<unknown>", 0, ""}
  678. }
  679. fn := ""
  680. if f.opts.LogCallerFunc {
  681. if fp := runtime.FuncForPC(pc); fp != nil {
  682. fn = fp.Name()
  683. }
  684. }
  685. return Caller{filepath.Base(file), line, fn}
  686. }
  687. const noValue = "<no-value>"
  688. func (f Formatter) nonStringKey(v any) string {
  689. return fmt.Sprintf("<non-string-key: %s>", f.snippet(v))
  690. }
  691. // snippet produces a short snippet string of an arbitrary value.
  692. func (f Formatter) snippet(v any) string {
  693. const snipLen = 16
  694. snip := f.pretty(v)
  695. if len(snip) > snipLen {
  696. snip = snip[:snipLen]
  697. }
  698. return snip
  699. }
  700. // sanitize ensures that a list of key-value pairs has a value for every key
  701. // (adding a value if needed) and that each key is a string (substituting a key
  702. // if needed).
  703. func (f Formatter) sanitize(kvList []any) []any {
  704. if len(kvList)%2 != 0 {
  705. kvList = append(kvList, noValue)
  706. }
  707. for i := 0; i < len(kvList); i += 2 {
  708. _, ok := kvList[i].(string)
  709. if !ok {
  710. kvList[i] = f.nonStringKey(kvList[i])
  711. }
  712. }
  713. return kvList
  714. }
  715. // startGroup opens a new group scope (basically a sub-struct), which locks all
  716. // the current saved values and starts them anew. This is needed to satisfy
  717. // slog.
  718. func (f *Formatter) startGroup(name string) {
  719. // Unnamed groups are just inlined.
  720. if name == "" {
  721. return
  722. }
  723. n := len(f.groups)
  724. f.groups = append(f.groups[:n:n], groupDef{f.groupName, f.valuesStr})
  725. // Start collecting new values.
  726. f.groupName = name
  727. f.valuesStr = ""
  728. f.values = nil
  729. }
  730. // Init configures this Formatter from runtime info, such as the call depth
  731. // imposed by logr itself.
  732. // Note that this receiver is a pointer, so depth can be saved.
  733. func (f *Formatter) Init(info logr.RuntimeInfo) {
  734. f.depth += info.CallDepth
  735. }
  736. // Enabled checks whether an info message at the given level should be logged.
  737. func (f Formatter) Enabled(level int) bool {
  738. return level <= f.opts.Verbosity
  739. }
  740. // GetDepth returns the current depth of this Formatter. This is useful for
  741. // implementations which do their own caller attribution.
  742. func (f Formatter) GetDepth() int {
  743. return f.depth
  744. }
  745. // FormatInfo renders an Info log message into strings. The prefix will be
  746. // empty when no names were set (via AddNames), or when the output is
  747. // configured for JSON.
  748. func (f Formatter) FormatInfo(level int, msg string, kvList []any) (prefix, argsStr string) {
  749. args := make([]any, 0, 64) // using a constant here impacts perf
  750. prefix = f.prefix
  751. if f.outputFormat == outputJSON {
  752. args = append(args, "logger", prefix)
  753. prefix = ""
  754. }
  755. if f.opts.LogTimestamp {
  756. args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
  757. }
  758. if policy := f.opts.LogCaller; policy == All || policy == Info {
  759. args = append(args, "caller", f.caller())
  760. }
  761. if key := *f.opts.LogInfoLevel; key != "" {
  762. args = append(args, key, level)
  763. }
  764. args = append(args, "msg", msg)
  765. return prefix, f.render(args, kvList)
  766. }
  767. // FormatError renders an Error log message into strings. The prefix will be
  768. // empty when no names were set (via AddNames), or when the output is
  769. // configured for JSON.
  770. func (f Formatter) FormatError(err error, msg string, kvList []any) (prefix, argsStr string) {
  771. args := make([]any, 0, 64) // using a constant here impacts perf
  772. prefix = f.prefix
  773. if f.outputFormat == outputJSON {
  774. args = append(args, "logger", prefix)
  775. prefix = ""
  776. }
  777. if f.opts.LogTimestamp {
  778. args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
  779. }
  780. if policy := f.opts.LogCaller; policy == All || policy == Error {
  781. args = append(args, "caller", f.caller())
  782. }
  783. args = append(args, "msg", msg)
  784. var loggableErr any
  785. if err != nil {
  786. loggableErr = err.Error()
  787. }
  788. args = append(args, "error", loggableErr)
  789. return prefix, f.render(args, kvList)
  790. }
  791. // AddName appends the specified name. funcr uses '/' characters to separate
  792. // name elements. Callers should not pass '/' in the provided name string, but
  793. // this library does not actually enforce that.
  794. func (f *Formatter) AddName(name string) {
  795. if len(f.prefix) > 0 {
  796. f.prefix += "/"
  797. }
  798. f.prefix += name
  799. }
  800. // AddValues adds key-value pairs to the set of saved values to be logged with
  801. // each log line.
  802. func (f *Formatter) AddValues(kvList []any) {
  803. // Three slice args forces a copy.
  804. n := len(f.values)
  805. f.values = append(f.values[:n:n], kvList...)
  806. vals := f.values
  807. if hook := f.opts.RenderValuesHook; hook != nil {
  808. vals = hook(f.sanitize(vals))
  809. }
  810. // Pre-render values, so we don't have to do it on each Info/Error call.
  811. buf := bytes.NewBuffer(make([]byte, 0, 1024))
  812. f.flatten(buf, vals, true) // escape user-provided keys
  813. f.valuesStr = buf.String()
  814. }
  815. // AddCallDepth increases the number of stack-frames to skip when attributing
  816. // the log line to a file and line.
  817. func (f *Formatter) AddCallDepth(depth int) {
  818. f.depth += depth
  819. }