Documentation ¶
Overview ¶
Package klog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. It provides functions Info, Warning, Error, Fatal, plus formatting variants such as Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags.
Basic examples:
klog.Info("Prepare to repel boarders") klog.Fatalf("Initialization failed: %s", err)
See the documentation for the V function for an explanation of these examples:
if klog.V(2) { klog.Info("Starting transaction...") } klog.V(2).Infoln("Processed", nItems, "elements")
Log output is buffered and written periodically using Flush. Programs should call Flush before exiting to guarantee all log output is written.
By default, all log statements write to standard error. This package provides several flags that modify this behavior. As a result, flag.Parse must be called before any logging is done.
-logtostderr=true Logs are written to standard error instead of to files. -alsologtostderr=false Logs are written to standard error as well as to files. -stderrthreshold=ERROR Log events at or above this severity are logged to standard error as well as to files. -log_dir="" Log files will be written to this directory instead of the default temporary directory. Other flags provide aids to debugging. -log_backtrace_at="" When set to a file and line number holding a logging statement, such as -log_backtrace_at=gopherflakes.go:234 a stack trace will be written to the Info log whenever execution hits that statement. (Unlike with -vmodule, the ".go" must be present.) -v=0 Enable V-leveled logging at the specified level. -vmodule="" The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the ".go" suffix) or "glob" pattern and N is a V level. For instance, -vmodule=gopher*=3 sets the V level to 3 in all Go files whose names begin "gopher".
Index ¶
- Variables
- func AddGlobalFlags(fs *pflag.FlagSet, name string)
- func CalculateMaxSize() uint64
- func CopyStandardLogTo(name string)
- func EnableColor(enable bool)
- func Error(args ...interface{})
- func ErrorDepth(depth int, args ...interface{})
- func ErrorS(err error, msg string, keysAndValues ...interface{})
- func ErrorTag(tag KlogTag, args ...interface{})
- func ErrorTagf(tag KlogTag, format string, args ...interface{})
- func ErrorTagln(tag KlogTag, args ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(args ...interface{})
- func Exit(args ...interface{})
- func ExitDepth(depth int, args ...interface{})
- func Exitf(format string, args ...interface{})
- func Exitln(args ...interface{})
- func Fatal(args ...interface{})
- func FatalDepth(depth int, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(args ...interface{})
- func Flush()
- func FlushSchedule(fn func(flush func(), frequency time.Duration))
- func GetEnvEnableColor() bool
- func GinErrorLogger() gin.HandlerFunc
- func GinErrorLoggerT(typ gin.ErrorType) gin.HandlerFunc
- func GinLogger(duration time.Duration) gin.HandlerFunc
- func GinLoggerWithOutPaths(duration time.Duration, paths map[string]bool) gin.HandlerFunc
- func Info(args ...interface{})
- func InfoDepth(depth int, args ...interface{})
- func InfoS(msg string, keysAndValues ...interface{})
- func InfoTag(tag KlogTag, args ...interface{})
- func InfoTagDepth(tag KlogTag, depth int, args ...interface{})
- func InfoTagf(tag KlogTag, format string, args ...interface{})
- func InfoTagln(tag KlogTag, args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func InitFlags(flagset *flag.FlagSet)
- func LogToStderr(stderr bool)
- func SetLogger(logr logr.Logger)
- func SetOutput(w io.Writer)
- func SetOutputBySeverity(name string, w io.Writer)
- func Warning(args ...interface{})
- func WarningDepth(depth int, args ...interface{})
- func WarningTag(tag KlogTag, args ...interface{})
- func WarningTagf(tag KlogTag, format string, args ...interface{})
- func WarningTagln(tag KlogTag, args ...interface{})
- func Warningf(format string, args ...interface{})
- func Warningln(args ...interface{})
- type GormLoggerWriter
- type KMetadata
- type KlogTag
- type Level
- type ObjectRef
- type OutputStats
- type Verbose
- func (v Verbose) Enabled() bool
- func (v Verbose) Error(err error, msg string, args ...interface{})
- func (v Verbose) Info(args ...interface{})
- func (v Verbose) InfoS(msg string, keysAndValues ...interface{})
- func (v Verbose) Infof(format string, args ...interface{})
- func (v Verbose) Infoln(args ...interface{})
Constants ¶
This section is empty.
Variables ¶
var ( Black = string([]byte{27, 91, 48, 59, 51, 48, 109}) Red = string([]byte{27, 91, 48, 59, 51, 49, 109}) Green = string([]byte{27, 91, 48, 59, 51, 50, 109}) Yellow = string([]byte{27, 91, 48, 59, 51, 51, 109}) Bule = string([]byte{27, 91, 48, 59, 51, 52, 109}) Magenta = string([]byte{27, 91, 48, 59, 51, 53, 109}) Cyan = string([]byte{27, 91, 48, 59, 51, 54, 109}) White = string([]byte{27, 91, 48, 59, 51, 55, 109}) HighlightBlack = string([]byte{27, 91, 49, 59, 51, 48, 109}) HighlightRed = string([]byte{27, 91, 49, 59, 51, 49, 109}) HighlightGreen = string([]byte{27, 91, 49, 59, 51, 50, 109}) HighlightYellow = string([]byte{27, 91, 49, 59, 51, 51, 109}) HighlightBlue = string([]byte{27, 91, 49, 59, 51, 52, 109}) HighlightMagenta = string([]byte{27, 91, 49, 59, 51, 53, 109}) HighlightCyan = string([]byte{27, 91, 49, 59, 51, 54, 109}) HighlightWhite = string([]byte{27, 91, 49, 59, 51, 55, 109}) UnderlineBlack = string([]byte{27, 91, 52, 59, 51, 48, 109}) UnderlineRed = string([]byte{27, 91, 52, 59, 51, 49, 109}) UnderlineGreen = string([]byte{27, 91, 52, 59, 51, 50, 109}) UnderlineYellow = string([]byte{27, 91, 52, 59, 51, 51, 109}) UnderlineBlue = string([]byte{27, 91, 52, 59, 51, 52, 109}) UnderlineMagenta = string([]byte{27, 91, 52, 59, 51, 53, 109}) UnderlineCyan = string([]byte{27, 91, 52, 59, 51, 54, 109}) UnderlineWhite = string([]byte{27, 91, 52, 59, 51, 55, 109}) BlinkingBlack = string([]byte{27, 91, 53, 59, 51, 48, 109}) BlinkingRed = string([]byte{27, 91, 53, 59, 51, 49, 109}) BlinkingGreen = string([]byte{27, 91, 53, 59, 51, 50, 109}) BlinkingYellow = string([]byte{27, 91, 53, 59, 51, 51, 109}) BlinkingBlue = string([]byte{27, 91, 53, 59, 51, 52, 109}) BlinkingMagenta = string([]byte{27, 91, 53, 59, 51, 53, 109}) BlinkingCyan = string([]byte{27, 91, 53, 59, 51, 54, 109}) BlinkingWhite = string([]byte{27, 91, 53, 59, 51, 55, 109}) ReverseBlack = string([]byte{27, 91, 55, 59, 51, 48, 109}) ReverseRed = string([]byte{27, 91, 55, 59, 51, 49, 109}) ReverseGreen = string([]byte{27, 91, 55, 59, 51, 50, 109}) ReverseYellow = string([]byte{27, 91, 55, 59, 51, 51, 109}) ReverseBlue = string([]byte{27, 91, 55, 59, 51, 52, 109}) ReverseMagenta = string([]byte{27, 91, 55, 59, 51, 53, 109}) ReverseCyan = string([]byte{27, 91, 55, 59, 51, 54, 109}) ReverseWhite = string([]byte{27, 91, 55, 59, 51, 55, 109}) InvisibleBlack = string([]byte{27, 91, 56, 59, 51, 48, 109}) InvisibleRed = string([]byte{27, 91, 56, 59, 51, 49, 109}) InvisibleGreen = string([]byte{27, 91, 56, 59, 51, 50, 109}) InvisibleYellow = string([]byte{27, 91, 56, 59, 51, 51, 109}) InvisibleBlue = string([]byte{27, 91, 56, 59, 51, 52, 109}) InvisibleMagenta = string([]byte{27, 91, 56, 59, 51, 53, 109}) InvisibleCyan = string([]byte{27, 91, 56, 59, 51, 54, 109}) InvisibleWhite = string([]byte{27, 91, 56, 59, 51, 55, 109}) Reset = string([]byte{27, 91, 48, 109}) )
var MaxSize uint64 = 1024 * 1024 * 1800
MaxSize is the maximum size of a log file in bytes.
var Stats struct { Info, Warning, Error OutputStats }
Stats tracks the number of lines of output and number of bytes per severity level. Values must be read with atomic.LoadInt64.
Functions ¶
func AddGlobalFlags ¶ added in v1.5.0
func CalculateMaxSize ¶
func CalculateMaxSize() uint64
CalculateMaxSize returns the real max size in bytes after considering the default max size and the flag options.
func CopyStandardLogTo ¶
func CopyStandardLogTo(name string)
CopyStandardLogTo arranges for messages written to the Go "log" package's default logs to also appear in the Google logs for the named and lower severities. Subsequent changes to the standard log's default output location or format may break this behavior.
Valid names are "INFO", "WARNING", "ERROR", and "FATAL". If the name is not recognized, CopyStandardLogTo panics.
func EnableColor ¶ added in v1.4.1
func EnableColor(enable bool)
func Error ¶
func Error(args ...interface{})
Error logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func ErrorDepth ¶
func ErrorDepth(depth int, args ...interface{})
ErrorDepth acts as Error but uses depth to determine which call frame to log. ErrorDepth(0, "msg") is the same as Error("msg").
func ErrorS ¶
ErrorS structured logs to the ERROR, WARNING, and INFO logs. the err argument used as "err" field of log line. The msg argument used to add constant description to the log line. The key/value pairs would be join by "=" ; a newline is always appended.
Basic examples: >> klog.ErrorS(err, "Failed to update pod status") output: >> E1025 00:15:15.525108 1 controller_utils.go:114] "Failed to update pod status" err="timeout"
func ErrorTagln ¶
func ErrorTagln(tag KlogTag, args ...interface{})
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Errorln ¶
func Errorln(args ...interface{})
Errorln logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Println; a newline is always appended.
func Exit ¶
func Exit(args ...interface{})
Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func ExitDepth ¶
func ExitDepth(depth int, args ...interface{})
ExitDepth acts as Exit but uses depth to determine which call frame to log. ExitDepth(0, "msg") is the same as Exit("msg").
func Exitf ¶
func Exitf(format string, args ...interface{})
Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Exitln ¶
func Exitln(args ...interface{})
Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
func Fatal ¶
func Fatal(args ...interface{})
Fatal logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func FatalDepth ¶
func FatalDepth(depth int, args ...interface{})
FatalDepth acts as Fatal but uses depth to determine which call frame to log. FatalDepth(0, "msg") is the same as Fatal("msg").
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Fatalln ¶
func Fatalln(args ...interface{})
Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Println; a newline is always appended.
func FlushSchedule ¶ added in v1.6.0
func GetEnvEnableColor ¶ added in v1.8.0
func GetEnvEnableColor() bool
func GinErrorLogger ¶ added in v1.1.0
func GinErrorLogger() gin.HandlerFunc
ErrorLogger returns an ErrorLoggerT with parameter gin.ErrorTypeAny
func GinErrorLoggerT ¶ added in v1.1.0
func GinErrorLoggerT(typ gin.ErrorType) gin.HandlerFunc
GinErrorLoggerT returns an GinErrorLoggerT middleware with the given type gin.ErrorType.
func GinLogger ¶ added in v1.1.0
func GinLogger(duration time.Duration) gin.HandlerFunc
Logger prints a logline for each request and measures the time to process for a call. It formats the log entries similar to http://godoc.org/github.com/gin-gonic/gin#Logger does.
Example:
router := gin.New() router.Use(gin.Recovery()) router.Use(klog.Logger(3 * time.Second))
func GinLoggerWithOutPaths ¶ added in v1.4.0
func Info ¶
func Info(args ...interface{})
Info logs to the INFO log. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func InfoDepth ¶
func InfoDepth(depth int, args ...interface{})
InfoDepth acts as Info but uses depth to determine which call frame to log. InfoDepth(0, "msg") is the same as Info("msg").
func InfoS ¶
func InfoS(msg string, keysAndValues ...interface{})
InfoS structured logs to the INFO log. The msg argument used to add constant description to the log line. The key/value pairs would be join by "=" ; a newline is always appended.
Basic examples: >> klog.InfoS("Pod status updated", "pod", "kubedns", "status", "ready") output: >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kubedns" status="ready"
func InfoTag ¶
func InfoTag(tag KlogTag, args ...interface{})
InfoTag logs to the INFO log with tag. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func InfoTagDepth ¶
func Infof ¶
func Infof(format string, args ...interface{})
Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Infoln ¶
func Infoln(args ...interface{})
Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println; a newline is always appended.
func LogToStderr ¶
func LogToStderr(stderr bool)
LogToStderr sets whether to log exclusively to stderr, bypassing outputs
func SetLogger ¶
SetLogger will set the backing logr implementation for klog. If set, all log lines will be suppressed from the regular Output, and redirected to the logr implementation. All log lines include the 'severity', 'file' and 'line' values attached as structured logging values. Use as:
... klog.SetLogger(zapr.NewLogger(zapLog))
func SetOutputBySeverity ¶
SetOutputBySeverity sets the output destination for specific severity
func Warning ¶
func Warning(args ...interface{})
Warning logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func WarningDepth ¶
func WarningDepth(depth int, args ...interface{})
WarningDepth acts as Warning but uses depth to determine which call frame to log. WarningDepth(0, "msg") is the same as Warning("msg").
func WarningTag ¶
func WarningTag(tag KlogTag, args ...interface{})
func WarningTagf ¶
func WarningTagln ¶
func WarningTagln(tag KlogTag, args ...interface{})
Types ¶
type GormLoggerWriter ¶ added in v1.7.0
type GormLoggerWriter struct { }
func (*GormLoggerWriter) Printf ¶ added in v1.7.0
func (l *GormLoggerWriter) Printf(format string, v ...interface{})
type KMetadata ¶
KMetadata is a subset of the kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface this interface may expand in the future, but will always be a subset of the kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface
type Level ¶
type Level int32
Level specifies a level of verbosity for V logs. *Level implements flag.Value; the -v flag is of type Level and should be modified only through the flag.Value interface.
type ObjectRef ¶
ObjectRef references a kubernetes object
type OutputStats ¶
type OutputStats struct {
// contains filtered or unexported fields
}
OutputStats tracks the number of output lines and bytes written.
func (*OutputStats) Bytes ¶
func (s *OutputStats) Bytes() int64
Bytes returns the number of bytes written.
func (*OutputStats) Lines ¶
func (s *OutputStats) Lines() int64
Lines returns the number of lines written.
type Verbose ¶
type Verbose struct {
// contains filtered or unexported fields
}
Verbose is a boolean type that implements Infof (like Printf) etc. See the documentation of V for more information.
func V ¶
V reports whether verbosity at the call site is at least the requested level. The returned value is a struct of type Verbose, which implements Info, Infoln and Infof. These methods will write to the Info log if called. Thus, one may write either
if glog.V(2).Enabled() { klog.Info("log this") }
or
klog.V(2).Info("log this")
The second form is shorter but the first is cheaper if logging is off because it does not evaluate its arguments.
Whether an individual call to V generates a log record depends on the setting of the -v and -vmodule flags; both are off by default. The V call will log if its level is less than or equal to the value of the -v flag, or alternatively if its level is less than or equal to the value of the -vmodule pattern matching the source file containing the call.
func (Verbose) Enabled ¶
Enabled will return true if this log level is enabled, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Error ¶
Error is equivalent to the global Error function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Info ¶
func (v Verbose) Info(args ...interface{})
Info is equivalent to the global Info function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) InfoS ¶
InfoS is equivalent to the global InfoS function, guarded by the value of v. See the documentation of V for usage.