// messageHandler is called when a new message arrives func handleMessage(client *MQTT.Client, msg MQTT.Message) { // Unmarshal JSON to RxPacket var packet shared.RxPacket err := json.Unmarshal(msg.Payload(), &packet) if err != nil { log.WithField("topic", msg.Topic()).WithError(err).Warn("Failed to unmarshal JSON.") return } // Filter messages by gateway if gateway != "" && packet.GatewayEui != gateway { return } // Decode payload data, err := base64.StdEncoding.DecodeString(packet.Data) if err != nil { log.WithField("topic", msg.Topic()).WithError(err).Warn("Failed to decode Payload.") return } ctx := log.WithFields(log.Fields{ "devAddr": packet.NodeEui, }) if showMeta { ctx = ctx.WithFields(log.Fields{ "gatewayEui": packet.GatewayEui, "time": packet.Time, "frequency": *packet.Frequency, "dataRate": packet.DataRate, "rssi": *packet.Rssi, "snr": *packet.Snr, }) } if showRaw { ctx = ctx.WithField("data", fmt.Sprintf("%x", data)) } if showTiming { rawData, err := base64.StdEncoding.DecodeString(packet.Data) if err == nil { airtime, err := util.CalculatePacketTime(len(rawData), packet.DataRate) if err == nil { ctx = ctx.WithField("airtime", fmt.Sprintf("%.1f ms", airtime)) } } } // Check for unprintable characters unprintable, _ := regexp.Compile(`[^[:print:]]`) if unprintable.Match(data) { ctx.Debug("Received Message") } else { ctx.WithField("message", fmt.Sprintf("%s", data)).Info("Received Message") } }
// Multiple fields can be set, via chaining, or WithFields(). func Example_multipleFields() { log.WithFields(log.Fields{ "user": "******", "file": "sloth.png", "type": "image/png", }).Info("upload") }
//Info prints impulse with label func (c Correction) Info(label string) { log.WithFields(log.Fields{ "M": c.Move, "D": fmt.Sprintf("%3d", c.Degree()), "T": c.Type(), }).Info(label) }
func TestFielder(t *testing.T) { h := memory.New() log.SetHandler(h) pet := &Pet{"Tobi", 3} log.WithFields(pet).Info("add pet") e := h.Entries[0] assert.Equal(t, log.Fields{"name": "Tobi", "age": 3}, e.Fields) }
func (m *Markup) printStackTree() { for _, one := range m.Impulses { log.WithFields(log.Fields{ "Time": fmt.Sprintf("%s->%s", one.Base.T, one.End.T), "Move": fmt.Sprintf("%.2f->%.2f", one.Base.P, one.End.P), "Prev": one.PrevWave(), "Next": one.NextWave(), }).Debug("Imp") } for _, one := range m.Corrections { log.WithFields(log.Fields{ "Time": fmt.Sprintf("%s->%s", one.Base.T, one.End.T), "Move": fmt.Sprintf("%.2f->%.2f", one.Base.P, one.End.P), "Prev": one.PrevWave(), "Next": one.NextWave(), }).Debug("Cor") } }
func main() { log.SetHandler(text.New(os.Stderr)) ctx := log.WithFields(log.Fields{ "app": "myapp", "env": "prod", }) for range time.Tick(time.Second) { _ = work(ctx) } }
func (m *Markup) printStack() { for _, one := range m.Impulses { log.WithFields(log.Fields{ "D": one.Duration(), "P": fmt.Sprintf("%.2f->%.2f", one.Base.P, one.End.P), "W1": one.W1, "W2": one.W2, "W3": one.W3, "W4": one.W4, "W5": one.W5, }).Debug("Imp") } for _, one := range m.Corrections { log.WithFields(log.Fields{ "D": one.Duration(), "P": fmt.Sprintf("%.2f->%.2f", one.Base.P, one.End.P), "T": one.Type(), }).Debug("Corr") } }
func main() { log.SetHandler(json.New(os.Stderr)) ctx := log.WithFields(log.Fields{ "file": "something.png", "type": "image/png", "user": "******", }) for range time.Tick(time.Millisecond * 200) { ctx.Info("upload") ctx.Info("upload complete") ctx.Warn("upload retry") ctx.WithError(errors.New("unauthorized")).Error("upload failed") } }
func main() { log.SetHandler(multi.New( text.New(os.Stderr), kinesis.New("logs"), )) ctx := log.WithFields(log.Fields{ "file": "something.png", "type": "image/png", "user": "******", }) for range time.Tick(time.Millisecond * 100) { ctx.Info("upload") ctx.Info("upload complete") } }
// Run terraform command in infrastructure directory. func (p *Proxy) Run(args ...string) error { if p.shouldInjectVars(args) { args = append(args, p.functionVars()...) } log.WithFields(log.Fields{ "args": args, }).Debug("terraform") cmd := exec.Command("terraform", args...) cmd.Env = append(os.Environ(), fmt.Sprintf("AWS_REGION=%s", p.Region)) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Dir = filepath.Join(Dir, p.Environment) return cmd.Run() }
func main() { flag.Parse() if *cfgAddr == "" { fmt.Println("required flag not provided: --config-http-address") os.Exit(1) } if *dnsAddr == "" { fmt.Println("required flag not provided: --lookupd-dns-address") os.Exit(1) } log.SetHandler(text.New(os.Stderr)) ctx := log.WithFields(log.Fields{ "cfgAddr": *cfgAddr, "dnsAddr": *dnsAddr, "ldPort": *ldPort, }) ips, err := dnscfg.Get(dnsAddr, ldPort) if err != nil { ctx.WithError(err).Error("dns lookup") os.Exit(1) } if len(ips) == 0 { ctx.Error("no ip addresses found") os.Exit(1) } cfgURL := "http://" + *cfgAddr + "/config/nsqlookupd_tcp_addresses" err = httpcfg.Set(cfgURL, ips) if err != nil { ctx.WithError(err).Error("setting config") } else { ctx.WithField("ips", ips).Info("setting config") } go configLoop(ctx, cfgURL) http.ListenAndServe(":6060", nil) }
func (m *Markup) addCorrection(correction *Correction) *Correction { context := log.WithFields(log.Fields{ "M": correction.Move, "D": correction.Degree, }) for _, one := range m.Corrections { if one.Move.Base.P == correction.Move.Base.P && one.Move.End.P == correction.Move.End.P { context.Debug("Correction found") return one } } context.Debug("+Correction") m.Corrections = append(m.Corrections, correction) return correction }
func (m *Markup) addImpulse(impulse *Impulse) *Impulse { context := log.WithFields(log.Fields{ "M": impulse.Move, "D": impulse.Degree, }) for _, one := range m.Impulses { if one.Move.Base.P == impulse.Move.Base.P && one.Move.End.P == impulse.Move.End.P { context.Debug("Impulse found") return one } } context.Debug("+Impulse") m.Impulses = append(m.Impulses, impulse) return impulse }
func main() { esClient := elastic.New("http://192.168.99.101:9200") esClient.HTTPClient = &http.Client{ Timeout: 5 * time.Second, } e := es.New(&es.Config{ Client: esClient, BufferSize: 100, }) t := text.New(os.Stderr) log.SetHandler(multi.New(e, t)) ctx := log.WithFields(log.Fields{ "file": "something.png", "type": "image/png", "user": "******", }) go func() { for range time.Tick(time.Millisecond * 200) { ctx.Info("upload") ctx.Info("upload complete") ctx.Warn("upload retry") ctx.WithError(errors.New("unauthorized")).Error("upload failed") ctx.Errorf("failed to upload %s", "img.png") } }() go func() { for range time.Tick(time.Millisecond * 25) { ctx.Info("upload") } }() select {} }
func main() { log.SetHandler(cli.Default) log.SetLevel(log.DebugLevel) ctx := log.WithFields(log.Fields{ "file": "something.png", "type": "image/png", "user": "******", }) go func() { for range time.Tick(time.Second) { ctx.Debug("doing stuff") } }() go func() { for range time.Tick(100 * time.Millisecond) { ctx.Info("uploading") ctx.Info("upload complete") } }() go func() { for range time.Tick(time.Second) { ctx.Warn("upload slow") } }() go func() { for range time.Tick(2 * time.Second) { err := errors.New("boom") ctx.WithError(err).Error("upload failed") } }() select {} }
func main() { flag.Parse() log.SetHandler(text.New(os.Stderr)) if *verbose { log.SetLevel(log.DebugLevel) } else { log.SetLevel(log.InfoLevel) } if _, err := os.Stat(tpl); os.IsNotExist(err) { log.WithError(err).Fatal("template provided does not exist") } ctx := log.WithFields(log.Fields{ "app": "spacegophers", }) s := NewServer(ctx, *addr, tpl) // serve the server s.Serve() }
//Info prints impulse with label func (i Impulse) Info(label string) { log.WithFields(log.Fields{ "M": i.Move, "D": fmt.Sprintf("%3d", i.Degree()), }).Info(label) }
//Info corrections selector func (w Wave) Info(label string) { log.WithFields(log.Fields{ "M": w.Move, "D": w.Degree(), }).Info(label) }