关于golang:聊聊klog的info方法

75次阅读

共计 2573 个字符,预计需要花费 7 分钟才能阅读完成。

本文次要钻研一下 klog 的 info 办法

Info

k8s.io/klog/v2@v2.4.0/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 相似,若没有换行则会追加换行

InfoDepth

k8s.io/klog/v2@v2.4.0/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

Infoln

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 相似,总是会新增加换行

Infof

k8s.io/klog/v2@v2.4.0/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 相似,若没有换行则会追加换行

InfoS

k8s.io/klog/v2@v2.4.0/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 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 之间用 = 连贯,总是会新增加换行

doc

  • klog

正文完
 0