...

Source file src/k8s.io/klog/v2/textlogger/example_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  	"bytes"
    21  	"fmt"
    22  	"regexp"
    23  
    24  	"k8s.io/klog/v2/textlogger"
    25  )
    26  
    27  var headerRe = regexp.MustCompile(`([IE])[[:digit:]]{4} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\.[[:digit:]]{6}[[:space:]]+[[:digit:]]+ example_test.go:[[:digit:]]+\] `)
    28  
    29  func ExampleConfig_Verbosity() {
    30  	var buffer bytes.Buffer
    31  	config := textlogger.NewConfig(textlogger.Verbosity(1), textlogger.Output(&buffer))
    32  	logger := textlogger.NewLogger(config)
    33  
    34  	logger.Info("initial verbosity", "v", config.Verbosity().String())
    35  	logger.V(2).Info("now you don't see me")
    36  	if err := config.Verbosity().Set("2"); err != nil {
    37  		logger.Error(err, "setting verbosity to 2")
    38  	}
    39  	logger.V(2).Info("now you see me")
    40  	if err := config.Verbosity().Set("1"); err != nil {
    41  		logger.Error(err, "setting verbosity to 1")
    42  	}
    43  	logger.V(2).Info("now I'm gone again")
    44  
    45  	fmt.Print(headerRe.ReplaceAllString(buffer.String(), "${1}...] "))
    46  
    47  	// Output:
    48  	// I...] "initial verbosity" v="1"
    49  	// I...] "now you see me"
    50  }
    51  

View as plain text