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

View File

@@ -18,6 +18,9 @@ import (
func main() {
fx.New(
fx.Provide(
// Logger
NewLogger,
// Configuration
config.Load,
@@ -37,9 +40,6 @@ func main() {
handlers.NewApiEndpointHandler,
handlers.NewEmbedCardHandler,
handlers.NewOauthLoginHandler,
// Logger
NewLogger,
),
fx.Invoke(
// Start background tasks

View File

@@ -5,13 +5,15 @@ import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"
"go.uber.org/zap"
)
type GiteaOAuth2Handler struct {
cfg *config.Config
logger *zap.Logger
}
func NewGiteaOauth2Token(cfg *config.Config) *GiteaOAuth2Handler {
@@ -24,7 +26,7 @@ func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, erro
}
if redirectHost == "" {
slog.Error("Redirect host not provided")
g.logger.Error("Redirect host not provided")
return "", nil
}
@@ -35,7 +37,7 @@ func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, erro
func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error) {
if g.cfg.GiteaOauthInstance == "" {
slog.Error("Instance URL not provided")
g.logger.Error("Instance URL not provided")
return "", nil
}
// No need to verify since we are accesing the gitea once and only for the email
@@ -45,7 +47,7 @@ func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error
}
userInfoUrl := g.cfg.GiteaOauthInstance + "/login/oauth/userinfo"
slog.Info(userInfoUrl)
g.logger.Info(userInfoUrl)
req, err := http.NewRequest("POST", userInfoUrl, nil)
if err != nil {
return "", err

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,
}
}
}