klogr.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // NewKlogr returns a logger that is functionally identical to
  19. // klogr.NewWithOptions(klogr.FormatKlog), i.e. it passes through to klog. The
  20. // difference is that it uses a simpler implementation.
  21. func NewKlogr() Logger {
  22. return New(&klogger{})
  23. }
  24. // klogger is a subset of klogr/klogr.go. It had to be copied to break an
  25. // import cycle (klogr wants to use klog, and klog wants to use klogr).
  26. type klogger struct {
  27. level int
  28. callDepth int
  29. prefix string
  30. values []interface{}
  31. }
  32. func (l *klogger) Init(info logr.RuntimeInfo) {
  33. l.callDepth += info.CallDepth
  34. }
  35. func (l *klogger) Info(level int, msg string, kvList ...interface{}) {
  36. merged := serialize.MergeKVs(l.values, kvList)
  37. if l.prefix != "" {
  38. msg = l.prefix + ": " + msg
  39. }
  40. // Skip this function.
  41. VDepth(l.callDepth+1, Level(level)).InfoSDepth(l.callDepth+1, msg, merged...)
  42. }
  43. func (l *klogger) Enabled(level int) bool {
  44. // Skip this function and logr.Logger.Info where Enabled is called.
  45. return VDepth(l.callDepth+2, Level(level)).Enabled()
  46. }
  47. func (l *klogger) Error(err error, msg string, kvList ...interface{}) {
  48. merged := serialize.MergeKVs(l.values, kvList)
  49. if l.prefix != "" {
  50. msg = l.prefix + ": " + msg
  51. }
  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 len(l.prefix) > 0 {
  59. l.prefix = l.prefix + "/"
  60. }
  61. l.prefix += name
  62. return &l
  63. }
  64. func (l klogger) WithValues(kvList ...interface{}) logr.LogSink {
  65. l.values = serialize.WithValues(l.values, kvList)
  66. return &l
  67. }
  68. func (l klogger) WithCallDepth(depth int) logr.LogSink {
  69. l.callDepth += depth
  70. return &l
  71. }
  72. var _ logr.LogSink = &klogger{}
  73. var _ logr.CallDepthLogSink = &klogger{}