golang k8s.io/klog包详解

网友投稿 625 2022-09-11

golang k8s.io/klog包详解

1.导入包

go get "k8s.io/klog"

2.源码解析

/ Errorln logs to the ERROR, WARNING, and INFO logs.// Arguments are handled in the manner of fmt.Println; a newline is always appended.func Errorln(args ...interface{}) { logging.println(errorLog, args...)}// Errorf logs to the ERROR, WARNING, and INFO logs.// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.func Errorf(format string, args ...interface{}) { logging.printf(errorLog, format, args...)}// Fatal logs to the FATAL, ERROR, WARNING, and INFO logs,// including a stack trace of all running goroutines, then calls os.Exit(255).// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.func Fatal(args ...interface{}) { logging.print(fatalLog, args...)}// FatalDepth acts as Fatal but uses depth to determine which call frame to log.// FatalDepth(0, "msg") is the same as Fatal("msg").func FatalDepth(depth int, args ...interface{}) { logging.printDepth(fatalLog, depth, args...)}// Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs,// including a stack trace of all running goroutines, then calls os.Exit(255).// Arguments are handled in the manner of fmt.Println; a newline is always appended.func Fatalln(args ...interface{}) { logging.println(fatalLog, args...)}// Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs,// including a stack trace of all running goroutines, then calls os.Exit(255).// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.func Fatalf(format string, args ...interface{}) { logging.printf(fatalLog, format, args...)}// fatalNoStacks is non-zero if we are to exit without dumping goroutine stacks.// It allows Exit and relatives to use the Fatal logs.var fatalNoStacks uint32// Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.func Exit(args ...interface{}) { atomic.StoreUint32(&fatalNoStacks, 1) logging.print(fatalLog, args...)}// ExitDepth acts as Exit but uses depth to determine which call frame to log.// ExitDepth(0, "msg") is the same as Exit("msg").func ExitDepth(depth int, args ...interface{}) { atomic.StoreUint32(&fatalNoStacks, 1) logging.printDepth(fatalLog, depth, args...)}// Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).func Exitln(args ...interface{}) { atomic.StoreUint32(&fatalNoStacks, 1) logging.println(fatalLog, args...)}// Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.func Exitf(format string, args ...interface{}) { atomic.StoreUint32(&fatalNoStacks, 1) logging.printf(fatalLog, format, args...)}

// InitFlags is for explicitly initializing the flags.func InitFlags(flagset *flag.FlagSet) { if flagset == nil { flagset = flag.CommandLine } flagset.StringVar(&logging.logDir, "log_dir", logging.logDir, "If non-empty, write log files in this directory") flagset.StringVar(&logging.logFile, "log_file", logging.logFile, "If non-empty, use this log file") flagset.Uint64Var(&logging.logFileMaxSizeMB, "log_file_max_size", logging.logFileMaxSizeMB, "Defines the maximum size a log file can grow to. Unit is megabytes. "+ "If the value is 0, the maximum file size is unlimited.") flagset.BoolVar(&logging.toStderr, "logtostderr", logging.toStderr, "log to standard error instead of files") flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", logging.alsoToStderr, "log to standard error as well as files") flagset.Var(&logging.verbosity, "v", "number for the log level verbosity") flagset.BoolVar(&logging.skipHeaders, "add_dir_header", logging.addDirHeader, "If true, adds the file directory to the header") flagset.BoolVar(&logging.skipHeaders, "skip_headers", logging.skipHeaders, "If true, avoid header prefixes in the log messages") flagset.BoolVar(&logging.skipLogHeaders, "skip_log_headers", logging.skipLogHeaders, "If true, avoid headers when opening log files") flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr") flagset.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging") flagset.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")}

打印格式在这个包下控制

3.实际运用

package mainimport ( "k8s.io/klog")func task(){ klog.Errorf("error") klog.Info("info") klog.Warning("warning")}func main() { task()}

4.运行结果

E0401 10:10:07.008441 10032 main.go:10] errorI0401 10:10:07.036573 10032 main.go:11] infoW0401 10:10:07.036573 10032 main.go:12] warning

5.日志输出到文件

flag.Set("logtostderr", "false") //日志输出到stderr,不输出到日志文件。false为关闭 flag.Set("log_file", "myfile.log")

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:k8s节点如何从Harbor中拉取镜像的?镜像拉取凭证的配置
下一篇:机票盲盒是营销噱头还是真实惠?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~