funcr.go 24 KB

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