How to change the date/time format of Go’s log package

Like yed posterior said, you can define a custom io.Writer by implementing a write function. You’ll also probably want to do a log.SetFlags(0) to take full control. Here’s an example that changes the date format as well as adds some log level info.

type logWriter struct {
}

func (writer logWriter) Write(bytes []byte) (int, error) {
    return fmt.Print(time.Now().UTC().Format("2006-01-02T15:04:05.999Z") + " [DEBUG] " + string(bytes))
}

func main() {

    log.SetFlags(0)
    log.SetOutput(new(logWriter))
    log.Println("This is something being logged!")
}

outputs:

2016-03-21T19:54:28.563Z [DEBUG] This is something being logged!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)