...

Source file src/github.com/go-logr/logr/funcr/example_test.go

Documentation: github.com/go-logr/logr/funcr

     1  /*
     2  Copyright 2021 The logr 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 funcr_test
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/go-logr/logr"
    23  	"github.com/go-logr/logr/funcr"
    24  )
    25  
    26  func ExampleNew() {
    27  	var log logr.Logger = funcr.New(func(prefix, args string) {
    28  		fmt.Println(prefix, args)
    29  	}, funcr.Options{})
    30  
    31  	log = log.WithName("MyLogger")
    32  	log = log.WithValues("savedKey", "savedValue")
    33  	log.Info("the message", "key", "value")
    34  	// Output: MyLogger "level"=0 "msg"="the message" "savedKey"="savedValue" "key"="value"
    35  }
    36  
    37  func ExampleNewJSON() {
    38  	var log logr.Logger = funcr.NewJSON(func(obj string) {
    39  		fmt.Println(obj)
    40  	}, funcr.Options{})
    41  
    42  	log = log.WithName("MyLogger")
    43  	log = log.WithValues("savedKey", "savedValue")
    44  	log.Info("the message", "key", "value")
    45  	// Output: {"logger":"MyLogger","level":0,"msg":"the message","savedKey":"savedValue","key":"value"}
    46  }
    47  
    48  func ExampleUnderlier() {
    49  	var log logr.Logger = funcr.New(func(prefix, args string) {
    50  		fmt.Println(prefix, args)
    51  	}, funcr.Options{})
    52  
    53  	if underlier, ok := log.GetSink().(funcr.Underlier); ok {
    54  		fn := underlier.GetUnderlying()
    55  		fn("hello", "world")
    56  	}
    57  	// Output: hello world
    58  }
    59  
    60  func ExampleOptions() {
    61  	var log logr.Logger = funcr.NewJSON(
    62  		func(obj string) { fmt.Println(obj) },
    63  		funcr.Options{
    64  			LogCaller: funcr.All,
    65  			Verbosity: 1, // V(2) and higher is ignored.
    66  		})
    67  	log.V(0).Info("V(0) message", "key", "value")
    68  	log.V(1).Info("V(1) message", "key", "value")
    69  	log.V(2).Info("V(2) message", "key", "value")
    70  	// Output:
    71  	// {"logger":"","caller":{"file":"example_test.go","line":67},"level":0,"msg":"V(0) message","key":"value"}
    72  	// {"logger":"","caller":{"file":"example_test.go","line":68},"level":1,"msg":"V(1) message","key":"value"}
    73  }
    74  
    75  func ExampleOptions_renderHooks() {
    76  	// prefix all builtin keys with "log:"
    77  	prefixSpecialKeys := func(kvList []any) []any {
    78  		for i := 0; i < len(kvList); i += 2 {
    79  			k, _ := kvList[i].(string)
    80  			kvList[i] = "log:" + k
    81  		}
    82  		return kvList
    83  	}
    84  
    85  	// present saved values as a single JSON object
    86  	valuesAsObject := func(kvList []any) []any {
    87  		return []any{"labels", funcr.PseudoStruct(kvList)}
    88  	}
    89  
    90  	var log logr.Logger = funcr.NewJSON(
    91  		func(obj string) { fmt.Println(obj) },
    92  		funcr.Options{
    93  			RenderBuiltinsHook: prefixSpecialKeys,
    94  			RenderValuesHook:   valuesAsObject,
    95  		})
    96  	log = log.WithName("MyLogger")
    97  	log = log.WithValues("savedKey1", "savedVal1")
    98  	log = log.WithValues("savedKey2", "savedVal2")
    99  	log.Info("the message", "key", "value")
   100  	// Output: {"log:logger":"MyLogger","log:level":0,"log:msg":"the message","labels":{"savedKey1":"savedVal1","savedKey2":"savedVal2"},"key":"value"}
   101  }
   102  
   103  func ExamplePseudoStruct() {
   104  	var log logr.Logger = funcr.NewJSON(
   105  		func(obj string) { fmt.Println(obj) },
   106  		funcr.Options{})
   107  	kv := []any{
   108  		"field1", 12345,
   109  		"field2", true,
   110  	}
   111  	log.Info("the message", "key", funcr.PseudoStruct(kv))
   112  	// Output: {"logger":"","level":0,"msg":"the message","key":{"field1":12345,"field2":true}}
   113  }
   114  
   115  func ExampleOptions_maxLogDepth() {
   116  	type List struct {
   117  		Next *List
   118  	}
   119  	l := List{}
   120  	l.Next = &l // recursive
   121  
   122  	var log logr.Logger = funcr.NewJSON(
   123  		func(obj string) { fmt.Println(obj) },
   124  		funcr.Options{MaxLogDepth: 4})
   125  	log.Info("recursive", "list", l)
   126  	// Output: {"logger":"","level":0,"msg":"recursive","list":{"Next":{"Next":{"Next":{"Next":{"Next":"<max-log-depth-exceeded>"}}}}}}
   127  }
   128  

View as plain text