Replace log package with slog for structured logging and improve error handling

This commit is contained in:
2025-05-17 21:37:43 +03:30
parent 3d7a3a043f
commit 99e3debf7c
2 changed files with 12 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ import (
"CatsOfMastodonBotGo/internal/server"
"CatsOfMastodonBotGo/internal/services"
"context"
"log"
"log/slog"
"time"
)
@@ -30,7 +30,7 @@ func main() {
var posts []domain.Post = nil
err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Config.Tag, config.Config.Instance)
if err != nil {
log.Println(err)
slog.Error(err.Error())
return
}
@@ -40,14 +40,14 @@ func main() {
var newAccounts = services.PostServiceInstance.GetNewAccounts(existingAccountIds, newPosts)
// Save to database
log.Printf("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts))
slog.Info("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts))
// Additional logging
if newAccounts != nil {
log.Printf("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
slog.Info("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
}
if newPosts != nil {
log.Printf("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
slog.Info("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
}
}
@@ -67,7 +67,7 @@ func main() {
r := server.SetupRouter()
err := r.Run(":8080")
if err != nil {
log.Fatal(err)
slog.Error(err.Error())
}
}