前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊klog的info方法

聊聊klog的info方法

作者头像
code4it
发布2021-01-12 14:53:07
1.3K0
发布2021-01-12 14:53:07
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下klog的info方法

Info

k8s.io/klog/v2@v2.4.0/klog.go

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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之间用=连接,总是会新添加换行

实例

代码语言:javascript
复制
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")
}

输出

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Info
  • InfoDepth
  • Infoln
  • Infof
  • InfoS
  • 实例
  • 小结
  • doc
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档