2
0

klogr.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2021 The Kubernetes 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 klog
  14. import (
  15. "github.com/go-logr/logr"
  16. "k8s.io/klog/v2/internal/serialize"
  17. )
  18. const (
  19. // nameKey is used to log the `WithName` values as an additional attribute.
  20. nameKey = "logger"
  21. )
  22. // NewKlogr returns a logger that is functionally identical to
  23. // klogr.NewWithOptions(klogr.FormatKlog), i.e. it passes through to klog. The
  24. // difference is that it uses a simpler implementation.
  25. func NewKlogr() Logger {
  26. return New(&klogger{})
  27. }
  28. // klogger is a subset of klogr/klogr.go. It had to be copied to break an
  29. // import cycle (klogr wants to use klog, and klog wants to use klogr).
  30. type klogger struct {
  31. callDepth int
  32. // hasPrefix is true if the first entry in values is the special
  33. // nameKey key/value. Such an entry gets added and later updated in
  34. // WithName.
  35. hasPrefix bool
  36. values []interface{}
  37. groups string
  38. }
  39. func (l *klogger) Init(info logr.RuntimeInfo) {
  40. l.callDepth += info.CallDepth
  41. }
  42. func (l *klogger) Info(level int, msg string, kvList ...interface{}) {
  43. merged := serialize.MergeKVs(l.values, kvList)
  44. // Skip this function.
  45. VDepth(l.callDepth+1, Level(level)).InfoSDepth(l.callDepth+1, msg, merged...)
  46. }
  47. func (l *klogger) Enabled(level int) bool {
  48. return VDepth(l.callDepth+1, Level(level)).Enabled()
  49. }
  50. func (l *klogger) Error(err error, msg string, kvList ...interface{}) {
  51. merged := serialize.MergeKVs(l.values, kvList)
  52. ErrorSDepth(l.callDepth+1, err, msg, merged...)
  53. }
  54. // WithName returns a new logr.Logger with the specified name appended. klogr
  55. // uses '.' characters to separate name elements. Callers should not pass '.'
  56. // in the provided name string, but this library does not actually enforce that.
  57. func (l klogger) WithName(name string) logr.LogSink {
  58. if l.hasPrefix {
  59. // Copy slice and modify value. No length checks and type
  60. // assertions are needed because hasPrefix is only true if the
  61. // first two elements exist and are key/value strings.
  62. v := make([]interface{}, 0, len(l.values))
  63. v = append(v, l.values...)
  64. prefix, _ := v[1].(string)
  65. v[1] = prefix + "." + name
  66. l.values = v
  67. } else {
  68. // Preprend new key/value pair.
  69. v := make([]interface{}, 0, 2+len(l.values))
  70. v = append(v, nameKey, name)
  71. v = append(v, l.values...)
  72. l.values = v
  73. l.hasPrefix = true
  74. }
  75. return &l
  76. }
  77. func (l klogger) WithValues(kvList ...interface{}) logr.LogSink {
  78. l.values = serialize.WithValues(l.values, kvList)
  79. return &l
  80. }
  81. func (l klogger) WithCallDepth(depth int) logr.LogSink {
  82. l.callDepth += depth
  83. return &l
  84. }
  85. var _ logr.LogSink = &klogger{}
  86. var _ logr.CallDepthLogSink = &klogger{}