// 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") }) }
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)) }
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) } }
func newBot() (b MMJira) { b = MMJira{l: zap.NewJSON(zap.DebugLevel), reg: metrics.NewRegistry()} data, err := ioutil.ReadFile("config.yaml") if err != nil { b.l.Panic("not able to read the file", zap.Error(err)) } var config InstanceConfig if err = yaml.Unmarshal(data, &config); err != nil { b.l.Panic("not able to marshal the file", zap.Error(err)) } b.c = &config if !b.c.Debug { b.l.SetLevel(zap.ErrorLevel) } mmpost, err := mmcontroller.NewController(b.c.MMicon, b.c.MMuser, b.c.Hooks, b.c.Debug, metrics.NewPrefixedChildRegistry(b.reg, "mmc.")) if err != nil { panic(err) } b.m = mmpost b.l.Debug("outputting config", zap.Object("config", b.c)) b.r = mux.NewRouter() b.r.HandleFunc("/", b.homeHandler) b.r.HandleFunc("/hooks/", b.getHandler).Methods("GET") b.r.HandleFunc("/hooks/{hookid}", b.postHandler).Methods("POST") b.r.Handle("/metrics", exp.ExpHandler(b.reg)) b.r.HandleFunc("/config/", b.configGetHandler).Methods("GET") return b }
// NewController is used to create a MMController func NewController(icon string, name string, hooks map[string]string, debug bool, reg metrics.Registry) (m *Controller, err error) { m = new(Controller) m.icon = icon m.name = name m.hooks = hooks m.l = zap.NewJSON(zap.ErrorLevel) if debug { m.l.SetLevel(zap.DebugLevel) } m.converter = jira.FNew() m.reg = reg return m, nil }
// 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) }) }
func init() { log = logger{zap.NewJSON(zap.AddCaller(), zap.AddStacks(zap.FatalLevel))} }