...

Source file src/k8s.io/klog/v2/textlogger/textlogger_slog_test.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_test
    21  
    22  import (
    23  	"errors"
    24  	"log/slog"
    25  	"os"
    26  	"time"
    27  
    28  	"github.com/go-logr/logr/slogr"
    29  	internal "k8s.io/klog/v2/internal/buffer"
    30  	"k8s.io/klog/v2/textlogger"
    31  )
    32  
    33  var _ slog.LogValuer = coordinates{}
    34  
    35  type coordinates struct {
    36  	x, y int
    37  }
    38  
    39  func (c coordinates) LogValue() slog.Value {
    40  	return slog.GroupValue(slog.Attr{Key: "X", Value: slog.IntValue(c.x)}, slog.Attr{Key: "Y", Value: slog.IntValue(c.y)})
    41  }
    42  
    43  func ExampleNewLogger_Slog() {
    44  	ts, _ := time.Parse(time.RFC3339, "2000-12-24T12:30:40Z")
    45  	internal.Pid = 123 // To get consistent output for each run.
    46  	config := textlogger.NewConfig(
    47  		textlogger.FixedTime(ts), // To get consistent output for each run.
    48  		textlogger.Verbosity(4),  // Matches slog.LevelDebug.
    49  		textlogger.Output(os.Stdout),
    50  	)
    51  	logrLogger := textlogger.NewLogger(config)
    52  	slogHandler := slogr.NewSlogHandler(logrLogger)
    53  	slogLogger := slog.New(slogHandler)
    54  
    55  	slogLogger.Debug("A debug message")
    56  	slogLogger.Log(nil, slog.LevelDebug-1, "A debug message with even lower level, not printed.")
    57  	slogLogger.Info("An info message")
    58  	slogLogger.Warn("A warning")
    59  	slogLogger.Error("An error", "err", errors.New("fake error"))
    60  
    61  	// The slog API supports grouping, in contrast to the logr API.
    62  	slogLogger.WithGroup("top").With("int", 42, slog.Group("variables", "a", 1, "b", 2)).Info("Grouping",
    63  		"sub", slog.GroupValue(
    64  			slog.Attr{Key: "str", Value: slog.StringValue("abc")},
    65  			slog.Attr{Key: "bool", Value: slog.BoolValue(true)},
    66  			slog.Attr{Key: "bottom", Value: slog.GroupValue(slog.Attr{Key: "coordinates", Value: slog.AnyValue(coordinates{x: -1, y: -2})})},
    67  		),
    68  		"duration", slog.DurationValue(time.Second),
    69  		slog.Float64("pi", 3.12),
    70  		"e", 2.71,
    71  		"moreCoordinates", coordinates{x: 100, y: 200},
    72  	)
    73  
    74  	// slog special values are also supported when passed through the logr API.
    75  	// This works with the textlogger, but might not work with other implementations
    76  	// and thus isn't portable. Passing attributes (= key and value in a single
    77  	// parameter) is not supported.
    78  	logrLogger.Info("slog values",
    79  		"variables", slog.GroupValue(slog.Int("a", 1), slog.Int("b", 2)),
    80  		"duration", slog.DurationValue(time.Second),
    81  		"coordinates", coordinates{x: 100, y: 200},
    82  	)
    83  
    84  	// Output:
    85  	// I1224 12:30:40.000000     123 textlogger_slog_test.go:55] "A debug message"
    86  	// I1224 12:30:40.000000     123 textlogger_slog_test.go:57] "An info message"
    87  	// W1224 12:30:40.000000     123 textlogger_slog_test.go:58] "A warning"
    88  	// E1224 12:30:40.000000     123 textlogger_slog_test.go:59] "An error" err="fake error"
    89  	// I1224 12:30:40.000000     123 textlogger_slog_test.go:62] "Grouping" top.int=42 top.variables={"a":1,"b":2} top.sub={"str":"abc","bool":true,"bottom":{"coordinates":{"X":-1,"Y":-2}}} top.duration="1s" top.pi=3.12 top.e=2.71 top.moreCoordinates={"X":100,"Y":200}
    90  	// I1224 12:30:40.000000     123 textlogger_slog_test.go:78] "slog values" variables={"a":1,"b":2} duration="1s" coordinates={"X":100,"Y":200}
    91  }
    92  

View as plain text