golang标准库中自带有一个日志功能库,其日志的基本结构体为:
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
}
日志输出可以制定前缀字符串,前缀字符串后是有flag定义的输出格式,包含输出日期和时间等等。并且可以指定日志输出文件。
在将日志输出到指定文件之前,会根据预定义格式在传入的日志信息前面加上头信息,将所有临时信息保存在buf中,然后输出到文件。
标准日志库还可以直接将日志信息打印到控制台输出,方便调试。只需要调用库中对应的打印信息。