Go Engineering - Foundation - CLI
CLI
Cobra 很好地集成了 Pflag 和 Viper
工具包
用途
Pflag
命令行参数解析
Viper
配置文件解析
Cobra
命令行框架
Pflag
Pflag 通过创建 Flag 和 FlagSet 来使用,使用 Pflag 的开源项目:Kubernetes、Istio、Helm、Docker、Etcd
Flag
一个命令行参数会被解析成一个 Flag 类型的变量
1234567891011121314// A Flag represents the state of a flag.type Flag struct { Name string // name as it appears on command line Shorthand string // one-letter abbreviated flag Usage string // help message V ...
Go Engineering - Foundation - Log - Package
开源日志包标准库 log 包
标准库自带,无需安装
只提供 Print、Panic、Fatal 函数用于日志输出
Go 标准库大量使用了该 log 包
glog
Kubernetes 使用的 klog 是基于 glog 进行封装
Google 推出的轻量级日志包
特性
支持 4 种日志级别: Info、Warning、Error、Fatal
支持命令行选项
支持根据文件大小切割日志文件
支持日志按级别分类输出
支持 V level – 开发者自定义日志级别
支持 vmodule – 开发者对不同的文件使用不同的日志级别
支持 traceLocation – 打印指定位置的栈信息
logrus
Github star 数量最多的日志包,Docker 和 Prometheus 也在使用 logrus
支持常用的日志级别
可扩展:允许使用者通过 Hook 的方式,将日志分发到任意地方
支持自定义的日志格式:内置支持 JSON 和 TEXT
结构化日志记录:Field 机制允许使用者自定义字段
预设日志字段:Default Field 机制,可以给一部分或者全部日志统一添加共同的日志字段
Fa ...