// Homework 47, problem 1: Log it. // // This program checks some temperature readings and reports with fmt.Println. // Change it to use log/slog: // - a reading from -40 to 30 is logged at Info, with "sensor" and "celsius" // - a reading above 30 is logged at Warn, with the same two pairs // - a reading below -40 is a broken sensor: log it at Error // - before each check, log "checking" at Debug, with "sensor" // - a -json flag picks the JSON handler, and a -debug flag shows Debug lines // Logs go to stderr. The last line, the summary, stays on stdout. // // Run with: go run exercises/47/homework/01-log-it.go // or: go run exercises/47/homework/01-log-it.go -json -debug package main import "fmt" type reading struct { sensor string celsius int } func main() { readings := []reading{ {"kitchen", 24}, {"attic", 35}, {"garden", -99}, {"garage", 18}, } // TODO: read the two flags and build the logger here. problems := 0 for _, r := range readings { // TODO: replace these lines with log entries at the right level. if r.celsius < -40 { fmt.Println("broken sensor:", r.sensor, r.celsius) problems++ } else if r.celsius > 30 { fmt.Println("too hot:", r.sensor, r.celsius) problems++ } else { fmt.Println("ok:", r.sensor, r.celsius) } } fmt.Printf("%d readings, %d problems\n", len(readings), problems) }