ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
LOG MODULES: ### Author:品茶 > Go log实现了一个简单的日志包,定义了一个logger类型,并提供了一些日志格式化的方法,输出到标准输出,如果日志没有换行,log模块会自动加上换行。 > 品茶:简单理解就是:按特定方式如何输出并根据情况调用不同的方案 方法集列表: ~~~ Constants 致命错误,等于print()加上os.exit(1) func Fatal(v ...interface{}) 致命错误,等于printf()加上os.exit(1) 带格式化 func Fatalf(format string, v ...interface{}) Println func Fatalln(v ...interface{}) func Flags() int func Output(calldepth int, s string) error print printf println 后调用panic() func Panic(v ...interface{}) func Panicf(format string, v ...interface{}) func Panicln(v ...interface{}) 加入日志前缀 func Prefix() string func Print(v ...interface{}) func Printf(format string, v ...interface{}) func Println(v ...interface{}) func SetFlags(flag int) func SetOutput(w io.Writer) func SetPrefix(prefix string) 模块拥有的方法,类型拥有同样的方法,只是用到了OOP的一些概念。 type Logger func New(out io.Writer, prefix string, flag int) *Logger func (l *Logger) Fatal(v ...interface{}) func (l *Logger) Fatalf(format string, v ...interface{}) func (l *Logger) Fatalln(v ...interface{}) func (l *Logger) Flags() int func (l *Logger) Output(calldepth int, s string) error func (l *Logger) Panic(v ...interface{}) func (l *Logger) Panicf(format string, v ...interface{}) func (l *Logger) Panicln(v ...interface{}) func (l *Logger) Prefix() string func (l *Logger) Print(v ...interface{}) func (l *Logger) Printf(format string, v ...interface{}) func (l *Logger) Println(v ...interface{}) func (l *Logger) SetFlags(flag int) func (l *Logger) SetOutput(w io.Writer) func (l *Logger) SetPrefix(prefix string) ~~~ Logger 类型: > mu 锁用来保证原子性写入 > prefix 前缀字符串 > flat 标记 > out 输出的内容 > buf ~~~ // A Logger represents an active logging object that generates lines of // output to an io.Writer. Each logging operation makes a single call to // the Writer's Write method. A Logger can be used simultaneously from // multiple goroutines; it guarantees to serialize access to the Writer. type Logger struct { mu sync.Mutex // ensures atomic writes; protects the following fields prefix string // prefix to write at beginning of each line flag int // properties out io.Writer // destination for output buf []byte // for accumulating text to write } ~~~