From e43cb05755ecacc5420d0ed6ca4270b9f7a53c6a Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 10 Jul 2023 13:53:25 +0200 Subject: [PATCH] Create the logger only after reading the config --- cmd/birthdaybot/main.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go index ddf5439..63345cf 100644 --- a/cmd/birthdaybot/main.go +++ b/cmd/birthdaybot/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "os" "os/signal" "syscall" @@ -11,35 +12,32 @@ import ( "github.com/charmbracelet/log" ) -func configureLogger(config *model.Config, logger *log.Logger) { +func loggerFromConfig(config *model.Config) *log.Logger { // ToDo: apply config to logger + return log.New(os.Stderr) } func main() { configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") flag.Parse() - logger := log.New(os.Stderr) - config, err := model.ReadConfig(*configFlag) if err != nil { - logger.Error("error reading config", "configPath", *configFlag, "error", err) + fmt.Fprintf(os.Stderr, "ERROR: error reading config %q: %s\n", *configFlag, err) os.Exit(1) } if err := config.IsValid(); err != nil { - logger.Error("invalid config", "configPath", *configFlag, "error", err) + fmt.Fprintf(os.Stderr, "ERROR: invalid config %q: %s\n", *configFlag, err) os.Exit(1) } - configureLogger(config, logger) - srv, err := server.New( - server.WithLogger(logger), + server.WithLogger(loggerFromConfig(config)), server.WithConfig(config), ) if err != nil { - logger.Error("error creating server", "error", err) + fmt.Fprintf(os.Stderr, "ERROR: error creating server: %s\n", err) os.Exit(1) }