Improved logging with zap

This commit is contained in:
2025-09-16 12:23:12 +03:30
parent 5680f9471f
commit 6d15ce2df9
3 changed files with 18 additions and 16 deletions
+9 -9
View File
@@ -1,11 +1,11 @@
package config
import (
"log/slog"
"os"
"strings"
"github.com/joho/godotenv"
"go.uber.org/zap"
)
type Config struct {
@@ -32,10 +32,10 @@ type Config struct {
GiteaOauthAllowedEmails []string
}
func Load() *Config {
func Load(logger *zap.Logger) *Config {
err := godotenv.Load()
if err != nil {
slog.Warn("Error loading .env file - Using environment variables instead")
logger.Warn("Error loading .env file - Using environment variables instead")
}
// Get mastodon instance
@@ -51,7 +51,7 @@ func Load() *Config {
// Get admin password (Its a single user/admin app so its just fine)
adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
if adminPassword == "" {
slog.Warn("No admin password provided, using default password 'catsaregood'")
logger.Warn("No admin password provided, using default password 'catsaregood'")
adminPassword = "catsaregood"
}
@@ -62,12 +62,12 @@ func Load() *Config {
}
issuer := os.Getenv("CAOM_JWT_ISSUER")
if issuer == "" {
slog.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
logger.Info("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
issuer = "CatsOfMastodonBotGo"
}
audience := os.Getenv("CAOM_JWT_AUDIENCE")
if audience == "" {
slog.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
logger.Info("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
audience = "CatsOfMastodonBotGo"
}
@@ -89,7 +89,7 @@ func Load() *Config {
}
if dbEngine == "" || dbHost == "" || dbPort == "" || dbUser == "" || dbPassword == "" || dbName == "" {
slog.Info("No database connection provided, using sqlite")
logger.Info("No database connection provided, using sqlite")
dbEngine = "sqlite"
dbHost = ""
dbPort = ""
@@ -100,7 +100,7 @@ func Load() *Config {
imageKitId := os.Getenv("CAOM_IMAGEKIT_ID")
if imageKitId == "" {
slog.Info("No imagekit id provided, not using imagekit.io")
logger.Info("No imagekit id provided, not using imagekit.io")
}
// Initialize AppContext
return &Config{
@@ -126,4 +126,4 @@ func Load() *Config {
GiteaOauthClientSecret: giteaOauthClientSecret,
GiteaOauthAllowedEmails: giteaOauthAllowedEmailsParsed,
}
}
}