func main() { flag.Parse() log.SetHandler(text.New(os.Stderr)) log.SetLevel(log.DebugLevel) // set up producer svc := kinesis.New(session.New()) p := producer.New(producer.Config{ StreamName: *stream, BacklogSize: 500, Client: svc, }) p.Start() // open data file f, err := os.Open("/tmp/users.txt") if err != nil { log.Fatal("Cannot open users.txt file") } defer f.Close() // loop over file data b := bufio.NewScanner(f) for b.Scan() { err := p.Put(b.Bytes(), "site") if err != nil { log.WithError(err).Fatal("error producing") } } p.Stop() }
func respondWithGRPCError(w http.ResponseWriter, err error) { const fallback = `{"error": "failed to marshal error message"}` var ( code = grpc.Code(err) desc = grpc.ErrorDesc(err) status = httpStatusFromCode(code) msg struct { Error string `json:"error"` } ) msg.Error = desc data, err := json.Marshal(&msg) if err != nil { log.WithError(err).Errorf("failed to marshal error message") status = http.StatusInternalServerError data = []byte(`{"error": "failed to marshal error message"}`) err = nil } w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(status) w.Write(data) }
func TestDockerfileCreation(t *testing.T) { log.SetHandler(cli.New(os.Stdout)) // The 'expected' hash expected_hash := "033429aed5b9104f5c8d0a15ed2b9a043ce93a70" // First we will read the sample json file dat, err := ioutil.ReadFile("test/sample_data.json") if err != nil { log.WithError(err) } // We read the sample json data and create a new DockerInfo struct var parsed_data DockerInfo err = json.Unmarshal(dat, &parsed_data) if err != nil { log.WithError(err) } // Pass the DockerInfo struct into the GenerateDockerFile function GenerateDockerFile(parsed_data, "test") defer os.Remove("test/Dockerfile") // Generate a sha1 hash of the generated Dockerfile and compare f, err := ioutil.ReadFile("test/Dockerfile") if err != nil { log.WithError(err) } generated_hash := sha1.New() generated_hash.Write([]byte(f)) bs := generated_hash.Sum(nil) // We would like a hex-encoding string to compare with hash_string := hex.EncodeToString(bs[:]) expect(t, hash_string, expected_hash) }
func setupMQTT() { broker := fmt.Sprintf("tcp://%s:1883", viper.GetString("broker")) opts := MQTT.NewClientOptions().AddBroker(broker) clientID := fmt.Sprintf("ttntool-%s", util.RandString(15)) opts.SetClientID(clientID) opts.SetKeepAlive(20) opts.SetOnConnectHandler(func(client *MQTT.Client) { log.Info("Connected to The Things Network") subscribeToDevices() }) opts.SetDefaultPublishHandler(handleMessage) opts.SetConnectionLostHandler(func(client *MQTT.Client, err error) { log.WithError(err).Error("Connection Lost. Reconnecting...") }) mqttClient = MQTT.NewClient(opts) }
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() }
// Errors are passed to WithError(), populating the "error" field. func Example_errors() { err := errors.New("boom") log.WithError(err).Error("upload failed") }
// WithError returns a new entry with the "error" set to `err`. func WithError(err error) *apexlog.Entry { return apexlog.WithError(err) }