Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-36 - Organize Environment and Logger init #48

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 4 additions & 39 deletions cmd/discord_bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,24 @@ import (

"github.com/caarlos0/env/v11"
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, logger.Cfg.DiscordServerID, logger.Cfg.DiscordBotToken, logger.Cfg.DiscordBotAsPublic)
serafdev marked this conversation as resolved.
Show resolved Hide resolved
bot.StartBot()
}

func loadConfig() {
if err := env.Parse(&cfg); err != nil {
if err := env.Parse(&logger.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()
}
41 changes: 41 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package logger

import (
"log"

"go.uber.org/zap"
)

var (
Logger *zap.Logger
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"`
}
serafdev marked this conversation as resolved.
Show resolved Hide resolved

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