klogr_slog.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //go:build go1.21
  2. // +build go1.21
  3. /*
  4. Copyright 2023 The Kubernetes Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package klog
  16. import (
  17. "context"
  18. "log/slog"
  19. "strconv"
  20. "time"
  21. "github.com/go-logr/logr"
  22. "k8s.io/klog/v2/internal/buffer"
  23. "k8s.io/klog/v2/internal/serialize"
  24. "k8s.io/klog/v2/internal/severity"
  25. "k8s.io/klog/v2/internal/sloghandler"
  26. )
  27. func (l *klogger) Handle(ctx context.Context, record slog.Record) error {
  28. if logging.logger != nil {
  29. if slogSink, ok := logging.logger.GetSink().(logr.SlogSink); ok {
  30. // Let that logger do the work.
  31. return slogSink.Handle(ctx, record)
  32. }
  33. }
  34. return sloghandler.Handle(ctx, record, l.groups, slogOutput)
  35. }
  36. // slogOutput corresponds to several different functions in klog.go.
  37. // It goes through some of the same checks and formatting steps before
  38. // it ultimately converges by calling logging.printWithInfos.
  39. func slogOutput(file string, line int, now time.Time, err error, s severity.Severity, msg string, kvList []interface{}) {
  40. // See infoS.
  41. if logging.logger != nil {
  42. // Taking this path happens when klog has a logger installed
  43. // as backend which doesn't support slog. Not good, we have to
  44. // guess about the call depth and drop the actual location.
  45. logger := logging.logger.WithCallDepth(2)
  46. if s > severity.ErrorLog {
  47. logger.Error(err, msg, kvList...)
  48. } else {
  49. logger.Info(msg, kvList...)
  50. }
  51. return
  52. }
  53. // See printS.
  54. b := buffer.GetBuffer()
  55. b.WriteString(strconv.Quote(msg))
  56. if err != nil {
  57. serialize.KVListFormat(&b.Buffer, "err", err)
  58. }
  59. serialize.KVListFormat(&b.Buffer, kvList...)
  60. // See print + header.
  61. buf := logging.formatHeader(s, file, line, now)
  62. logging.printWithInfos(buf, file, line, s, nil, nil, 0, &b.Buffer)
  63. buffer.PutBuffer(b)
  64. }
  65. func (l *klogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
  66. clone := *l
  67. clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
  68. return &clone
  69. }
  70. func (l *klogger) WithGroup(name string) logr.SlogSink {
  71. clone := *l
  72. if clone.groups != "" {
  73. clone.groups += "." + name
  74. } else {
  75. clone.groups = name
  76. }
  77. return &clone
  78. }
  79. var _ logr.SlogSink = &klogger{}