예제 #1
0
// TestLogError ...
func TestLogError(t *testing.T) {
	Convey("Check Log standart error", t, func() {
		buf := &bytes.Buffer{}
		logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.AddSync(buf)))
		logger.StubTime()

		LogError(logger, errors.New("message"))
		So(string(buf.Bytes()), ShouldEqual, `{"msg":"message","level":"error","ts":0,"fields":{}}`+"\n")
	})

	Convey("Check Log ErrorEx", t, func() {
		callerTest = true
		defer func() { callerTest = false }()

		buf := &bytes.Buffer{}
		logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.AddSync(buf)))
		logger.StubTime()

		LogError(logger, NewEx(zap.DebugLevel, "message",
			zap.String("field1", "field1 data"),
			zap.Int("field2", 5)))

		So(string(buf.Bytes()), ShouldEqual,
			`{"msg":"message","level":"debug","ts":0,"fields":{"caller":"<fake>","field1":"field1 data","field2":5}}`+"\n")
	})

}
예제 #2
0
파일: main.go 프로젝트: plumbum/go-samples
func main() {
	mongoSession, err := mgo.Dial("127.0.0.1")
	if err != nil {
		panic(err)
	}
	defer mongoSession.Close()

	mongoSession.SetMode(mgo.Monotonic, true)

	log := zap.NewJSON(
		zap.Debug,
		zap.Fields(zap.Int("count", 1)),
		zap.Output(NewWriter(mongoSession)),
	)
	url := "http://example.local"
	tryNum := 42
	startTime := time.Now()
	for i := range [logCounts]struct{}{} {
		log.Info("Failed to fetch URL.",
			zap.String("url", url),
			zap.Int("attempt", tryNum),
			zap.Duration("backoff", time.Since(startTime)),
			zap.Int("index", i),
		)
	}
	fmt.Printf("Finished in %v\n", time.Since(startTime))
}
예제 #3
0
func main() {
	f, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		fmt.Printf("%s", err)
		return
	}

	defer clearCloseFile(f)

	file, err := os.OpenFile("app.json", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.AddSync(file)))

	log.SetOutput(f)

	// runtime.GOMAXPROCS(runtime.NumCPU())
	err = run(logger)
	// err = test()
	if err != nil {
		fmt.Printf("%s", err)
	}
}
예제 #4
0
// TestAddURL ...
func TestAddURL(t *testing.T) {
	Convey("Add URLs", t, func() {
		h := helpNewHTMLMetadata()

		h.AddURL("/link1")
		h.AddURL("")
		h.AddURL("link2")
		h.AddURL("link3/link3")
		h.AddURL("http://testhost2/link4")
		h.AddURL("  link5  ")
		h.AddURL("/wrong%9")

		hostIDValid := sql.NullInt64{Int64: 1, Valid: true}
		hostIDInvalid := sql.NullInt64{Valid: false}

		expectedURLs := make(map[string]sql.NullInt64)
		expectedURLs["http://testhost1/link1"] = hostIDValid
		expectedURLs["http://testhost1/test/link2"] = hostIDValid
		expectedURLs["http://testhost1/test/link3/link3"] = hostIDValid
		expectedURLs["http://testhost2/link4"] = hostIDInvalid
		expectedURLs["http://testhost1/test/link5"] = hostIDValid
		So(h.URLs, ShouldResemble, expectedURLs)

		expectedWrongURLs := make(map[string]string)
		expectedWrongURLs["/wrong%9"] = `parse /wrong%9: invalid URL escape "%9"`
		So(h.wrongURLs, ShouldResemble, expectedWrongURLs)
	})

	Convey("Wrong URLs to log", t, func() {
		buf := &bytes.Buffer{}
		logger := zap.NewJSON(zap.DebugLevel, zap.Output(zap.AddSync(buf)))
		logger.StubTime()

		h := helpNewHTMLMetadata()
		h.AddURL("/link1")
		h.AddURL("/wrong%2")
		h.WrongURLsToLog(logger)

		expected := `{"msg":"Error parse URL","level":"warn","ts":0,"fields":{"err_url":"/wrong%2","details":"parse /wrong%2: invalid URL escape \"%2\""}}
`
		So(string(buf.Bytes()), ShouldEqual, expected)
	})
}