diff --git a/cmd/main.go b/cmd/main.go index 66f655a1a..c9ba5f28c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ import ( "flag" "fmt" "log" + "log/slog" "net" "net/http" _ "net/http/pprof" @@ -26,6 +27,7 @@ import ( "runtime" "runtime/pprof" + internallog "github.com/datacommonsorg/mixer/internal/log" "github.com/datacommonsorg/mixer/internal/metrics" pbs "github.com/datacommonsorg/mixer/internal/proto/service" "github.com/datacommonsorg/mixer/internal/server" @@ -113,10 +115,10 @@ var ( ) func main() { - log.Println("Enter mixer main() function") + internallog.SetUpLogger() + slog.Info("Enter mixer main() function") // Parse flag flag.Parse() - log.SetFlags(log.LstdFlags | log.Lshortfile) if *v3MirrorFraction < 0 || *v3MirrorFraction > 1.0 { log.Fatalf("v3_mirror_fraction must be between 0 and 1.0, got %f", *v3MirrorFraction) @@ -136,7 +138,7 @@ func main() { } err := profiler.Start(cfg) if err != nil { - log.Printf("Failed to start profiler: %v", err) + slog.Warn("Failed to start profiler", "error", err) } } @@ -286,7 +288,7 @@ func main() { if *useCloudSQL { if sqldb.IsConnected(&sqlClient) { - log.Printf("SQL client has already been created, will not use CloudSQL") + slog.Warn("SQL client has already been created, will not use CloudSQL") } else { client, err := sqldb.NewCloudSQLClient(*cloudSQLInstance) if err != nil { @@ -417,7 +419,7 @@ func main() { go func() { // Code from https://pkg.go.dev/net/http/pprof README httpProfileFrom := fmt.Sprintf("localhost:%d", *httpProfilePort) - log.Printf("Serving profile over HTTP on %v", httpProfileFrom) + slog.Info("Serving profile over HTTP", "address", httpProfileFrom) log.Printf("%s\n", http.ListenAndServe(httpProfileFrom, nil)) }() } @@ -427,7 +429,7 @@ func main() { if err != nil { log.Fatalf("Failed to listen on network: %v", err) } - log.Println("Mixer ready to serve!!") + slog.Info("Mixer ready to serve!!") if err := srv.Serve(lis); err != nil { log.Fatalf("Failed to serve: %v", err) } diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 000000000..66bc51c6c --- /dev/null +++ b/internal/log/log.go @@ -0,0 +1,12 @@ +package log + +import ( + "log/slog" + "os" +) + +// SetUpLogger sets up a logger. +func SetUpLogger() { + logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})) + slog.SetDefault(logger) +} diff --git a/internal/server/v2/facet/contained_in.go b/internal/server/v2/facet/contained_in.go index c5479bb47..dc9817949 100644 --- a/internal/server/v2/facet/contained_in.go +++ b/internal/server/v2/facet/contained_in.go @@ -16,7 +16,7 @@ package facet import ( "context" - "log" + "log/slog" "net/http" "sort" @@ -172,7 +172,7 @@ func btContainedInFacet( totalSeries, ) } - log.Println("Fetch series cache in contained-in observation query") + slog.Info("Fetch series cache in contained-in observation query") // When date doesn't matter, use SeriesFacet to get the facets for the // child places if queryDate == "" || queryDate == shared.LATEST { diff --git a/internal/server/v2/observation/contained_in.go b/internal/server/v2/observation/contained_in.go index 1e82735d1..3e533f3d4 100644 --- a/internal/server/v2/observation/contained_in.go +++ b/internal/server/v2/observation/contained_in.go @@ -17,7 +17,7 @@ package observation import ( "context" - "log" + "log/slog" "net/http" "sort" @@ -143,7 +143,7 @@ func FetchContainedIn( totalSeries, ) } - log.Println("Fetch series cache in contained-in observation query") + slog.Info("Fetch series cache in contained-in observation query") directResp, err := FetchDirectBT( ctx, store.BtGroup, diff --git a/test/http_memprof/http_memprof.go b/test/http_memprof/http_memprof.go index 884cd030f..5bc26ce23 100644 --- a/test/http_memprof/http_memprof.go +++ b/test/http_memprof/http_memprof.go @@ -20,6 +20,7 @@ import ( "flag" "fmt" "log" + "log/slog" "os" "os/exec" "path/filepath" @@ -188,7 +189,7 @@ func writeResultsToCsv(results []*MemoryProfileResult, outputPath string) { } func main() { - log.SetFlags(log.LstdFlags | log.Lshortfile) + slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))) flag.Parse() @@ -229,7 +230,7 @@ func main() { log.Fatalf("Could not connect to %s: timed out. Last state: %s", *grpcAddr, s) } } - log.Println("Connected to gRPC succesfully") + slog.Info("Connected to gRPC succesfully") //nolint:errcheck // TODO: Fix pre-existing issue and remove comment. defer conn.Close() diff --git a/test/setup.go b/test/setup.go index ca246f340..4a97e13ff 100644 --- a/test/setup.go +++ b/test/setup.go @@ -19,6 +19,7 @@ import ( "context" "encoding/json" "log" + "log/slog" "net" "os" "path" @@ -298,11 +299,11 @@ func UpdateGolden(v interface{}, root, fname string) { encoder.SetIndent("", " ") err := encoder.Encode(v) if err != nil { - log.Printf("could not encode golden response %v", err) + slog.Warn("could not encode golden response", "err", err) } if os.WriteFile( path.Join(root, fname), bytes.TrimRight(buf.Bytes(), "\n"), 0644) != nil { - log.Printf("could not write golden files to %s", fname) + slog.Warn("could not write golden files to", "err", err) } } @@ -316,18 +317,18 @@ func UpdateProtoGolden( // Use encoding/json to get stable output. data, err := marshaller.Marshal(resp) if err != nil { - log.Printf("marshaller.Marshal(%s) = %s", fname, err) + slog.Warn("marshaller.Marshal()", "err", err) return } var rm json.RawMessage = data jsonByte, err := json.MarshalIndent(rm, "", " ") if err != nil { - log.Printf("json.MarshalIndent(%s) = %s", fname, err) + slog.Warn("json.MarshalIndent()", "err", err) return } err = os.WriteFile(path.Join(root, fname), jsonByte, 0644) if err != nil { - log.Printf("os.WriteFile(%s) = %s", fname, err) + slog.Warn("os.WriteFile", "err", err) } } @@ -377,7 +378,7 @@ func ReadGolden(goldenDir string, goldenFile string) (string, error) { // If not enabled, it returns nil. func NewSpannerClient() *spanner.SpannerClient { if !EnableSpannerGraph { - log.Println("Spanner graph not enabled.") + slog.Info("Spanner graph not enabled.") return nil } _, filename, _, _ := runtime.Caller(0)