golang log标准库实现了简单日志服务。但不支持log分类、分级、过滤等复杂功能。由第三方库如glog(Github)、seelog(Github)
简单demo如下:
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
}
type Logger struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
prefix string // prefix to write at beginning of each line
flag int // properties
out io.Writer // destination for output
buf []byte // for accumulating text to write
}
const (
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Ltime // the time in the local 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, use UTC rather than the local time zone
LstdFlags = Ldate | Ltime // initial values for the standard logger
)