...

Source file src/k8s.io/klog/v2/internal/sloghandler/sloghandler_slog.go

Documentation: k8s.io/klog/v2/internal/sloghandler

     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 sloghandler
    21  
    22  import (
    23  	"context"
    24  	"log/slog"
    25  	"runtime"
    26  	"strings"
    27  	"time"
    28  
    29  	"k8s.io/klog/v2/internal/severity"
    30  )
    31  
    32  func Handle(_ context.Context, record slog.Record, groups string, printWithInfos func(file string, line int, now time.Time, err error, s severity.Severity, msg string, kvList []interface{})) error {
    33  	now := record.Time
    34  	if now.IsZero() {
    35  		// This format doesn't support printing entries without a time.
    36  		now = time.Now()
    37  	}
    38  
    39  	// slog has numeric severity levels, with 0 as default "info", negative for debugging, and
    40  	// positive with some pre-defined levels for more important. Those ranges get mapped to
    41  	// the corresponding klog levels where possible, with "info" the default that is used
    42  	// also for negative debug levels.
    43  	level := record.Level
    44  	s := severity.InfoLog
    45  	switch {
    46  	case level >= slog.LevelError:
    47  		s = severity.ErrorLog
    48  	case level >= slog.LevelWarn:
    49  		s = severity.WarningLog
    50  	}
    51  
    52  	var file string
    53  	var line int
    54  	if record.PC != 0 {
    55  		// Same as https://cs.opensource.google/go/x/exp/+/642cacee:slog/record.go;drc=642cacee5cc05231f45555a333d07f1005ffc287;l=70
    56  		fs := runtime.CallersFrames([]uintptr{record.PC})
    57  		f, _ := fs.Next()
    58  		if f.File != "" {
    59  			file = f.File
    60  			if slash := strings.LastIndex(file, "/"); slash >= 0 {
    61  				file = file[slash+1:]
    62  			}
    63  			line = f.Line
    64  		}
    65  	} else {
    66  		file = "???"
    67  		line = 1
    68  	}
    69  
    70  	kvList := make([]interface{}, 0, 2*record.NumAttrs())
    71  	record.Attrs(func(attr slog.Attr) bool {
    72  		kvList = appendAttr(groups, kvList, attr)
    73  		return true
    74  	})
    75  
    76  	printWithInfos(file, line, now, nil, s, record.Message, kvList)
    77  	return nil
    78  }
    79  
    80  func Attrs2KVList(groups string, attrs []slog.Attr) []interface{} {
    81  	kvList := make([]interface{}, 0, 2*len(attrs))
    82  	for _, attr := range attrs {
    83  		kvList = appendAttr(groups, kvList, attr)
    84  	}
    85  	return kvList
    86  }
    87  
    88  func appendAttr(groups string, kvList []interface{}, attr slog.Attr) []interface{} {
    89  	var key string
    90  	if groups != "" {
    91  		key = groups + "." + attr.Key
    92  	} else {
    93  		key = attr.Key
    94  	}
    95  	return append(kvList, key, attr.Value)
    96  }
    97  

View as plain text