From ee91b1d8d6cb7498efdace47e535adcf3ffe4587 Mon Sep 17 00:00:00 2001 From: Sylvie Date: Mon, 20 May 2024 16:41:51 -0500 Subject: [PATCH] set up structured logging --- src/cmd/main.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/cmd/main.go b/src/cmd/main.go index 3fb652ce..764eed5f 100644 --- a/src/cmd/main.go +++ b/src/cmd/main.go @@ -1,41 +1,52 @@ package main import ( - "fmt" "github.com/CDCgov/reportstream-sftp-ingestion/azure" "github.com/CDCgov/reportstream-sftp-ingestion/report_stream" - "log" + "log/slog" + "os" "time" ) func main() { - fmt.Println("Hello World") + + setupLogging() + + slog.Info("Hello World") //TODO: Extract the client string to allow multi-environment blobHandler, err := azure.NewBlobHandler("DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;") if err != nil { - log.Fatalf("Failed to init Azure blob client: %v", err) + slog.Error("Failed to init Azure blob client", slog.Any("error", err)) + os.Exit(1) } content, err := readAzureFile(blobHandler, "reportstream.txt") if err != nil { - log.Fatalf("Failed to read the file: %v", err) + slog.Error("Failed to read the file", slog.Any("error", err)) + os.Exit(1) } apiHandler := report_stream.ApiHandler{"http://localhost:7071"} reportId, err := apiHandler.SendReport(content) if err != nil { - log.Fatalf("Failed to send the file to ReportStream: %v", err) + slog.Error("Failed to send the file to ReportStream", slog.Any("error", err)) + os.Exit(1) } - log.Printf("File sent to ReportStream - reportId: %s", reportId) + slog.Info("File sent to ReportStream", slog.String("reportId", reportId)) for { t := time.Now() - fmt.Println(t.Format("2006-01-02T15:04:05Z07:00")) + slog.Info(t.Format("2006-01-02T15:04:05Z07:00")) time.Sleep(10 * time.Second) } } +func setupLogging() { + jsonLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) + slog.SetDefault(jsonLogger) +} + type BlobHandler interface { FetchFile(blobPath string) ([]byte, error) } @@ -47,7 +58,7 @@ func readAzureFile(blobHandler BlobHandler, filePath string) ([]byte, error) { } //TODO: Auth and call ReportStream - log.Println(string(content)) + slog.Info(string(content)) return content, nil }