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/server"
"CatsOfMastodonBotGo/internal/services" "CatsOfMastodonBotGo/internal/services"
"context" "context"
"log" "log/slog"
"time" "time"
) )
@@ -30,7 +30,7 @@ func main() {
var posts []domain.Post = nil var posts []domain.Post = nil
err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Config.Tag, config.Config.Instance) err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Config.Tag, config.Config.Instance)
if err != nil { if err != nil {
log.Println(err) slog.Error(err.Error())
return return
} }
@@ -40,14 +40,14 @@ func main() {
var newAccounts = services.PostServiceInstance.GetNewAccounts(existingAccountIds, newPosts) var newAccounts = services.PostServiceInstance.GetNewAccounts(existingAccountIds, newPosts)
// Save to database // 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 // Additional logging
if newAccounts != nil { 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 { 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() r := server.SetupRouter()
err := r.Run(":8080") err := r.Run(":8080")
if err != nil { if err != nil {
log.Fatal(err) slog.Error(err.Error())
} }
} }

View File

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