本文主要研究一下klog的info方法git
k8s.io/klog/v2@v2.4.0/klog.gogithub
// 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...) }
Info使用logging.print打印info級別的日誌,參數的處理跟fmt.Print相似,若沒有換行則會追加換行
k8s.io/klog/v2@v2.4.0/klog.goapp
// 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...) }
InfoDepth能夠指定要打印的call frame,Info使用的depth爲0
k8s.io/klog/v2@v2.4.0/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...) }
Infoln的參數處理與fmt.Println相似,老是會新添加換行
k8s.io/klog/v2@v2.4.0/klog.gocode
// 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...) }
Infof的參數處理與fmt.Printf相似,若沒有換行則會追加換行
k8s.io/klog/v2@v2.4.0/klog.goorm
// 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...) }
InfoS用於打印結構化的日誌,kv之間用
=
鏈接,老是會新添加換行
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") }
輸出dns
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:19] hello by Infoln I1226 22:29:10.258651 6455 klog_demo.go:20] hello by Infof I1226 22:29:10.258658 6455 klog_demo.go:21] "Pod status updated" pod="kubedns" status="ready"
klog提供了Info、InfoDepth、Infoln、Infof、InfoS方法;Info使用logging.print打印info級別的日誌,參數的處理跟fmt.Print相似,若沒有換行則會追加換行;InfoDepth能夠指定要打印的call frame,Info使用的depth爲0;Infoln的參數處理與fmt.Println相似,老是會新添加換行;Infof的參數處理與fmt.Printf相似,若沒有換行則會追加換行;InfoS用於打印結構化的日誌,kv之間用=
鏈接,老是會新添加換行ip