Skip to content

Instantly share code, notes, and snippets.

@farshidtz
Last active August 15, 2018 12:06
Show Gist options
  • Save farshidtz/a64cb850e0a0c544823628ca4bc0e995 to your computer and use it in GitHub Desktop.
Save farshidtz/a64cb850e0a0c544823628ca4bc0e995 to your computer and use it in GitHub Desktop.
Go logger with prefix after the timestamp
package main
import (
"io"
"log"
"os"
"time"
)
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...))
}
func main(){
// Without any flags
logger := log.New(&writer{os.Stdout, "2006/01/02 15:04:05 "}, "[info] ", 0)
logger.Println("Hello world!")
// 2016/07/14 16:47:04 [info] Hello world!
// With a flag
logger2 := log.New(&writer{os.Stdout, "2006/01/02 15:04:05 "}, "[info] ", log.Lshortfile)
logger2.Println("Hello world!")
// 2016/07/14 16:50:31 [info] main.go:28: Hello world!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment