...

Source file src/k8s.io/klog/v2/klogr_slog.go

Documentation: k8s.io/klog/v2

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

View as plain text