I Spy Code - GO

Setting output flags

You can use flags to change the type of time and date stamp on your message.

Here is a go lang example that shows how to set the output flags for the logger using SetFlags.

Source: (example.go)

package main
 
import (
    "log"
)
 
func main() {
    log.Println("Default")
 
    // the date in the local time zone: 2009/01/23
    log.SetFlags(log.Ldate)
    log.Println("Using log.Ldate")
 
    // the time in the local time zone: 01:23:23
    log.SetFlags(log.Ltime)
    log.Println("Using log.Ltime")
 
    // microsecond resolution: 01:23:23.123123
    log.SetFlags(log.Lmicroseconds)
    log.Println("Using log.Lmicroseconds")
 
    // full file name and line number: /a/b/c/d.go:23
    log.SetFlags(log.Llongfile)
    log.Println("Using log.Llongfile")
 
    // final file name element and line number: d.go:23
    log.SetFlags(log.Lshortfile)
    log.Println("Using log.Lshortfile")
 
    // if Ldate or Ltime is set, use UTC rather than the local time zone
    log.SetFlags(log.Ltime | log.LUTC)
    log.Println("Using log.LUTC")
 
    // initial values for the standard logger
    log.SetFlags(log.LstdFlags)
    log.Println("Using log.LstdFlags")
}
 

Output:

$ go run example.go
2016/10/11 06:36:12 Default
2016/10/11 Using log.Ldate
06:36:12 Using log.Ltime
06:36:12.882487 Using log.Lmicroseconds
/home/dennis/ISPY/GO/example.go:24: Using log.Llongfile
example.go:28: Using log.Lshortfile
10:36:12 Using log.LUTC
2016/10/11 06:36:12 Using log.LstdFlags