l := log.New(os.Stdout, "[AAA] ", 2)
l.Printf("Listening on %s", addr)
Prints out [AAA] 17:07:51 Listening on ...
If you want to add the date, use log.Ldate | log.Ltime
as flag:
l := log.New(os.Stdout, "[AAA] ", log.Ldate | log.Ltime)
l.Printf("Listening on %s", addr)
// [AAA] 2016-07-14 17:07:51 Listening on ...
But for having the date and time before prefix, you can use a customized writer:
type writer struct {
io.Writer
timeFormat string
}
func (w writer) Write(b []byte) (n int, err error) {
return w.Writer.Write(append([]byte(time.Now().Format(w.timeFormat)), b...))
}
Then, pass the custom writer to log's New
:
l := log.New(&writer{os.Stdout, "2006-01-02 15:04:05 "}, "[AAA] ", 0)
l.Printf("Listening on %s", addr)
// 2016-07-14 17:07:51 [AAA] Listening on ...
There is a complete example here.