From 290418e19d603b56409f5d657d0a5f0a3347f457 Mon Sep 17 00:00:00 2001 From: HNX Date: Mon, 10 Jun 2024 02:03:49 -0400 Subject: [PATCH] ISSUE-36 - Organize Environment and Logger init (#48) * ISSUE-36 - Organize Environment and Logger init - Organizing Logger - Organizing Config --- cmd/discord_bot/main.go | 44 +++++---------------------------------- internal/config/config.go | 12 +++++++++++ internal/logger/logger.go | 34 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 internal/config/config.go create mode 100644 internal/logger/logger.go diff --git a/cmd/discord_bot/main.go b/cmd/discord_bot/main.go index d894a23..2e2b8dd 100644 --- a/cmd/discord_bot/main.go +++ b/cmd/discord_bot/main.go @@ -4,60 +4,26 @@ import ( "log" "github.com/caarlos0/env/v11" + config "github.com/ccil-kbw/robot/internal/config" environment "github.com/ccil-kbw/robot/internal/environment" + logger "github.com/ccil-kbw/robot/internal/logger" "github.com/ccil-kbw/robot/pkg/discord" - "go.uber.org/zap" -) - -// TODO: Should Types have a folder of their own? -type config struct { - Environment string `env:"ENVIRONMENT" envDefault:"dev"` - DiscordServerID string `env:"DISCORD_SERVER_ID"` - DiscordBotToken string `env:"DISCORD_BOT_TOKEN"` - DiscordBotAsPublic bool `env:"DISCORD_BOT_AS_PUBLIC" envDefault:"true"` -} - -var ( - cfg = config{} - logger *zap.Logger ) func init() { environment.LoadEnvironmentVariables() + logger.InitializeLogger() loadConfig() - initializeLogger() } func main() { - bot := discord.NewDiscordBot(logger, cfg.DiscordServerID, cfg.DiscordBotToken, cfg.DiscordBotAsPublic) + bot := discord.NewDiscordBot(logger.Logger, config.Cfg.DiscordServerID, config.Cfg.DiscordBotToken, config.Cfg.DiscordBotAsPublic) bot.StartBot() } func loadConfig() { - if err := env.Parse(&cfg); err != nil { + if err := env.Parse(&config.Cfg); err != nil { log.Fatalf("%+v\n", err) } log.Println("configuration ready") } - -func initializeLogger() { - var err error - if cfg.Environment == "dev" { - logger, err = zap.NewDevelopment() - if err != nil { - initExampleLoggerWhenFail() - } - logger.Info("Using Development Logger") - } else { - logger, err = zap.NewProduction() - if err != nil { - initExampleLoggerWhenFail() - } - logger.Info("Using Production Logger") - } -} - -func initExampleLoggerWhenFail() { - log.Println("can't initialize zap logger, using basic logger") - logger = zap.NewExample() -} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..36cb42d --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,12 @@ +package config + +var ( + Cfg = config{} +) + +type config struct { + Environment string `env:"ENVIRONMENT" envDefault:"dev"` + DiscordServerID string `env:"DISCORD_SERVER_ID"` + DiscordBotToken string `env:"DISCORD_BOT_TOKEN"` + DiscordBotAsPublic bool `env:"DISCORD_BOT_AS_PUBLIC" envDefault:"true"` +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..01d8767 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,34 @@ +package logger + +import ( + "log" + + config "github.com/ccil-kbw/robot/internal/config" + "go.uber.org/zap" +) + +var ( + Logger *zap.Logger +) + +func InitializeLogger() { + var err error + if config.Cfg.Environment == "dev" { + Logger, err = zap.NewDevelopment() + if err != nil { + initExampleLoggerWhenFail() + } + Logger.Info("Using Development Logger") + } else { + Logger, err = zap.NewProduction() + if err != nil { + initExampleLoggerWhenFail() + } + Logger.Info("Using Production Logger") + } +} + +func initExampleLoggerWhenFail() { + log.Println("can't initialize zap logger, using basic logger") + Logger = zap.NewExample() +}