当年话下
您可以log.SetOutput(...)与使用所需格式(例如内置 RFC3999 或您选择的格式之一)打印条目的参数编写器一起使用,例如:const timeFormat = time.RFC3339type logWriter struct{}func (lw *logWriter) Write(bs []byte) (int, error) { return fmt.Print(time.Now().UTC().Format(timeFormat), " | ", string(bs))}func main() { log.SetFlags(0) // Omit default prefixes. log.SetOutput(new(logWriter)) // Use our custom writer. log.Printf("Hello, %s!", "World") // 2018-09-18T14:50:40Z | Hello, World!}您可以使用不同的格式更改时间戳,例如:const timeFormat = "Jan 2, 2006 at 3:04:05pm (UTC)"// ... log.Printf("Hello, %s!", "World") // Sep 18, 2018 at 2:52:26pm (UTC) | Hello, World!