序
本文主要研究一下klog的info方法
Infok8s.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...)}
Info使用logging.print列印info級別的日誌,引數的處理跟fmt.Print類似,若沒有換行則會追加換行
InfoDepthk8s.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...)}
InfoDepth可以指定要列印的call frame,Info使用的depth為0
Infolnk8s.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...)}
Infoln的引數處理與fmt.Println類似,總是會新新增換行
Infofk8s.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...)}
Infof的引數處理與fmt.Printf類似,若沒有換行則會追加換行
InfoSk8s.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...)}
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 InfoI1226 22:29:10.258619 6455 klog_demo.go:17] hello by InfoDepth 0I1226 22:29:10.258642 6455 proc.go:204] hello by InfoDepth 1I1226 22:29:10.258645 6455 klog_demo.go:19] hello by InfolnI1226 22:29:10.258651 6455 klog_demo.go:20] hello by InfofI1226 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之間用=連線,總是會新新增換行
docklog
最新評論