klog

klog #

V() 级别 #

级别 含义
v=0 Generally useful for this to always be visible to a cluster operator.
v=1 A reasonable default log level if you don’t want verbosity.
v=2 Useful steady state information about the service and important log messages that may correlate to significant changes in the system.
This is the recommended default log level for most systems.
v=3 Extended information about changes.
v=4 Debug level verbosity.
v=5 Trace level verbosity.
v=6 Display requested resources.
v=7 Display HTTP request headers.
v=8 Display HTTP request contents.
v=9 Display HTTP request contents without truncation of contents.

详细说明 #

  • klog.V(0) - Generally useful for this to ALWAYS be visible to an operator
    • Programmer errors
    • Logging extra info about a panic
    • CLI argument handling
  • klog.V(1) - A reasonable default log level if you don’t want verbosity.
    • Information about config (listening on X, watching Y)
    • Errors that repeat frequently that relate to conditions that can be corrected (pod detected as unhealthy)
  • klog.V(2) - Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems.
    • Logging HTTP requests and their exit code
    • System state changing (killing pod)
    • Controller state change events (starting pods)
    • Scheduler log messages
  • klog.V(3) - Extended information about changes
    • More info about system state changes
  • klog.V(4) - Debug level verbosity
    • Logging in particularly thorny parts of code where you may want to come back later and check it
  • klog.V(5) - Trace level verbosity
    • Context to understand the steps leading up to errors and warnings
    • More information for troubleshooting reported issues

参考:

// 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 V(level Level) Verbose {
	// This function tries hard to be cheap unless there's work to do.
	// The fast path is two atomic loads and compares.

	// Here is a cheap but safe test to see if V logging is enabled globally.
	if logging.verbosity.get() >= level {
		return newVerbose(level, true)
	}

	// It's off globally but vmodule may still be set.
	// Here is another cheap but safe test to see if vmodule is enabled.
	if atomic.LoadInt32(&logging.filterLength) > 0 {
		// Now we need a proper lock to use the logging structure. The pcs field
		// is shared so we must lock before accessing it. This is fairly expensive,
		// but if V logging is enabled we're slow anyway.
		logging.mu.Lock()
		defer logging.mu.Unlock()
		if runtime.Callers(2, logging.pcs[:]) == 0 {
			return newVerbose(level, false)
		}
		v, ok := logging.vmap[logging.pcs[0]]
		if !ok {
			v = logging.setV(logging.pcs[0])
		}
		return newVerbose(level, v >= level)
	}
	return newVerbose(level, false)
}

Verbose 函数列表 #

func (v Verbose) Enabled() bool
func (v Verbose) Info(args ...interface{})
func (v Verbose) Infoln(args ...interface{})
func (v Verbose) Infof(format string, args ...interface{})
func (v Verbose) InfoS(msg string, keysAndValues ...interface{})
func (v Verbose) Error(err error, msg string, args ...interface{})
func (v Verbose) ErrorS(err error, msg string, keysAndValues ...interface{})

注意事项 #

使用 Infos, InfoSDepth, ErrorS, ErrorSDepth #

  • klog.Infofklog.Infoklog.Infoln -> klog.InfoS
  • klog.InfoDepth -> klog.InfoSDepth
  • klog.V(N).Infofklog.V(N).Infoklog.V(N).Infoln -> klog.V(N).InfoS
  • klog.Warningklog.Warningfklog.Warningln -> klog.InfoS
  • klog.WarningDepth -> klog.InfoSDepth
  • klog.Errorklog.Errorfklog.Errorln -> klog.ErrorS
  • klog.ErrorDepth -> klog.ErrorSDepth
  • klog.Fatalklog.Fatalfklog.Fatalln -> klog.ErrorS followed by os.Exit(1)
  • klog.FatalDepth -> klog.ErrorDepth followed by os.Exit(1)
// Info logs to the INFO log.
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func Info(args ...interface{}) {
	logging.print(infoLog, logging.logr, logging.filter, args...)
}

// InfoDepth acts as Info but uses depth to determine which call frame to log.
// InfoDepth(0, "msg") is the same as Info("msg").
func InfoDepth(depth int, args ...interface{}) {
	logging.printDepth(infoLog, logging.logr, logging.filter, depth, args...)
}

// Infoln logs to the INFO log.
// Arguments are handled in the manner of fmt.Println; a newline is always appended.
func Infoln(args ...interface{}) {
	logging.println(infoLog, logging.logr, logging.filter, args...)
}

// Infof logs to the INFO log.
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Infof(format string, args ...interface{}) {
	logging.printf(infoLog, logging.logr, logging.filter, format, args...)
}

// 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 InfoS(msg string, keysAndValues ...interface{}) {
	logging.infoS(logging.logr, logging.filter, 0, msg, keysAndValues...)
}

// if loggr is specified, will call loggr.Info, otherwise output with logging module.
func (l *loggingT) infoS(loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
	if filter != nil {
		msg, keysAndValues = filter.FilterS(msg, keysAndValues)
	}
	if loggr != nil {
		loggr.Info(msg, keysAndValues...)
		return
	}
	l.printS(nil, infoLog, depth+1, msg, keysAndValues...)
}

// logr.Logger.Info
// https://github.com/go-logr/logr

// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
// the log line.  The key/value pairs can then be used to add additional
// variable information.  The key/value pairs should alternate string
// keys and arbitrary values.
Info(msg string, keysAndValues ...interface{})

参考:

不要用 Fatal() #

klog.Fatal() 打印日志后,会调用 os.Exit(255)

应该改用 klog.ErrorS(),再自己调用 os.Exit(255)

// 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 Fatal(args ...interface{}) {
	logging.print(fatalLog, logging.logr, logging.filter, args...)
}

参考:Replacing Fatal calls


本文访问量

本站总访问量

本站总访客数