Skip to content

Commit

Permalink
Merge pull request #4 from flxw/use-slog
Browse files Browse the repository at this point in the history
chore: use slog for structured log messages
  • Loading branch information
flxw committed Sep 25, 2023
2 parents 90b53f0 + bc0bfb4 commit 1ee6614
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package main
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"log/slog"
"sync"
"time"

Expand Down Expand Up @@ -66,7 +65,7 @@ func main() {
func RefreshMaterializedViews() {
db, err := gorm.Open(postgres.Open(Config.Database.String), &gorm.Config{})
if err != nil {
log.Panicln("failed to connect database")
slog.Error("failed to connect to database", "message", err.Error())
}

db.Exec("REFRESH MATERIALIZED VIEW keyless_usage_per_month_mv")
Expand All @@ -76,7 +75,7 @@ func RefreshMaterializedViews() {
func CommenceCrawlRun() {
db, err := gorm.Open(postgres.Open(Config.Database.String), &gorm.Config{})
if err != nil {
log.Panicln("failed to connect database")
slog.Error("failed to connect to database", "message", err.Error())
}

db.AutoMigrate(&CrawledEntry{})
Expand All @@ -90,7 +89,7 @@ func CommenceCrawlRun() {
maximumIndex := CalculateCurrentMaximumIndex(rekorClient)
targetIndex := Min(startIndex+BATCH_MAXIMUM, maximumIndex)

log.Println("Crawling the following index range:", startIndex, targetIndex)
slog.Info("Commencing crawl run", "startIndex", startIndex, "targetIndex", targetIndex)

rekordQueue := make(chan CrawledEntry)
go SpawnRekorCrawlerRoutines(startIndex, targetIndex, rekorClient, rekordQueue)
Expand All @@ -101,7 +100,7 @@ func CommenceCrawlRun() {
}

db.Create(&crawledEntries)
log.Println("Finished crawling and persisting the entries")
slog.Info("Finished crawl run and persisted the entries")
}

func SpawnRekorCrawlerRoutines(fromIndex int64, toIndex int64, rekorClient *rekorClient.Rekor, rekordQueue chan CrawledEntry) {
Expand Down Expand Up @@ -140,7 +139,7 @@ func FetchEntryByUuid(rekorClient *rekorClient.Rekor, index int64, wg *sync.Wait
resp, err := rekorClient.Entries.GetLogEntryByIndex(params)

if err != nil {
fmt.Println(err)
slog.Error(err.Error())
return
}

Expand All @@ -149,19 +148,19 @@ func FetchEntryByUuid(rekorClient *rekorClient.Rekor, index int64, wg *sync.Wait

pe, err := models.UnmarshalProposedEntry(bytes.NewReader(b), runtime.JSONConsumer())
if err != nil {
log.Println(err)
slog.Error(err.Error())
return
}

eimpl, err := types.UnmarshalEntry(pe)
if err != nil {
log.Println(err)
slog.Error(err.Error())
return
}

verifier, err := eimpl.Verifier()
if err != nil {
log.Println(err)
slog.Error(err.Error())
return
}

Expand All @@ -172,7 +171,7 @@ func FetchEntryByUuid(rekorClient *rekorClient.Rekor, index int64, wg *sync.Wait

identities, _ := verifier.Identities()
if err != nil {
log.Println(err)
slog.Error(err.Error())
return
}

Expand All @@ -194,7 +193,7 @@ func DetermineMostRecentlyCrawledIndex(db *gorm.DB) int64 {
db.Raw(query).Scan(&index)
if index == -1 {
index = Config.Rekor.StartIndex
log.Println("Could not fetch index from db, falling back to config start index:", index)
slog.Warn("Could not fetch index from db, falling back to config start index")
}

return index
Expand Down

0 comments on commit 1ee6614

Please sign in to comment.