package textlogger
import "k8s.io/klog/v2/textlogger"
Package textlogger contains an implementation of the logr interface which is producing the exact same output as klog.
Experimental
Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.
Index ¶
- Variables
- func NewLogger(c *Config) logr.Logger
- type Config
- func NewConfig(opts ...ConfigOption) *Config
- func (c *Config) AddFlags(fs *flag.FlagSet)
- func (c *Config) VModule() flag.Value
- func (c *Config) Verbosity() flag.Value
- type ConfigOption
- func Output(output io.Writer) ConfigOption
- func VModuleFlagName(name string) ConfigOption
- func Verbosity(level int) ConfigOption
- func VerbosityFlagName(name string) ConfigOption
- type KlogBufferWriter
Examples ¶
Variables ¶
var ( // TimeNow is used to retrieve the current time. May be changed for testing. // // Experimental // // Notice: This variable is EXPERIMENTAL and may be changed or removed in a // later release. TimeNow = time.Now )
Functions ¶
func NewLogger ¶
NewLogger constructs a new logger.
Verbosity can be modified at any time through the Config.V and Config.VModule API.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release. The behavior of the returned Logger may change.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config influences logging in a text logger. To make this configurable via command line flags, instantiate this once per program and use AddFlags to bind command line flags to the instance before passing it to NewTestContext.
Must be constructed with NewConfig.
Experimental
Notice: This type is EXPERIMENTAL and may be changed or removed in a later release.
func NewConfig ¶
func NewConfig(opts ...ConfigOption) *Config
NewConfig returns a configuration with recommended defaults and optional modifications. Command line flags are not bound to any FlagSet yet.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.
func (*Config) AddFlags ¶
AddFlags registers the command line flags that control the configuration.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.
func (*Config) VModule ¶
VModule returns a value instance that can be used to query (via String) or modify (via Set) the vmodule settings. This is thread-safe and can be done at runtime.
func (*Config) Verbosity ¶
Verbosity returns a value instance that can be used to query (via String) or
modify (via Set) the verbosity threshold. This is thread-safe and can be
done at runtime.
Code:play
Output:Example¶
package main
import (
"bytes"
"fmt"
"regexp"
"k8s.io/klog/v2/textlogger"
)
var headerRe = regexp.MustCompile(`([IE])[[:digit:]]{4} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\.[[:digit:]]{6}[[:space:]]+[[:digit:]]+ example_test.go:[[:digit:]]+\] `)
func main() {
var buffer bytes.Buffer
config := textlogger.NewConfig(textlogger.Verbosity(1), textlogger.Output(&buffer))
logger := textlogger.NewLogger(config)
logger.Info("initial verbosity", "v", config.Verbosity().String())
logger.V(2).Info("now you don't see me")
if err := config.Verbosity().Set("2"); err != nil {
logger.Error(err, "setting verbosity to 2")
}
logger.V(2).Info("now you see me")
if err := config.Verbosity().Set("1"); err != nil {
logger.Error(err, "setting verbosity to 1")
}
logger.V(2).Info("now I'm gone again")
fmt.Print(headerRe.ReplaceAllString(buffer.String(), "${1}...] "))
}
I...] "initial verbosity" v="1"
I...] "now you see me"
type ConfigOption ¶
type ConfigOption func(co *configOptions)
ConfigOption implements functional parameters for NewConfig.
Experimental
Notice: This type is EXPERIMENTAL and may be changed or removed in a later release.
func Output ¶
func Output(output io.Writer) ConfigOption
Output overrides stderr as the output stream.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.
func VModuleFlagName ¶
func VModuleFlagName(name string) ConfigOption
VModulFlagName overrides the default -vmodule for the per-module verbosity levels.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.
func Verbosity ¶
func Verbosity(level int) ConfigOption
Verbosity overrides the default verbosity level of 0. See https://github.com/kubernetes/community/blob/9406b4352fe2d5810cb21cc3cb059ce5886de157/contributors/devel/sig-instrumentation/logging.md#logging-conventions for log level conventions in Kubernetes.
Experimental
Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.
func VerbosityFlagName ¶
func VerbosityFlagName(name string) ConfigOption
VerbosityFlagName overrides the default -v for the verbosity level.
type KlogBufferWriter ¶
type KlogBufferWriter interface { // WriteKlogBuffer takes a pre-formatted buffer prepared by klog and // writes it unchanged to the output stream. Can be used with // klog.WriteKlogBuffer when setting a logger through // klog.SetLoggerWithOptions. WriteKlogBuffer([]byte) }
KlogBufferWriter is implemented by the textlogger LogSink.
Source Files ¶
options.go textlogger.go
- Version
- v2.100.1
- Published
- Apr 28, 2023
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 1 day ago –
Tools for package owners.