刚刚学习了golang 中 使用log package 来记录日志的方法。非常简单,但是应付一般的progress 已经完全足够了。
一下是blackcardriver的代码:
package main
import (
"log"
"os"
"time"
)
func main() {
test_log2()
}
//ouput the log in different flag format,(totally 32 style)
func test_log1() {
//onte that the original content will be covered!
fp, err := os.Create("test.log")
if err != nil {
log.Fatal("create file faild")
}
defer fp.Close()
//recommand to use flag 18
aLoger := log.New(fp, "PreFix :", 0)
for i := 0; i < 33; i++ {
aLoger.SetFlags(i)
aLoger.Println("It is style ", i)
}
}
//create a new file name by time
func create_file_bytime() (*os.File, error) {
ntime := time.Now()
filename := ntime.Format("logs_demo 0102#150405.log")
file, err := os.Create(filename)
return file, err
}
//record log in different file to avoid cover the old logs file
func test_log2() {
osfile, err := create_file_bytime()
if err != nil {
log.Fatal(err)
}
loger := log.New(osfile, "", 18)
loger.Println("Create log Scuess !!!....")
}
附上样式参考案例
PreFix :It is style 0
PreFix :2019/03/30 It is style 1
PreFix :16:45:05 It is style 2
PreFix :2019/03/30 16:45:05 It is style 3
PreFix :16:45:05.963607 It is style 4
PreFix :2019/03/30 16:45:05.963607 It is style 5
PreFix :16:45:05.963607 It is style 6
PreFix :2019/03/30 16:45:05.963607 It is style 7
PreFix :D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 8
PreFix :2019/03/30 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 9
PreFix :16:45:05 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 10
PreFix :2019/03/30 16:45:05 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 11
PreFix :16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 12
PreFix :2019/03/30 16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 13
PreFix :16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 14
PreFix :2019/03/30 16:45:05.963607 D:/WorkPlace/Git WorPlace/Channel/logs-demo/main.go:25: It is style 15
PreFix :main.go:25: It is style 16
PreFix :2019/03/30 main.go:25: It is style 17
PreFix :16:45:05 main.go:25: It is style 18
PreFix :2019/03/30 16:45:05 main.go:25: It is style 19
PreFix :16:45:05.963607 main.go:25: It is style 20
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style 21
PreFix :16:45:05.963607 main.go:25: It is style 22
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style 23
PreFix :main.go:25: It is style 24
PreFix :2019/03/30 main.go:25: It is style 25
PreFix :16:45:05 main.go:25: It is style 26
PreFix :2019/03/30 16:45:05 main.go:25: It is style 27
PreFix :16:45:05.963607 main.go:25: It is style 28
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style 29
PreFix :16:45:05.963607 main.go:25: It is style 30
PreFix :2019/03/30 16:45:05.963607 main.go:25: It is style 31