sequence

This article focuses on klog’s info method

Info

K8s. IO/klog/[email protected] / klog. Go

// 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...) }Copy the code

Info Uses logging.print to print info-level logs. The parameters are treated similarly to fmt. print, except that newlines are appended if there are no newlines

InfoDepth

K8s. IO/klog/[email protected] / klog. Go

// 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...) }Copy the code

InfoDepth specifies the call frame to print. The depth used by Info is 0

Infoln

K8s. IO/klog/[email protected] / klog. Go

// 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...) }Copy the code

Infoln handles arguments like fmt.Println, always adding new line breaks

Infof

K8s. IO/klog/[email protected] / klog. Go

// 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...) }Copy the code

Infof handles arguments similarly to fmt.Printf, appending newlines if there are no newlines

InfoS

K8s. IO/klog/[email protected] / klog. Go

// 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, msg, keysAndValues...) }Copy the code

InfoS is used to print structured logs, kv is connected with =, new line breaks are always added

The instance

import ( "flag" "k8s.io/klog/v2" ) func main() { klog.InitFlags(flag.CommandLine) defer klog.Flush() klog.Info("hello by  Info") klog.InfoDepth(0, "hello by InfoDepth 0") klog.InfoDepth(1, "hello by InfoDepth 1") klog.Infoln("hello by Infoln") klog.Infof("hello by %s", "Infof") klog.InfoS("Pod status updated", "pod", "kubedns", "status", "ready") }Copy the code

The output

I1226 22:29:10.258496 6455 klog_demo.go:16] Hello by Info I1226 22:29:10.258619 6455 klog_demo.go:17] Hello by InfoDepth 0 I1226 22:29:10.258642 6455 proc.go:204] Hello by InfoDepth 1 I1226 22:29:10.258645 6455 klog_demo.go: 204] Hello by InfoDepth 1 I1226 22:29:10.258645 6455 klog_demo.go: 204] Hello by InfoDepth 1 I1226 22:29:10.258645 6455 klog_demo.go: 204 Infoln I1226 22:29:10.258651 6455 klog_demo.go:20] Hello by Infof I1226 22:29:10.258658 6455 klog_demo.go:21] status updated" pod="kubedns" status="ready"Copy the code

summary

Klog provides the Info, InfoDepth, Infoln, Infof, and InfoS methods. Info Uses logging.print to print info-level logs. The parameters are treated in the same way as FMT.Print. If there are no newlines, newlines are appended. InfoDepth specifies the call frame to print. The depth used by Info is 0. Infoln handles arguments like fmt.Println, always adding new line breaks; Infof handles arguments in the same way as FMT.Printf. If there are no newlines, newlines are appended. InfoS is used to print structured logs, kv is connected with =, new line breaks are always added

doc

  • klog