...

Source file src/k8s.io/klog/v2/ktesting/options.go

Documentation: k8s.io/klog/v2/ktesting

     1  /*
     2  Copyright 2021 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 ktesting
    18  
    19  import (
    20  	"flag"
    21  	"strconv"
    22  
    23  	"k8s.io/klog/v2/internal/serialize"
    24  	"k8s.io/klog/v2/internal/verbosity"
    25  )
    26  
    27  // Config influences logging in a test logger. To make this configurable via
    28  // command line flags, instantiate this once per program and use AddFlags to
    29  // bind command line flags to the instance before passing it to NewTestContext.
    30  //
    31  // Must be constructed with NewConfig.
    32  type Config struct {
    33  	vstate *verbosity.VState
    34  	co     configOptions
    35  }
    36  
    37  // Verbosity returns a value instance that can be used to query (via String) or
    38  // modify (via Set) the verbosity threshold. This is thread-safe and can be
    39  // done at runtime.
    40  func (c *Config) Verbosity() flag.Value {
    41  	return c.vstate.V()
    42  }
    43  
    44  // VModule returns a value instance that can be used to query (via String) or
    45  // modify (via Set) the vmodule settings. This is thread-safe and can be done
    46  // at runtime.
    47  func (c *Config) VModule() flag.Value {
    48  	return c.vstate.VModule()
    49  }
    50  
    51  // ConfigOption implements functional parameters for NewConfig.
    52  type ConfigOption func(co *configOptions)
    53  
    54  type configOptions struct {
    55  	anyToString       serialize.AnyToStringFunc
    56  	verbosityFlagName string
    57  	vmoduleFlagName   string
    58  	verbosityDefault  int
    59  	bufferLogs        bool
    60  }
    61  
    62  // AnyToString overrides the default formatter for values that are not
    63  // supported directly by klog. The default is `fmt.Sprintf("%+v")`.
    64  // The formatter must not panic.
    65  func AnyToString(anyToString func(value interface{}) string) ConfigOption {
    66  	return func(co *configOptions) {
    67  		co.anyToString = anyToString
    68  	}
    69  }
    70  
    71  // VerbosityFlagName overrides the default -testing.v for the verbosity level.
    72  func VerbosityFlagName(name string) ConfigOption {
    73  	return func(co *configOptions) {
    74  		co.verbosityFlagName = name
    75  	}
    76  }
    77  
    78  // VModulFlagName overrides the default -testing.vmodule for the per-module
    79  // verbosity levels.
    80  func VModuleFlagName(name string) ConfigOption {
    81  	return func(co *configOptions) {
    82  		co.vmoduleFlagName = name
    83  	}
    84  }
    85  
    86  // Verbosity overrides the default verbosity level of 5. That default is higher
    87  // than in klog itself because it enables logging entries for "the steps
    88  // leading up to errors and warnings" and "troubleshooting" (see
    89  // https://github.com/kubernetes/community/blob/9406b4352fe2d5810cb21cc3cb059ce5886de157/contributors/devel/sig-instrumentation/logging.md#logging-conventions),
    90  // which is useful when debugging a failed test. `go test` only shows the log
    91  // output for failed tests. To see all output, use `go test -v`.
    92  func Verbosity(level int) ConfigOption {
    93  	return func(co *configOptions) {
    94  		co.verbosityDefault = level
    95  	}
    96  }
    97  
    98  // BufferLogs controls whether log entries are captured in memory in addition
    99  // to being printed. Off by default. Unit tests that want to verify that
   100  // log entries are emitted as expected can turn this on and then retrieve
   101  // the captured log through the Underlier LogSink interface.
   102  func BufferLogs(enabled bool) ConfigOption {
   103  	return func(co *configOptions) {
   104  		co.bufferLogs = enabled
   105  	}
   106  }
   107  
   108  // NewConfig returns a configuration with recommended defaults and optional
   109  // modifications. Command line flags are not bound to any FlagSet yet.
   110  func NewConfig(opts ...ConfigOption) *Config {
   111  	c := &Config{
   112  		co: configOptions{
   113  			verbosityFlagName: "testing.v",
   114  			vmoduleFlagName:   "testing.vmodule",
   115  			verbosityDefault:  5,
   116  		},
   117  	}
   118  	for _, opt := range opts {
   119  		opt(&c.co)
   120  	}
   121  
   122  	c.vstate = verbosity.New()
   123  	// Cannot fail for this input.
   124  	_ = c.vstate.V().Set(strconv.FormatInt(int64(c.co.verbosityDefault), 10))
   125  	return c
   126  }
   127  
   128  // AddFlags registers the command line flags that control the configuration.
   129  func (c *Config) AddFlags(fs *flag.FlagSet) {
   130  	fs.Var(c.vstate.V(), c.co.verbosityFlagName, "number for the log level verbosity of the testing logger")
   131  	fs.Var(c.vstate.VModule(), c.co.vmoduleFlagName, "comma-separated list of pattern=N log level settings for files matching the patterns")
   132  }
   133  

View as plain text