Documentation ¶
Overview ¶
Package log provides the canonical logging functionality used by Go-based Istio components.
Istio's logging subsystem is built on top of the [Zap](https://godoc.org/go.uber.org/zap) package. High performance scenarios should use the Error, Warn, Info, and Debug methods. Lower perf scenarios can use the more expensive convenience methods such as Debugf and Warnw.
The package provides direct integration with the Cobra command-line processor which makes it easy to build programs that use a consistent interface for logging. Here's an example of a simple Cobra-based program using this log package:
func main() { // get the default logging options options := log.DefaultOptions() rootCmd := &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // configure the logging system if err := log.Configure(options); err != nil { // print an error and quit } // output some logs log.Info("Hello") log.Sync() }, } // add logging-specific flags to the cobra command options.AttachCobraFlags(rootCmd) rootCmd.SetArgs(os.Args[1:]) rootCmd.Execute() }
Once configured, this package intercepts the output of the standard golang "log" package as well as anything sent to the global zap logger (zap.L()).
Index ¶
- Constants
- Variables
- func Close() error
- func Configure(options *Options) error
- func Debug(fields any)
- func DebugEnabled() bool
- func Debugf(format string, args ...any)
- func EnableKlogWithCobra()
- func EnableKlogWithGoFlag()
- func EnableKlogWithVerbosity(v int)
- func Error(fields any)
- func ErrorEnabled() bool
- func Errorf(format string, args ...any)
- func Fatal(fields any)
- func FatalEnabled() bool
- func Fatalf(format string, args ...any)
- func Info(fields any)
- func InfoEnabled() bool
- func Infof(format string, args ...any)
- func LevelToString(level Level) string
- func NewLogrAdapter(l *Scope) logr.Logger
- func Scopes() map[string]*Scope
- func Sync() error
- func Warn(fields any)
- func WarnEnabled() bool
- func Warnf(format string, args ...any)
- type Extension
- type Level
- type Options
- func (o *Options) AttachCobraFlags(cmd *cobra.Command)
- func (o *Options) AttachFlags(stringArrayVar func(p *[]string, name string, value []string, usage string), ...)
- func (o *Options) SetDefaultOutputLevel(scope string, level Level)
- func (o *Options) WithExtension(e Extension) *Options
- func (o *Options) WithStackdriverLoggingFormat() *Options
- func (o *Options) WithTeeToUDS(addr, path string) *Options
- type Scope
- func (s *Scope) Debug(msg any)
- func (s *Scope) DebugEnabled() bool
- func (s *Scope) Debugf(format string, args ...any)
- func (s *Scope) Description() string
- func (s *Scope) Error(msg any)
- func (s *Scope) ErrorEnabled() bool
- func (s *Scope) Errorf(format string, args ...any)
- func (s *Scope) Fatal(msg any)
- func (s *Scope) FatalEnabled() bool
- func (s *Scope) Fatalf(format string, args ...any)
- func (s *Scope) GetLogCallers() bool
- func (s *Scope) GetOutputLevel() Level
- func (s *Scope) GetStackTraceLevel() Level
- func (s *Scope) Info(msg any)
- func (s *Scope) InfoEnabled() bool
- func (s *Scope) Infof(format string, args ...any)
- func (s *Scope) LogWithTime(level Level, msg string, t time.Time)
- func (s *Scope) Name() string
- func (s *Scope) SetLogCallers(logCallers bool)
- func (s *Scope) SetOutputLevel(l Level)
- func (s *Scope) SetStackTraceLevel(l Level)
- func (s *Scope) Warn(msg any)
- func (s *Scope) WarnEnabled() bool
- func (s *Scope) Warnf(format string, args ...any)
- func (s *Scope) WithLabels(kvlist ...any) *Scope
Constants ¶
const ( DefaultScopeName = "default" OverrideScopeName = "all" )
const (
GrpcScopeName string = "grpc"
)
Variables ¶
var (
KlogScope = RegisterScope("klog", "")
)
Functions ¶
func Configure ¶
Configure initializes Istio's logging subsystem.
You typically call this once at process startup. Once this call returns, the logging system is ready to accept data. nolint: staticcheck
func DebugEnabled ¶
func DebugEnabled() bool
DebugEnabled returns whether output of messages using this scope is currently enabled for debug-level output.
func EnableKlogWithCobra ¶
func EnableKlogWithCobra()
EnableKlogWithCobra enables klog to work with cobra / pflags. k8s libraries like client-go use klog.
func EnableKlogWithGoFlag ¶
func EnableKlogWithGoFlag()
EnableKlogWithCobra enables klog to work with go flags. k8s libraries like client-go use klog.
func EnableKlogWithVerbosity ¶
func EnableKlogWithVerbosity(v int)
EnableKlogWithVerbosity sets the klog verbosity directly. When using in an application, EnableKlogWithCobra is preferred to expose a --vklog flag.
func ErrorEnabled ¶
func ErrorEnabled() bool
ErrorEnabled returns whether output of messages using this scope is currently enabled for error-level output.
func FatalEnabled ¶
func FatalEnabled() bool
FatalEnabled returns whether output of messages using this scope is currently enabled for fatal-level output.
func InfoEnabled ¶
func InfoEnabled() bool
InfoEnabled returns whether output of messages using this scope is currently enabled for info-level output.
func LevelToString ¶
func NewLogrAdapter ¶
NewLogrAdapter creates a new logr.Logger using the given Zap Logger to log.
func Sync ¶
func Sync() error
Sync flushes any buffered log entries. Processes should normally take care to call Sync before exiting.
func WarnEnabled ¶
func WarnEnabled() bool
WarnEnabled returns whether output of messages using this scope is currently enabled for warn-level output.
Types ¶
type Extension ¶
Extension provides an extension mechanism for logs. This is essentially like https://pkg.go.dev/golang.org/x/exp/slog#Handler. This interface should be considered unstable; we will likely swap it for slog in the future and not expose zap internals. Returns a modified Core interface, and a Close() function.
type Level ¶
type Level int
Level is an enumeration of all supported log levels.
const ( // NoneLevel disables logging NoneLevel Level = iota // FatalLevel enables fatal level logging FatalLevel // ErrorLevel enables error level logging ErrorLevel // WarnLevel enables warn level logging WarnLevel // InfoLevel enables info level logging InfoLevel // DebugLevel enables debug level logging DebugLevel )
func StringToLevel ¶
type Options ¶
type Options struct { // OutputPaths is a list of file system paths to write the log data to. // The special values stdout and stderr can be used to output to the // standard I/O streams. This defaults to stdout. OutputPaths []string // ErrorOutputPaths is a list of file system paths to write logger errors to. // The special values stdout and stderr can be used to output to the // standard I/O streams. This defaults to stderr. ErrorOutputPaths []string // JSONEncoding controls whether the log is formatted as JSON. JSONEncoding bool // contains filtered or unexported fields }
Options defines the set of options supported by Istio's component logging package.
func DefaultOptions ¶
func DefaultOptions() *Options
DefaultOptions returns a new set of options, initialized to the defaults
func (*Options) AttachCobraFlags ¶
AttachCobraFlags attaches a set of Cobra flags to the given Cobra command.
Cobra is the command-line processor that Istio uses. This command attaches the necessary set of flags to expose a CLI to let the user control all logging options.
func (*Options) AttachFlags ¶
func (o *Options) AttachFlags( stringArrayVar func(p *[]string, name string, value []string, usage string), stringVar func(p *string, name string, value string, usage string), _ func(p *int, name string, value int, usage string), boolVar func(p *bool, name string, value bool, usage string), )
AttachFlags allows attaching of flags through a set of lambda functions.
func (*Options) SetDefaultOutputLevel ¶
SetDefaultOutputLevel sets the minimum log output level for a given scope. This can be overwritten by flags
func (*Options) WithExtension ¶
func (*Options) WithStackdriverLoggingFormat ¶
WithStackdriverLoggingFormat configures logging output to match Stackdriver structured logging conventions.
func (*Options) WithTeeToUDS ¶
WithTeeToUDS configures a parallel logging pipeline that writes logs to a server over UDS. addr is the socket that the server listens on, and path is the HTTP path that process the log message.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope constrains logging control to a named scope level. It gives users a fine grained control over output severity threshold and stack traces.
Scope supports structured logging using WithLabels:
s := RegisterScope("MyScope", "Description", 0) s = s.WithLabels("foo", "bar", "baz", 123, "qux", 0.123) s.Info("Hello") // <time> info MyScope Hello foo=bar baz=123 qux=0.123
The output format can be globally configured to be JSON instead, using Options in this package.
e.g. <time> info MyScope { "message":"Hello","foo":"bar","baz":123 }
Scope also supports an error dictionary. The caller can pass a *structured.Error object as the first parameter to any of the output functions (Fatal*, Error* etc.) and this will append the fields in the object to the output:
e := &structured.Error{MoreInfo:"See the documentation in istio.io/helpful_link"} s.WithLabels("foo", "bar").Error(e, "Hello") <time> info MyScope Hello moreInfo=See the documentation in istio.io/helpful_link foo=bar
See structured.Error for additional guidance on defining errors in a dictionary.
func FindScope ¶
FindScope returns a previously registered scope, or nil if the named scope wasn't previously registered
func RegisterScope ¶
RegisterScope registers a new logging scope. If the same name is used multiple times for a single process, the same Scope struct is returned.
Scope names cannot include colons, commas, or periods.
func WithLabels ¶
WithLabels adds a key-value pairs to the labels in s. The key must be a string, while the value may be any type. It returns a copy of the default scope, with the labels added.
func (*Scope) DebugEnabled ¶
DebugEnabled returns whether output of messages using this scope is currently enabled for debug-level output.
func (*Scope) Description ¶
Description returns this scope's description
func (*Scope) ErrorEnabled ¶
ErrorEnabled returns whether output of messages using this scope is currently enabled for error-level output.
func (*Scope) FatalEnabled ¶
FatalEnabled returns whether output of messages using this scope is currently enabled for fatal-level output.
func (*Scope) GetLogCallers ¶
GetLogCallers returns the output level associated with the scope.
func (*Scope) GetOutputLevel ¶
GetOutputLevel returns the output level associated with the scope.
func (*Scope) GetStackTraceLevel ¶
GetStackTraceLevel returns the stack tracing level associated with the scope.
func (*Scope) InfoEnabled ¶
InfoEnabled returns whether output of messages using this scope is currently enabled for info-level output.
func (*Scope) LogWithTime ¶
LogWithTime outputs a message with a given timestamp.
func (*Scope) SetLogCallers ¶
SetLogCallers adjusts the output level associated with the scope.
func (*Scope) SetOutputLevel ¶
SetOutputLevel adjusts the output level associated with the scope.
func (*Scope) SetStackTraceLevel ¶
SetStackTraceLevel adjusts the stack tracing level associated with the scope.
func (*Scope) WarnEnabled ¶
WarnEnabled returns whether output of messages using this scope is currently enabled for warn-level output.
func (*Scope) WithLabels ¶
WithLabels adds a key-value pairs to the labels in s. The key must be a string, while the value may be any type. It returns a copy of s, with the labels added. e.g. newScope := oldScope.WithLabels("foo", "bar", "baz", 123, "qux", 0.123)