Пример #1
0
func TestDefaultFormat(t *testing.T) {
	buf := bytes.NewBuffer(make([]byte, 4096))
	log.SetWriter(buf)

	msg := "this is a test message"
	log.Info(msg)
	if ok, _ := regexp.Match(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} -? ?info test/deeper/log_test.go:\d+ this is a test message`, buf.Bytes()); !ok {
		t.Logf("%s", buf.Bytes()) // 2016-01-24 19:41:19 info test/deeper/log_test.go:16 this is a test message
		t.FailNow()
	}
}
Пример #2
0
func TestSetFormatFile(t *testing.T) {
	format := fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.PackageToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)

	buf := bytes.NewBuffer(make([]byte, 4096))
	log.SetWriter(buf)

	rand := time.Now().String()
	log.Debug(rand)
	if bytes.HasPrefix(buf.Bytes(), ([]byte)("<file>github.com/gotips/log/test/deeper/log_test.go</file>")) {
		t.FailNow()
	}
}
Пример #3
0
func xTestPrint(t *testing.T) {
	file, err := os.OpenFile("test.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	log.SetWriter(file)

	for i := 0; i < 100e4; i++ {
		if i%1e4 == 0 {
			fmt.Println(i)
		}
		log.Info("can't load package: package lib: cannot find package `xxx` in any of")
	}

}
Пример #4
0
func execChangeWriterExample() {
	// 改变 Writer,把日志打印到缓冲区,也可以打印到文件,定时切换文件,可以实现日志滚动
	buf := bytes.NewBuffer(make([]byte, 255))
	log.SetWriter(buf)

	log.Infof(msgFmt, 15)

	// 查看缓冲区
	// 每条日志结尾会加一个换行符
	line, err := buf.ReadString('\n')
	if err != nil {
		fmt.Println(err) // 这里不能再调用 log.Error()
		os.Exit(1)
	}

	fmt.Print(line)
}