Ejemplo n.º 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")
	})

}
Ejemplo n.º 2
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)
	}
}
Ejemplo n.º 3
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)
	})
}