The
Golang Log standard library implements a simple logging service. But does not support log classification, classification, filtering and other complex functions. By a third-party library such as Glog (Github), Seelog (Github)
Simple demo is as follows:
Package main
Import (
"log"
"OS
)
Func main () {
file,err: = os. Create ("Test.log")
if err!= nil {
log. Fatalln ("Fail to create Test.log file!")
}
defer file. Close ()
logger: = log. New (file, "info:", log. Lstdflags|log. Lshortfile)
logger. Println (' This is phpddt.com testing ')//info:2015/11/15 01:11:48 test.go:17:this is phpddt.com testing
}
Visible, the specified prefix can achieve a simple rating.
View the logger structure by viewing the source code:
Type Logger struct {
Mu sync. Mutex//ensures Atomic writes; Protects the following fields
Prefix string//prefix to write at beginning of
Flag Int//Properties
Out IO. Writer//destination for output
BUF []byte//For accumulating text to write
}
Constant for log format flag:
Const (
Ldate = 1 << iota//The date in the local time ZONE:2009/01/23
Ltime//The time zone:01:23:23
Lmicroseconds//Microsecond resolution:01:23:23.123123. Assumes Ltime.
Llongfile//full file name and line number:/a/b/c/d.go:23
Lshortfile//Final file name element and line number:d.go:23. Overrides Llongfile
LUTC//If Ldate or Ltime is set, with UTC rather than the local time zone
Lstdflags = Ldate | Ltime//Initial values for the standard logger
)
The Go-log.pnggolang log implements three types of interfaces:
Print General message output
Fatal forced out
Panic output log after panic