funcr.go 23 KB

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