...

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

Documentation: k8s.io/klog/v2/textlogger

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package textlogger_test
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"time"
    23  
    24  	"github.com/go-logr/logr"
    25  	"k8s.io/klog/v2"
    26  	internal "k8s.io/klog/v2/internal/buffer"
    27  	"k8s.io/klog/v2/textlogger"
    28  )
    29  
    30  var _ logr.Marshaler = coordinatesMarshaler{}
    31  
    32  type coordinatesMarshaler struct {
    33  	x, y int
    34  }
    35  
    36  func (c coordinatesMarshaler) MarshalLog() interface{} {
    37  	return map[string]int{"X": c.x, "Y": c.y}
    38  }
    39  
    40  type variables struct {
    41  	A, B int
    42  }
    43  
    44  func ExampleNewLogger() {
    45  	ts, _ := time.Parse(time.RFC3339, "2000-12-24T12:30:40Z")
    46  	internal.Pid = 123 // To get consistent output for each run.
    47  	config := textlogger.NewConfig(
    48  		textlogger.FixedTime(ts), // To get consistent output for each run.
    49  		textlogger.Verbosity(4),  // Matches Kubernetes "debug" level.
    50  		textlogger.Output(os.Stdout),
    51  	)
    52  	logger := textlogger.NewLogger(config)
    53  
    54  	logger.V(4).Info("A debug message")
    55  	logger.V(5).Info("A debug message with even lower level, not printed.")
    56  	logger.Info("An info message")
    57  	logger.Error(errors.New("fake error"), "An error")
    58  	logger.WithValues("int", 42).Info("With values",
    59  		"duration", time.Second,
    60  		"float", 3.12,
    61  		"coordinates", coordinatesMarshaler{x: 100, y: 200},
    62  		"variables", variables{A: 1, B: 2},
    63  	)
    64  	// The logr API supports skipping functions during stack unwinding, in contrast to slog.
    65  	someHelper(logger, "hello world")
    66  
    67  	// Output:
    68  	// I1224 12:30:40.000000     123 textlogger_test.go:54] "A debug message"
    69  	// I1224 12:30:40.000000     123 textlogger_test.go:56] "An info message"
    70  	// E1224 12:30:40.000000     123 textlogger_test.go:57] "An error" err="fake error"
    71  	// I1224 12:30:40.000000     123 textlogger_test.go:58] "With values" int=42 duration="1s" float=3.12 coordinates={"X":100,"Y":200} variables={"A":1,"B":2}
    72  	// I1224 12:30:40.000000     123 textlogger_test.go:65] "hello world"
    73  }
    74  
    75  func someHelper(logger klog.Logger, msg string) {
    76  	logger.WithCallDepth(1).Info(msg)
    77  }
    78  
    79  func ExampleBacktrace() {
    80  	ts, _ := time.Parse(time.RFC3339, "2000-12-24T12:30:40Z")
    81  	internal.Pid = 123 // To get consistent output for each run.
    82  	backtraceCounter := 0
    83  	config := textlogger.NewConfig(
    84  		textlogger.FixedTime(ts), // To get consistent output for each run.
    85  		textlogger.Backtrace(func(_ /* skip */ int) (filename string, line int) {
    86  			backtraceCounter++
    87  			if backtraceCounter == 1 {
    88  				// Simulate "missing information".
    89  				return "", 0
    90  			}
    91  			return "fake.go", 42
    92  
    93  			// A real implementation could use Ginkgo:
    94  			//
    95  			// import ginkgotypes "github.com/onsi/ginkgo/v2/types"
    96  			//
    97  			// location := ginkgotypes.NewCodeLocation(skip + 1)
    98  			// return location.FileName, location.LineNumber
    99  		}),
   100  		textlogger.Output(os.Stdout),
   101  	)
   102  	logger := textlogger.NewLogger(config)
   103  
   104  	logger.Info("First message")
   105  	logger.Info("Second message")
   106  
   107  	// Output:
   108  	// I1224 12:30:40.000000     123 ???:1] "First message"
   109  	// I1224 12:30:40.000000     123 fake.go:42] "Second message"
   110  }
   111  

View as plain text