序
本文主要研究一下klog的info方法
Info
// 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类似,若没有换行则会追加换行
InfoDepth
// 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
Infoln
// 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类似,总是会新添加换行
Infof
// 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类似,若没有换行则会追加换行
InfoS
// 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")
}
输出
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之间用=
连接,总是会新添加换行
网友评论