Skip to content

Commit

Permalink
set up structured logging
Browse files Browse the repository at this point in the history
  • Loading branch information
somesylvie committed May 20, 2024
1 parent 0e81204 commit ee91b1d
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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
}

0 comments on commit ee91b1d

Please sign in to comment.