logs

package
v0.24.13 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 16 Imported by: 1,014

Documentation

Overview

Package logs contains support for logging options, flags and setup. Commands must explicitly enable command line flags. They no longer get added automatically when importing this package.

Index

Constants

View Source
const (
	DefaultLogFormat = "text"
	JSONLogFormat    = "json"
)

Supported klog formats

View Source
const (
	// owner: @pohly
	// kep: http://kep.k8s.io/3077
	// alpha: v1.24
	//
	// Enables looking up a logger from a context.Context instead of using
	// the global fallback logger and manipulating the logger that is
	// used by a call chain.
	ContextualLogging featuregate.Feature = "ContextualLogging"
)

Variables

This section is empty.

Functions

func AddFeatureGates added in v0.24.0

func AddFeatureGates(mutableFeatureGate featuregate.MutableFeatureGate) error

AddFeatureGates adds all feature gates used by this package.

func AddFlags

func AddFlags(fs *pflag.FlagSet, opts ...Option)

AddFlags registers this package's flags on arbitrary FlagSets. This includes the klog flags, with the original underscore as separator between. If commands want hyphens as separators, they can set k8s.io/component-base/cli/flag/WordSepNormalizeFunc as normalization function on the flag set before calling AddFlags.

May be called more than once.

func AddGoFlags added in v0.23.0

func AddGoFlags(fs *flag.FlagSet, opts ...Option)

AddGoFlags is a variant of AddFlags for traditional Go flag.FlagSet. Commands should use pflag whenever possible for the sake of consistency. Cases where this function is needed include tests (they have to set up flags in flag.CommandLine) and commands that for historic reasons use Go flag.Parse and cannot change to pflag because it would break their command line interface.

func BindLoggingFlags added in v0.22.0

func BindLoggingFlags(c *config.LoggingConfiguration, fs *pflag.FlagSet)

BindLoggingFlags binds the Options struct fields to a flagset.

Programs using LoggingConfiguration must use SkipLoggingConfigurationFlags when calling AddFlags to avoid the duplicate registration of flags.

func FlushLogs

func FlushLogs()

FlushLogs flushes logs immediately. This should be called at the end of the main function via defer to ensure that all pending log messages are printed before exiting the program.

func GlogSetter

func GlogSetter(val string) (string, error)

GlogSetter is a setter to set glog level.

func InitLogs

func InitLogs()

InitLogs initializes logs the way we want for Kubernetes. It should be called after parsing flags. If called before that, it will use the default log settings.

InitLogs disables support for contextual logging in klog while that Kubernetes feature is not considered stable yet. Commands which want to support contextual logging can:

  • call klog.EnableContextualLogging after calling InitLogs, with a fixed `true` or depending on some command line flag or a feature gate check
  • set up a FeatureGate instance, the advanced logging configuration with Options and call Options.ValidateAndApply with the FeatureGate; k8s.io/component-base/logs/example/cmd demonstrates how to do that

func NewLogger

func NewLogger(prefix string) *log.Logger

NewLogger creates a new log.Logger which sends logs to klog.Info.

func UnsupportedLoggingFlags added in v0.22.0

func UnsupportedLoggingFlags(normalizeFunc func(f *pflag.FlagSet, name string) pflag.NormalizedName) []*pflag.Flag

UnsupportedLoggingFlags lists unsupported logging flags. The normalize function is optional.

func ValidateLoggingConfiguration added in v0.22.0

func ValidateLoggingConfiguration(c *config.LoggingConfiguration, fldPath *field.Path) field.ErrorList

Types

type KlogWriter

type KlogWriter struct{}

KlogWriter serves as a bridge between the standard log package and the glog package.

func (KlogWriter) Write

func (writer KlogWriter) Write(data []byte) (n int, err error)

Write implements the io.Writer interface.

type Option added in v0.23.0

type Option func(*addFlagsOptions)

func SkipLoggingConfigurationFlags added in v0.23.0

func SkipLoggingConfigurationFlags() Option

SkipLoggingConfigurationFlags must be used as option for AddFlags when the program also uses a LoggingConfiguration struct for configuring logging. Then only flags not covered by that get added.

type Options added in v0.19.0

type Options struct {
	Config config.LoggingConfiguration
}

Options has klog format parameters

func NewOptions added in v0.19.0

func NewOptions() *Options

NewOptions return new klog options

func (*Options) AddFlags added in v0.19.0

func (o *Options) AddFlags(fs *pflag.FlagSet)

AddFlags add logging-format flag.

Programs using LoggingConfiguration must use SkipLoggingConfigurationFlags when calling AddFlags to avoid the duplicate registration of flags.

func (*Options) ValidateAndApply added in v0.23.0

func (o *Options) ValidateAndApply(featureGate featuregate.FeatureGate) error

ValidateAndApply combines validation and application of the logging configuration. This should be invoked as early as possible because then the rest of the program startup (including validation of other options) will already run with the final logging configuration.

The optional FeatureGate controls logging features. If nil, the default for these features is used.

Directories

Path Synopsis
Package datapol contains functions to determine if objects contain sensitive data to e.g.
Package datapol contains functions to determine if objects contain sensitive data to e.g.
example
cmd
Package testinit adds logging flags to a Ginkgo or Go test program during initialization, something that the logs package itself no longer does.
Package testinit adds logging flags to a Ginkgo or Go test program during initialization, something that the logs package itself no longer does.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL