本文主要研究一下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