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())
}
}

View File

@@ -1,7 +1,7 @@
package config
import (
"log"
"log/slog"
"os"
"github.com/joho/godotenv"
@@ -22,7 +22,7 @@ var Config *config
func Load() *config {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
panic("Error loading .env file")
}
// Get mastodon instance
@@ -38,23 +38,23 @@ func Load() *config {
// Get admin password (Its a single user/admin app so its just fine)
adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
if adminPassword == "" {
log.Println("No admin password provided, using default password 'catsaregood'")
slog.Error("No admin password provided, using default password 'catsaregood'")
adminPassword = "catsaregood"
}
// Jwt params
secret := os.Getenv("CAOM_JWT_SECRET")
if secret == "" {
log.Fatal("No jwt secret provided")
panic("No jwt secret provided")
}
issuer := os.Getenv("CAOM_JWT_ISSUER")
if issuer == "" {
log.Println("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
slog.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
issuer = "CatsOfMastodonBotGo"
}
audience := os.Getenv("CAOM_JWT_AUDIENCE")
if audience == "" {
log.Println("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
slog.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
audience = "CatsOfMastodonBotGo"
}