textlogger

package
v2.100.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2023 License: Apache-2.0 Imports: 12 Imported by: 89

Documentation

Overview

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

Examples

Constants

This section is empty.

Variables

View Source
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

func NewLogger(c *Config) logr.Logger

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

func (c *Config) AddFlags(fs *flag.FlagSet)

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 added in v2.90.1

func (c *Config) VModule() flag.Value

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 added in v2.90.1

func (c *Config) Verbosity() flag.Value

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.

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}...] "))

}
Output:

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 added in v2.90.1

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳