...

Source file src/k8s.io/klog/v2/textlogger/textlogger_slog.go

Documentation: k8s.io/klog/v2/textlogger

     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 textlogger
    21  
    22  import (
    23  	"context"
    24  	"log/slog"
    25  
    26  	"github.com/go-logr/logr"
    27  
    28  	"k8s.io/klog/v2/internal/serialize"
    29  	"k8s.io/klog/v2/internal/sloghandler"
    30  )
    31  
    32  func (l *tlogger) Handle(ctx context.Context, record slog.Record) error {
    33  	return sloghandler.Handle(ctx, record, l.groups, l.printWithInfos)
    34  }
    35  
    36  func (l *tlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
    37  	clone := *l
    38  	clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
    39  	return &clone
    40  }
    41  
    42  func (l *tlogger) WithGroup(name string) logr.SlogSink {
    43  	clone := *l
    44  	if clone.groups != "" {
    45  		clone.groups += "." + name
    46  	} else {
    47  		clone.groups = name
    48  	}
    49  	return &clone
    50  }
    51  
    52  var _ logr.SlogSink = &tlogger{}
    53  

View as plain text